Prevent the adapter from recycling views on scroll ( Edit do not ever do this.)

后端 未结 4 1184
走了就别回头了
走了就别回头了 2020-12-16 04:35

I have a custom base adapter that will take in an arraylist of data. From here it will fill out a grid view with custom buttons. It does so perfectly and fills up the gridvi

4条回答
  •  佛祖请我去吃肉
    2020-12-16 05:15

    I had this exact problem So I added some code to check every view that it was not highlighted and when it found the highlighted view it changed it back.

    //--------SET-FOREGROUND-IMAGE-(BORDER)------------
        /*If the user clicks on an item and then scrolls down so the selected item is no longer in view Then the
        Item that the user clicked on will be recycled with the foreground image.
        This is BAD because when the user sees the selected item (as distinguished by it's different border)
        it will be holding different data from a different data model item.
        These following lines of code will change the colour of any non selected item back to normal and they will
        colour of the selected views appropriately*/
            if(currentLine.getLineId() == clickedId)
            {recycleHolder.cardView.setForeground(parentActivity.getResources().getDrawable(R.drawable.card_view_border_selected));}
            else
            {recycleHolder.cardView.setForeground(parentActivity.getResources().getDrawable(R.drawable.card_view_border));}
    

    and this code is placed within

     @Override
    public void onBindViewHolder(final RecyclableViewHolder recycleHolder, int i)
    {
     }
    

提交回复
热议问题