Android-Listview items background color changing when scrolling

后端 未结 4 498
小蘑菇
小蘑菇 2021-01-14 17:41

My ListView contains two Textviews. In one row first one is for name and second one is for result. I need to change the background color of the re

4条回答
  •  萌比男神i
    2021-01-14 18:29

    Set this code on adapter (custom), so you can recycle the view with the previous color only if the view is selected.

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE );
    
        Task task = taskArrayList.get(position);
    
        View view = inflater.inflate(R.layout.task_row, parent, false);
    
        if(MyActivity.getIsClicked() && MyActivity.getPositionClicked() == position){
            view.setBackgroundResource(R.color.backgroundSelectedItem);
        }
    

提交回复
热议问题