列表视图与listadapter突出显示项目

时间:2012-02-24 16:26:28

标签: android listview highlight

我正在使用带有以下代码的ListView。问题是,当我点击列表项时,它没有突出显示。你看,我尝试使用arg0.setBackgroundColor(Color.RED);,但这会导致当我点击该项目并且不要让我的手指向上,没有任何反应,当我点击该项目并让我的手指向上,它会突出显示并且保持突出显示。 当我放开手指突出显示时如何删除? 我不知道为什么这不像一个简单的列表视图。

 private class ListAdapter extends ArrayAdapter { 
        private ArrayList mList; 
        private Context mContext;

        public ListAdapter(Context context, int textViewResourceId, ArrayList list) {
            super(context, textViewResourceId, list);
            this.mList = list;
            this.mContext = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            try {
                if (view == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.list_item, null);
                }

                final Object listItem = mList.get(position);
                if (listItem != null) {

                    ((TextView) view.findViewById(R.id.tv_name)).setText(((HotOrNot) listItem).getName());
                    view.setOnClickListener(new OnClickListener() {
                        public void onClick(View arg0) {
                            arg0.setBackgroundColor(Color.RED);
                            Toast.makeText(SQLView.this, "ID: " + ((HotOrNot) listItem).getID(), Toast.LENGTH_SHORT).show();
                            Toast.makeText(SQLView.this, "Name: " + ((HotOrNot) listItem).getName(), Toast.LENGTH_SHORT).show();
                            Toast.makeText(SQLView.this, "Descr: " + ((HotOrNot) listItem).getDescription(), Toast.LENGTH_SHORT).show();


                           // finish();
                        }
                    });
                }
            } catch (Exception e) {
                //Log.i(Splash.ListAdapter.class.toString(), e.getMessage());
            }
            return view;
        }
    }

1 个答案:

答案 0 :(得分:0)

将此xml文件放在drawable文件夹中,并将list_item设置为mainlayout中的背景

  <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:drawable="#FFFFFF" android:state_pressed="false" android:state_selected="false"/>
        <item android:drawable="#FF00FF" android:state_pressed="true" android:state_selected="false"/>
        <item android:drawable="#FF00FF" android:state_pressed="true" android:state_selected="true"/>

    </selector>