1

重複の可能性:
選択したリスト項目の背景を変更する方法、アンドロイド

設定画面の Android タブレットでは、ユーザーが次の選択を行うまで、リストの listitem の背景が変わります。これは、その機能を動作させる標準的な方法です。今まで、選択されたアイテムにブール値を使用し、クリックするたびに変更を反映するためにリストを更新していましたが、これは正しいアプローチですか?

4

1 に答える 1

1
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.
于 2012-11-20T13:19:12.993 に答える