【问题标题】:Listview with listadapter highlight item带有 listadapter 突出显示项的 Listview
【发布时间】:2012-03-15 02:43:45
【问题描述】:

我正在使用带有以下代码的 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;
        }
    }

【问题讨论】:

标签: android listview highlight


【解决方案1】:

将此 xml 文件放入您的 drawable 文件夹和主布局中的 list_item 设置背景中

  <?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>

【讨论】:

  • 我无法理解你的评论
  • 你无法理解我的答案 ....首先从 复制我的答案,然后右键单击 drawable 文件夹并创建 xml 文件并命名为 list_bg 并保存在 drawable文件夹(不要在布局中),然后粘贴,,,然后在您的布局中 list_item 有任何布局并给 android:background="@drawable/list_bg" 然后清理并构建您的项目并重新运行
猜你喜欢
  • 2013-02-04
  • 1970-01-01
  • 2015-09-08
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2013-11-18
  • 1970-01-01
相关资源
最近更新 更多