Set a background color to a selected ListView Item in android

后端 未结 7 1605
甜味超标
甜味超标 2020-12-15 23:29

I have a listview which displays several items. Now I want to scroll to some specific item (e.g. the 33th item). I know that this can be done via

myList.setS         


        
相关标签:
7条回答
  • 2020-12-15 23:58

    I tried by the following way and solved the problem.

    Created a javaBean class that receives the position. Like following.

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

    Then from The OnListItemClickListener() I set the position of the selected item. Like following

    mList.setOnItemClickListener(new OnItemClickListener() {
    
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Global.setListPosition(arg2);                   
        }
    });
    

    Then in the adapter class do the following

    @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.image1);
        } else {
            mHolder.mDetailsRetailerLayout
                    .setBackgroundResource(R.drawable.image2);
        }
    
        if (mDetailsRetailerBean != null) {
            Log.i("Global Position", "" + Global.getListPosition());
            mHolder.mDetailsRetailer.setText(mDetailsRetailerBean
                    .getRetailerName());
        }
        return v;
    }    
    

    Try this one to change the Selected row background color change in Android List view.

    0 讨论(0)
提交回复
热议问题