Change ListView Item Background on click - Android

前端 未结 3 774
陌清茗
陌清茗 2020-12-20 09:24

I\'ve prepared a custom listview using BaseAdapter. Now I want to change color of selected item of listview on click event. And multiple items should be selected. Here I am

3条回答
  •  心在旅途
    2020-12-20 10:01

    Its very simple... Just try the following code...

    In your List Adapter:

    Define an Integer Array first

    ArrayList itemPos = new ArrayList();
    

    then use this code in your getView Method :

            if (itemPos.contains(position)) {
                holder.txtOne.setTextColor(Color.BLUE);
            } else {
                holder.txtOne.setTextColor(Color.WHITE);
            }
    

    Now use this code in click event of your Text View :

                if (!itemPos.contains(position)) {
                    holder.txtOne.setTextColor(Color.BLUE);
                    itemPos.add(position);
                    notifyDataSetChanged();
                } else {
                    holder.txtOne.setTextColor(Color.WHITE);
                    notifyDataSetChanged();
                    int po = itemPos.indexOf(position);
                    itemPos.remove(po);
                }
    

提交回复
热议问题