选中android时突出显示所选的水平列表项

时间:2014-09-18 08:04:39

标签: android listview selection selecteditem

enter image description here

游戏,社区,音乐等处于水平列表视图中。

现在选择社区,社区相关数据如下所示。

我想在点击音乐时突出显示音乐,并将社区颜色更改为默认值。

listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {

((android.widget.LinearLayout)((android.widget.LinearLayout)arg1).getChildAt(2)).setBackgroundColor(getResources().getColor(R.color.MyBlue));
...

这会更改所选项目的背景颜色,但是当我滚动其他项目时也会更改未选中的项目。 上面的代码更改了线性布局,该布局在下面显示的项目xml文件中定义。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <ImageView 
        android:id="@+id/IVCategory"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:layout_margin="10dp"
        android:layout_gravity="center"    
        />

    <TextView 
        android:id="@+id/txtCatName"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="@android:color/transparent"
        />

    <LinearLayout
        android:id="@+id/selectedBlue"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

我可以按照我这样做的方式做到这一点吗?我经常搜索,但我无法理解选择者如何帮助我。

1 个答案:

答案 0 :(得分:0)

    * Create Class:

    public class Test {
        private String  name;
        private int selected = 0;


        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public void setSelected(int selected) {
            this.selected = selected;
        }

        public int getSelected() {
            return selected;
        }
    }


Create Custom adaptor:

    public class TestAdaptor extends BaseAdapter {
        Context context;
        List<Test> list;
        private LayoutInflater inflater;
        public TestAdaptor(Context context, List<Test> list ) {
            this.context  =  context;
            this.list  =  list;
            this.inflater  =  LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public test getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            try {
                if(rowView == null)
                    rowView =  this.inflater.inflate(R.layout.volume_list_item, null);

                if(list.get(position).getSelected() == 1){
                    rowView.setBackgroundColor(this.context.getResources().getColor(R.color.app_color));
                }else{
                    rowView.setBackgroundColor(this.context.getResources().getColor(R.color.gray));
                }
                rowView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        list.get(position).setSelected(1);
                        notifyDataSetChanged();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rowView;
        }
    }*


finally:

listview.setAdaptor(new TestAdaptor(this, lsit));