Android RecyclerView with Multiple View Types and ItemTouchHelper

Viewed 508

I'm trying to get ItemTouchHelper to work with a RecyclerView that has multiple view types. I've seen a few answers that say that in order to get this to work the recyclerView adapter has to have setHasStableIds(true) and overrider getItemId(int position) -making sure your items have stable ids. However, I can not seem to get this to work, and cannot seem to find any actual examples of an implementation.

Can anyone provide or point to a working example of using ItemTouchHelper with multiple view types?

1 Answers

public class NewsAdapter extends RecyclerView.Adapter {

private static final String TAG = NewsAdapter.class.getSimpleName();
private ArrayList<Article> articles = new ArrayList<>();

public void setList(ArrayList<Article> articles) {
    this.articles = articles;
    notifyDataSetChanged();
}

setList

private static final String TAG = NewsAdapter.class.getSimpleName();
private ArrayList<Article> articles = new ArrayList<>();

public void setList(ArrayList<Article> articles) {
    this.articles = articles;
    notifyDataSetChanged();
}

onCreateViewHolder

 @NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view;

    // Here types are: 0=item news image, 1=item news horizontal, 2=Card
    if (viewType == 0) {
        view = layoutInflater.inflate(R.layout.item_news_image, parent, false);
        return new ImageViewHolder(view);
    } else {
        view = layoutInflater.inflate(R.layout.item_news_horizontal, parent, false);
        return new HorizontalViewHolder(view);
    }
}

onBindViewHolder

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (position == 0) {
        // bind viewHolder 'item news image'
        ImageViewHolder imageViewHolder = (ImageViewHolder) holder;

        Glide.with(imageViewHolder.itemView.getContext()).load(articles.get(position).getUrlToImage()).into(imageViewHolder.urlToImageView);
        imageViewHolder.publishedAtTextView.setText(articles.get(position).getPublishedAt());
        imageViewHolder.authorTextView.setText(articles.get(position).getAuthor());
        imageViewHolder.titleTextView.setText(articles.get(position).getTitle());
    } else {
        // bind viewHolder 'horizontal'
        HorizontalViewHolder horizontalViewHolder = (HorizontalViewHolder) holder;

        Glide.with(horizontalViewHolder.itemView.getContext()).load(articles.get(position).getUrlToImage()).into(horizontalViewHolder.urlToImageView);
        horizontalViewHolder.publishedAtTextView.setText(articles.get(position).getPublishedAt());
        horizontalViewHolder.authorTextView.setText(articles.get(position).getAuthor());
        horizontalViewHolder.titleTextView.setText(articles.get(position).getTitle());
    }
}

getItemCount

@Override
public int getItemCount() {
    return articles.size();
}

getItemViewType

 @Override
public int getItemViewType(int position) {
    if (position == 0) {
        return 0;
    } else {
        return 1;
    }
}

ImageViewHolder

static class ImageViewHolder extends RecyclerView.ViewHolder {

    ImageView urlToImageView;
    TextView publishedAtTextView, authorTextView, titleTextView;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);
        urlToImageView = itemView.findViewById(R.id.imageINIUrlToImage);
        publishedAtTextView = itemView.findViewById(R.id.textINIPublishedAt);
        authorTextView = itemView.findViewById(R.id.textINIAuthor);
        titleTextView = itemView.findViewById(R.id.textINITitle);
    }
}

HorizontalViewHolder

static class HorizontalViewHolder extends RecyclerView.ViewHolder {

    ImageView urlToImageView;
    TextView publishedAtTextView, authorTextView, titleTextView;

    public HorizontalViewHolder(@NonNull View itemView) {
        super(itemView);
        urlToImageView = itemView.findViewById(R.id.imageINHUrlToImage);
        publishedAtTextView = itemView.findViewById(R.id.textINHPublishedAt);
        authorTextView = itemView.findViewById(R.id.textINHAuthor);
        titleTextView = itemView.findViewById(R.id.textINHTitle);
    }
}

MainActivity

 final NewsAdapter newsAdapter = new NewsAdapter();
 binding.recyclerViewTopStories.setAdapter(newsAdapter);

 topStoriesViewModel.getList().observe(getViewLifecycleOwner(), newsAdapter::setList);

enter image description here

Related