0

I'm using recyclerview to show json array data. In adapter I checked if a file exists, ui elements would be different behavior like change textview color and button text. Everything is good but when scrolling down, every 5 item, one item gives same behavior with correct condition but item's condition incorrect. For example progressbar on the asynctask's onprogressupdate apply to multiple items. If click download item 1, progress apply to item number 1,6,11,... I tried to use this.setIsRecyclable(false) inside MyViewHolder constructor. At first glance it's seems work fine but when scroll down and back again to item, ui elements like progressbar not work's fine (before complete download, button setEnabled back to true) .

public void bind(final Context context, 
    final WallpaperList myWallpaperCollection, final OnItemClickListener listener) {

    btnDownload=itemView.findViewById(R.id.button_download);
    btnApply=itemView.findViewById(R.id.button_apply);
    progressBar=itemView.findViewById(R.id.pb);

    tvWallpaperId=itemView.findViewById(R.id.wallpaper_id);
    imageWallpaper=itemView.findViewById(R.id.wallpaper_image);

    progressBar.setMax(100);

    tvWallpaperId.setText(myWallpaperCollection.getName());

    int adapterPosition=getAdapterPosition();

    if(interMainFile.exists()){
    tvWallpaperId.setTextColor(tvWallpaperId.getContext()
          .getResources().getColor(R.color.buttonbackground2));
        btnDownload.setText("preview");
        progressBar.setProgress(100);

    } else {
        btnDownload.setText("download");
        progressBar.setProgress(0);

        tvWallpaperId.setTextColor(tvWallpaperId.getContext()
           .getResources().getColor(R.color.newColor));
        }

        btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(interMainFile.exists()){
                    btnDownload.setText("preview");
                } else {
                    btnDownload.setText("download");
                    btnDownload.setEnabled(false);

                    progressBar.setProgress(0);
                    new DownloadTask().execute(url_to_download_main);
                }
            }
        });
    }
Heinz Schilling
  • 2,177
  • 4
  • 18
  • 35

1 Answers1

0

It's because all views is Recycled during scrolling. RecyclerView widget re using Views during scrolling to provide best UX, and smooth appearing.

In you case, you need to define several View Types, in Adapter. As described in the another Question.

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder0(View itemView){
        ...
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder2(View itemView){
        ...
    }

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be contiguous
        return position % 2 * 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolder0 viewHolder0 = (ViewHolder0)holder;
                ...
                break;

            case 2:
                ViewHolder2 viewHolder2 = (ViewHolder2)holder;
                ...
                break;
        }
    }
}
GensaGames
  • 5,538
  • 4
  • 24
  • 53