4

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

所以我的问题是:DiffUtil 是否支持多种视图类型,是否有一个很好的 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 /// 

    }

}
    }
4

1 回答 1

0

DiffUtil 不会对您的视图感兴趣,只有模型来自适配器。似乎您想要对postList包含不同类型(项目、广告等)的差异进行区分

您可以DiffUtil.ItemCallback<Object>像在getItemViewTypefor Objecttype 中一样使用。

于 2020-05-10T22:19:00.480 回答