4

我正在尝试让 ItemTouchHelper 与具有多种视图类型的 RecyclerView 一起使用。我已经看到一些答案说,为了让它工作,recyclerView 适配器必须有 setHasStableIds(true) 和 overrider getItemId(int position) - 确保你的项目有稳定的ID。但是,我似乎无法让它工作,并且似乎找不到任何实际的实现示例。

任何人都可以提供或指出使用 ItemTouchHelper 和多种视图类型的工作示例吗?

4

1 回答 1

-1

公共类 NewsAdapter 扩展 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();
}

设置列表

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());
    }
}

获取项目计数

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

获取项目视图类型

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

图像视图持有者

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);
    }
}

Horizo​​ntalViewHolder

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);
    }
}

主要活动

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

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

在此处输入图像描述

于 2021-10-04T21:50:29.723 回答