如何设置不同的listselector如何设置、不同、listselector

2023-09-08 08:44:59 作者:侽銀就昰贏

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View item,
                    int position, long id) {

        if(position == 0)
            mainListView.setSelector(R.drawable.settings_selector_up);
        else if(position == mainListView.getCount())
            mainListView.setSelector(R.drawable.settings_selector_down);
        else
            mainListView.setSelector(R.drawable.settings_selector_mid);
            }
                });

我有ListView控件,我的ListView有圆角。我用3选择器列表视图。所以,当我在ListView中的第一项选择应该settings_selector_up其中有一个圆角只在左上和topright。在列表视图没有圆角中间,列表视图的最后一个项目应该有一轮BOTTOMLEFT和bottomright角落。我用这个在onitemClickListener,但它不是一个很好的点。是否有另一种方式来做到这一点?

I have listview, my listview has a rounded corner. I use 3 selectors for listview. So when i select in the first item of listView should work settings_selector_up which has a rounded corner only in topleft and topright. In the middle of listview no rounded corners, the last item of listview should have round bottomleft and bottomright corner. I use this in onitemClickListener, but it is not a good point. Is there another a way to do this?

推荐答案

嗯,这似乎是一个pretty的快速解决的问题,但实际并非如此。您应修改您的适配器,并使其接受3种不同类型的项目(上,下,中),然后改变getView中是这样的:

Well, it seems to be a pretty quick problem to solve but actual it is not. You should modify your adapter and make it to accept 3 different type of items (up, down, mid) and then change the getView in something like this:

...
if (convertView == null) {
   convertView = getLayoutInflater().inflate(R.layout.your_layout, parent, false);

   switch (type) {
       case TYPE_ITEM_UP:
           convertView.setBackgroundResource(R.drawable.selector_up);
           break;

   case TYPE_ITEM_DOWN:
        convertView.setBackgroundResource(R.drawable.selector_down);
        break;
       ...
    }
}
...

看看这个例如,可以减少你的问题是,非常有帮助。我用的确切您的问题相同的解决方案,它工作得很好。

Take a look to this example, you can reduce your problem to that, very helpful. I used the same solution for the exact your problem and it works well.