How to set ListView selected item alternet text color in android

前端 未结 5 1269
猫巷女王i
猫巷女王i 2021-01-07 01:31

I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.<

5条回答
  •  广开言路
    2021-01-07 01:59

    Like others have said, just change the color in your onItemClick method. Additionally:

    @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
    
    
            setItemNormal();
            View rowView = view;
            setItemSelected(rowView);
    
        }
    
    public void setItemSelected(View view){
        View rowView = view;
        TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
        tv.setTextColor(Color.BLUE);
    }
    
    public void setItemNormal()
    {
        for (int i=0; i< mDrawerList.getChildCount(); i++)
        {
            View v = mDrawerList.getChildAt(i);
            //TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
            TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
            txtview.setTextColor(Color.BLACK);
        }
    }
    

    by doing this we set all items to normal before we change it on each item press.

提交回复
热议问题