简体   繁体   中英

Listview Change Item Text color on Button Click

I have a list view with custom adapter and when i click on first item there's button("next) below list view is visible and the first row item text color changes. On click of button Next i want to change the next row item text color. Please Help Thank you.

  mainListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    itemPosition =i;
            String text = textView.getText().toString();
            Toast.makeText(getContext(), "Selected item: " + text + " - " + i, Toast.LENGTH_SHORT).show();

            song_namef.setText(text);

            playSong(i);
            hello.setVisibility(View.VISIBLE);

            setItemNormal();
            View rowView = view;
            setItemSelected(rowView);

        }
    });
 public void setItemSelected(View view){
    View rowView = view;
    TextView tv = (TextView)rowView.findViewById(R.id.textView);
    tv.setTextColor(Color.WHITE);
}

public void setItemNormal()
{
    for (int i=0; i< mainListViews.getChildCount(); i++)
    {
        View v = mainListViews.getChildAt(i);
        TextView txtview = ((TextView)v.findViewById(R.id.textView));
        txtview.setTextColor(getResources().getColor(R.color.tabsScrollColor));
    }}
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
   Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show();

                itemPosition=itemPosition+1;
here i want to color next row of item text
}});

getView

 public View getView(final int position, View convertView, ViewGroup viewGroup) {

         val = position;
        viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.list_items, viewGroup, false);

            // Find the child views.
            viewHolder.country = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.cancel = (ImageView)convertView.findViewById(R.id.button);



            convertView.setTag(viewHolder);
            //  convertView.setTag(vh);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
            //vh = (ViewHolder)vi.getTag();
        }
        String hello = countryList.get(position);
        Log.d("Hello",hello);


        viewHolder.country.setText(hello.substring(18));

First of all create a selector.xml and

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector> 

After that in layout of item of List define the textColor attribute as:

android:textColor="@color/list_item_text"

Try this:

hello.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show();
        itemPosition = itemPosition + 1;

        setItemNormal();
        // check if the next view is visible on screen, then change color
        if(itemPosition <= mainListViews.getLastVisiblePosition()){
            View rowView = mainListViews.getChildAt(itemPosition - mainListViews.getFirstVisiblePosition());
            setItemSelected(rowView);
        }
}});

Hope that help!

By the way, directly modify the view is bad because view will be redrawn when list is scrolled. Please review your code if your list is long. Also given the variables suitable names, eg "hello"->"btnNext", will help the others to understand your code and suggest a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM