4

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?

drod
  • 359
  • 5
  • 17
  • please find this https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.html – yogesh lokhande Sep 30 '17 at 14:56
  • @pskink -without setHasStableIds(true) when dragging, item drops immediately after crossing first item of different view type. with setHasStableIds(true) does same thing but item also disappears. Works perfect without multiple view types. – drod Sep 30 '17 at 14:56
  • @pskink -no haven't overridden and drop methods. anything specific you recommend looking at? – drod Sep 30 '17 at 15:45
  • @pskink yep seen that and will look again. But back to the original question -can you point me to a working example with multiple view types? – drod Sep 30 '17 at 15:47
  • @pskink I've gone over the docs again for 10th time. there is no info about itemtouchhelper with multiple viewtypes. Can you or anyone confirm it actually works? Or point to docs that address this question, thanks – drod Sep 30 '17 at 17:41
  • @drod, this is a very late reply, but it does indeed work in my app, and It shouldn't be much different from using ItemTouchHelper with a single view type. If this is still relevant for you, could you provide the code you're using that does not work correctly? – Max May 20 '23 at 11:38

1 Answers1

-1

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

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '21 at 00:30