How to use the same RecyclerView Adapter for different layouts

后端 未结 4 2089
逝去的感伤
逝去的感伤 2021-02-09 06:48

I am developing an application which heavily relies on the usage of RecyclerView.

I really need to know how to use the same RecyclerView for different item layouts. An e

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-09 07:32

    its too late but, might be helpful for someone needy developer Your adapter should look like this, also you can add header and footer using this sample

    public class SampleAdapter extends RecyclerView.Adapter {
    
    // Declaring Variable to Understand which View is being worked on
    // IF the view under inflation and population is header or Item
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_FOOTER = 2;
    
    private Activity mContext;
    private ArrayList _mItems;
    private int mLayout;
    private String mProductHeadingTitle="Heading";
    private String mProductHeadingSubTitle="SubHeading";
    private String loadingText="LOADING";
    private int visibility= View.VISIBLE;
    
    public interface SampleAdapterInterface {
         void itemClicked(int position);
    }
    
    SampleAdapterInterface mCallBack;
    
    public SampleAdapter(Activity context, ArrayList items, int item_layout) {
        if (_mItems != null) {
            _mItems.clear();
        }
        this.mContext = context;
        this._mItems = items;
        this.mLayout = item_layout;
        mCallBack = (SampleAdapterInterface) context;
    }
    
    @Override
    public int getItemCount() {
        return _mItems.size()+2; // +2 for header and footer
    }
    
    @Override
    public int getItemViewType(int position) {
    
        if (position==0)
            return TYPE_HEADER;
        else if(position==(_mItems.size()+1))
            return TYPE_FOOTER;
    
        return TYPE_ITEM;
    }
    
    public void setHeaderData(String title,String subTitle)
    {
        this.mProductHeadingTitle=title;
        this.mProductHeadingSubTitle=subTitle;
    
        Log.d("LOG", "ProductHeadingTitle: " + mProductHeadingTitle);
        Log.d("LOG", "ProductHeadingSubTitle: " + mProductHeadingSubTitle);
    
        notifyDataSetChanged();
    }
    
    public void setFooterData(String loadingText,int visibility)
    {
        this.loadingText=loadingText;
        this.visibility=visibility;
    
        Log.d("LOG", "LoadingText: " + loadingText);
        Log.d("LOG", "Visibility: " + visibility);
    
        notifyDataSetChanged();
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
    
        if (viewType == TYPE_HEADER)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout,parent,false);
    
            ViewHolder vhHeader = new ViewHolder(view,viewType);
    
            return vhHeader;
        }
        else if (viewType == TYPE_ITEM)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(mLayout,parent,false);
    
            //Creating ViewHolder and passing the object of type view
            ViewHolder vhItem = new ViewHolder(view,viewType);
    
            return vhItem;
        }
        else if (viewType == TYPE_FOOTER)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_lyout,parent,false);
    
            ViewHolder vhFooter = new ViewHolder(view,viewType);
    
            return vhFooter;
        }
        return null;
    }
    
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int pos) {
    
    
        if(viewHolder.Holderid ==0)
        {
            // header view
            Log.d("LOG", "in header binder");
            viewHolder.mProductCatalogTitle.setText(mProductHeadingTitle);
            viewHolder.mProductCatalogSubTitle.setText(mProductHeadingSubTitle);
        }
        else if(viewHolder.Holderid==1)
        {
            final int position=pos-1; // -1 to substract header number
            // your code
        }
        else if(viewHolder.Holderid==2)
        {
            // footer
            Log.d("LOG", "in footer binder");
            viewHolder.mProgressBar.setVisibility(visibility);
            viewHolder.mLoading.setText(loadingText);
        }
    
    
    }
    
    class ViewHolder extends RecyclerView.ViewHolder {
    
        int Holderid;
    
        // header
        TextView mProductCatalogTitle;
        TextView mProductCatalogSubTitle;
    
        //list
        // item type variable declaration
    
        // footer
        ProgressBar mProgressBar;
        TextView mLoading;
    
        public ViewHolder(View itemView, int viewType) {
            super(itemView);
            // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
            if(viewType == TYPE_HEADER)
            {
                Holderid = 0;
                mProductCatalogTitle = (TextView) itemView.findViewById(R.id.tv_title);
                mProductCatalogSubTitle = (TextView) itemView.findViewById(R.id.tv_subtitle);
            }
            else if(viewType == TYPE_ITEM)
            {
                Holderid = 1;
                itemView.setClickable(true);
                itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCallBack.itemClicked(getAdapterPosition()-1);
                    }
                });
                // initialize the view holder
            }
    
            else if(viewType == TYPE_FOOTER)
            {
                Holderid = 2;
                mLoading = (TextView) itemView.findViewById(R.id.tv_loading);
                mProgressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
            }
        }
    }
    

    in your activity

    // Adding to adapter as gridview when grid button clicked
    private void setProductGridAdapter() {
        mListViewTab.setVisibility(View.VISIBLE);
        mGridViewTab.setVisibility(View.INVISIBLE);
    
        mSampleAdapter = new SampleAdapter(YourActicity.this,
                yourlist,R.layout.item_product_grid);
        mRecyclerView.setAdapter(mSampleAdapter);
    
        mRecyclerView.setHasFixedSize(true);
    
        final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),
                2,                              //number of columns
                LinearLayoutManager.VERTICAL,   // orientation
                false);                         //reverse layout
        mRecyclerView.setLayoutManager(layoutManager);
    
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
    
                // to show header and footer in full row
                if(position==0 || position==(yourlist.size()+1))
                    return layoutManager.getSpanCount();
                else
                    return 1;
            }
        });
    
        mRecyclerView.scrollToPosition(AppController.currentPosition);
    
        mSampleAdapter.notifyDataSetChanged();
    
        // Scroll listener for RecyclerView to call load more products
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    
                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
    
                int lastInScreen = firstVisibleItem + visibleItemCount;
                if ((lastInScreen >= totalItemCount) && !isLoading) {
                    //last item
                    // do something after last item like load more code or 
                    // show No more items string
                    mSampleAdapter.setFooterData("NO More Items",View.INVISIBLE);
                }
                AppController.currentPosition = firstVisibleItem;
            }
        });
    
    }
    
    // Adding to adapter as listview when list button clicked
    private void setProductListAdapter() {
        mListViewTab.setVisibility(View.INVISIBLE);
        mGridViewTab.setVisibility(View.VISIBLE);
    
        mSampleAdapter = new SampleAdapter(YourActicity.this, yourlist,R.layout.item_product_list);
        mRecyclerView.setAdapter(mSampleAdapter);
    
        mRecyclerView.setHasFixedSize(true);
    
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL,   // orientation
                false);                         //reverse layout
        mRecyclerView.setLayoutManager(layoutManager);
    
        mRecyclerView.scrollToPosition(AppController.currentPosition);
        mSampleAdapter.notifyDataSetChanged();
    
        // Scroll listener for RecyclerView to call load more products
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    
                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
    
                int lastInScreen = firstVisibleItem + visibleItemCount;
                if ((lastInScreen >= totalItemCount) && !isLoading) {
                    //last item
                    // do something after last item like load more code or 
                    // show No more items string
                    mSampleAdapter.setFooterData("NO MOre Items",View.INVISIBLE);
                }
                AppController.currentPosition = firstVisibleItem;
            }
        });
    }
    

    and your activity must implement SampleAdapterInterface in activity to get callback from adapter. Call those methods when toggling button from grid to list and vice versa

提交回复
热议问题