gpt4 book ai didi

android - 如何将 DiffUtil 与多种 View 类型一起使用

转载 作者:行者123 更新时间:2023-12-04 23:45:23 26 4
gpt4 key购买 nike

我已经实现了具有多种 View 类型的 recyclerview,并且我想添加 DiffUtil。我设法为一种类型做到了,但是当我尝试支持多种 View 类型时,我不能

所以我的问题是:DiffUtil 是否支持多种 View 类型,是否有一个很好的 java 实现教程

我的适配器


public class PostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_AD = 1;
private final int VIEW_TYPE_CATEGORIES = 2;
private final int VIEW_TYPE_NULL = 99;

private Context context;
private List<Object> postList;

public PostsAdapter() {
}

/*public PostsAdapter() {
super(Post.DIFF_CALLBACK);
}*/

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();

switch (viewType){
case VIEW_TYPE_ITEM:
View viewItem = LayoutInflater.from(context).inflate(R.layout.item_post,
parent, false);
return new ItemViewHolder(viewItem);

case VIEW_TYPE_AD:
View unifiedNativeLayoutView = LayoutInflater.from(context).inflate(R.layout.item_ads,
parent, false);
return new AdsViewHolder(unifiedNativeLayoutView);

case VIEW_TYPE_CATEGORIES:
View viewCategories = LayoutInflater.from(context).inflate(R.layout.item_categories,
parent, false);
return new HorizontalItemViewHolder(viewCategories);
default:
View viewLoading = LayoutInflater.from(context).inflate(R.layout.item_loading,
parent, false);
return new LoadingViewHolder(viewLoading);
}
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);

switch (viewType){
case VIEW_TYPE_ITEM:
populateItemRows((ItemViewHolder) holder, position);
break;
case VIEW_TYPE_AD:
populateNativeAdModule((AdsViewHolder) holder, position);
break;
case VIEW_TYPE_CATEGORIES:
populateHorizontalItems((HorizontalItemViewHolder) holder, position);

default:
showLoadingView();
}
}

@Override
public int getItemViewType(int position) {
Object recyclerViewItem = postList.get(position);

if(recyclerViewItem instanceof UnifiedNativeAd){
return VIEW_TYPE_AD;
}else if(postList.get(position) == null){
return VIEW_TYPE_NULL;
// Empty Ad view
}else{
return VIEW_TYPE_ITEM;
}

}

@Override
public int getItemCount() {
return 0;
}



private static class ItemViewHolder extends RecyclerView.ViewHolder {

private CardView cardView1;

ItemViewHolder(@NonNull View itemView) {
super(itemView);
cardView1 = itemView.findViewById(R.id.cv_post);


}

void setName(String text1) {
TextView name = itemView.findViewById(R.id.name);
name.setText(text1);
}

}



private static class HorizontalItemViewHolder extends RecyclerView.ViewHolder {

private RecyclerView RV;

HorizontalItemViewHolder(@NonNull View itemView) {
super(itemView);

// Samedi
RV = itemView.findViewById(R.id.rv_categories);
}


}

private static class AdsViewHolder extends RecyclerView.ViewHolder {
TemplateView template ;

AdsViewHolder(@NonNull View itemView) {
super(itemView);
template = itemView.findViewById(R.id.adview_feed);

}
}

private static class LoadingViewHolder extends RecyclerView.ViewHolder {
LoadingViewHolder(@NonNull View itemView) {
super(itemView);
//progressBar = itemView.findViewById(R.id.progressBar);
}
}


private void showLoadingView() {
//ProgressBar would be displayed

}

private void populateItemRows(final ItemViewHolder holder, final int position) {

// ***** Codes ///
}

private void populateHorizontalItems(final HorizontalItemViewHolder holder, final int position){

// ***** Codes ///

}

private void populateNativeAdModule(AdsViewHolder nativeAdHolder,
final int position ){

// ***** ADS Codes ///

}

}
}

最佳答案

DiffUtil 不会对您的 View 感兴趣,只有模型来自适配器。看来您想在 postList 上进行比较包含不同类型的类型(项目、广告等)

您可以使用 DiffUtil.ItemCallback<Object>正如你在 getItemViewType 中所做的那样对于 Object类型。

关于android - 如何将 DiffUtil 与多种 View 类型一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61719051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com