android - 如何更改在android中选择的列表项的背景

标签 android android-listview

<分区>

Possible Duplicate:
how to change selected list item background, android

在设置屏幕的 android 平板电脑中,列表中列表项的背景会发生变化,直到用户做出下一个选择,这是使该功能正常工作的标准方式。到目前为止,我对选定的项目使用了一个 bool 值,并且我刷新列表以反射(reflect)每次单击后的更改,我不这么认为,这是正确的方法吗?

最佳答案

You can use the following process . It works fine.

   Write a Global bean that get the postion of the selected item from the list.

     public class Global {      
        public static int mListPosition = 0; 

        public static int getListPosition() {
                return mListPosition;
            }    
        public static void setListPosition(int mListPosition) {
                Global.mListPosition = mListPosition;
            }   

        }

from List onClickListener set the position into the global bean


mList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Global.setListPosition(arg2);
mAdapter.notifyDataSetChanged();
}
        });



in the adapter 

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.details_retailer_list_row, null);

                mHolder = new ViewHolder();
                v.setTag(mHolder);

                mHolder.mDetailsRetailerLayout = (LinearLayout) v.findViewById(R.id.details_cart_retailer_row_layout);
                mHolder.mDetailsRetailer = (TextView) v.findViewById(R.id.detailsRetailerRow);

            }
            else {
                mHolder =  (ViewHolder) v.getTag();
            }           


            final CartDetailsRetailerBean mDetailsRetailerBean = mItems.get(position);
            if (position == Global.getListPosition()) {
                mHolder.mDetailsRetailerLayout
                        .setBackgroundResource(R.drawable.row_white);
            } else {
                mHolder.mDetailsRetailerLayout
                        .setBackgroundResource(R.drawable.row_red);
            }

            if(mDetailsRetailerBean != null){

                Log.i("Global Position", ""+Global.getListPosition());



                mHolder.mDetailsRetailer.setText(mDetailsRetailerBean.getRetailerName());

            }

            return v;
        }

        class ViewHolder {
            public LinearLayout mDetailsRetailerLayout;
            public TextView mDetailsRetailer;

        }


Please apply it . I think it will work fine.

关于android - 如何更改在android中选择的列表项的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13473375/

相关文章:

java - 将第二个 TextView 添加到 customAdapter ListView

android - 复用同一个ListView来显示不同的数据

android - 使用 columnIndex 在 android 中调用 OnItem 单击

android - 如何在 ListView 中使用 Baseadapter?为什么会出现 Null 点异常?

java - 处理 onClick 事件时 getView 出现空指针异常

android - 相当于 'configurations.properties'文件的Gradle

java - Android - 触摸屏幕时执行某些操作

android - CustomLayout 的 subview 中出现意外的命名空间前缀应用程序

android - Android 中相当于 WPF Grid 的控件

android - 获取 ListView 的适配器