If i change first item background color then after some items another item also get changed

127 views Asked by At

I tried in getView aswell, this is for selected item should be highlighted. here is my code. I have seen each answers which is relative to this but not worked for me.

 slots.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                for(int i=0;i<list.size();i++)
                {
                    View v=getViewByPosition(i,slots);

                    if(i==position)
                    {
                        v.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                    }
                    else {
                        v.setBackgroundColor(getResources().getColor(R.color.AppBackground));
                    }

                }

            }
        });
        progress.dismiss();
    }

    public View getViewByPosition(int pos, HorizontalListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }
1

There are 1 answers

0
Vitaliy  Ptitsin On BEST ANSWER

Main idea of ListView\ RecyclerView is View's reusing. So when you scrolling firs live item on screen, moved to new down item. Create Holder class, with list of vies. And method Bind, when yo set righ color of item, based of you adapter model.sample