Android two way view - Horizontal Recyclerview inside Vertical Recyclerview

前端 未结 1 862
鱼传尺愫
鱼传尺愫 2021-01-15 10:24

I want to create a dynamic loading view that contains horizontal recyclerview inside vertical recyclerview and vertical recyclerview should be inside Scrollview as the main

相关标签:
1条回答
  • 2021-01-15 11:05

    The best way is to use the RendererRecyclerViewAdapter

    Step 1: add ViewModel interface to your simple item

    public class SimpleItem implements ViewModel {
        /* your getters */
    }
    

    Step 2: Create ViewBinder for your simple item

    private ViewRenderer getSimpleViewBinder() {
        return new ViewBinder<>(
            R.layout.simple_item, /* your item layout */
            SimpleItem.class, /* your model class */
            (model, finder, payloads) -> {
                /* your binding */
                finder.setText(R.id.textView, model.getText());
        });
    }
    

    Step 3: Extend DefaultCompositeViewModel and add an uniq ID of your horizontal item

    public class HorizontalViewModel extends DefaultCompositeViewModel {
    
        private int mID;
    
        public HorizontalViewModel(int ID, List<? extends ViewModel> items) {
            super(items);
            mID = ID;
        }
    
        public int getID() {
            return mID;
        }
    }
    

    It is your Horizontal item that contains all simple items inside.

    Step 4: Create HorizontalViewBinder

    public class HorizontalViewBinder extends CompositeViewBinder<HorizontalViewModel> {
    
        public HorizontalViewBinder(int layoutID,
                                    int recyclerViewID,
                                    Class<HorizontalViewModel> type) {
            super(layoutID, recyclerViewID, type);
        }
    
        @Override
        public void bindView(HorizontalViewModel model, CompositeViewHolder holder) {
            super.bindView(model, holder);
            holder.getRecyclerView().addOnScrollListener(new EndlessScrollListener() {
                @Override
                public void onLoadMore(int page, int totalItemsCount) {
                    holder.getAdapter().showLoadMore();
                    /* sendLoadMoreRequest(model.getID()); */
                }
            });
        }
    
        @NonNull
        @Override
        public CompositeViewHolder createViewHolder(ViewGroup parent) {
            final CompositeViewHolder viewHolder = super.createViewHolder(parent);
            viewHolder.getAdapter().registerRenderer(new LoadMoreViewBinder(R.layout.load_more)); 
            /* don't forget to create load more layout */
            return viewHolder;
        }
    }
    

    Step 5: Create a getter for your HorizontalViewBinder

    private ViewRenderer getHorizontalViewBinder() {
        return new HorizontalViewBinder<>(
            R.layout.horizontal_layout, /* your horizontal layout that contains RecyclerView */
            R.id.recycler_view, /* an ID of RecyclerView */
            HorizontalViewModel.class /* your created class, see Step 3 */
        );
    }
    

    Step 6: Initialize in your Fragment/Activity the main RendererRecyclerViewAdapter

    RendererRecyclerViewAdapter mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
    mRecyclerViewAdapter.registerRenderer(new Horizontal().registerRenderer(getHorizontalViewBinder().registerRenderer(getSimpleViewBinder())); /* register HorizontalViewBinder and SimpleViewBinder */
    mRecyclerViewAdapter.setItems(getYourList());
    

    Step 7: Add LoadMoreListener to main RecyclerView

    mRecyclerView.addOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            mRecyclerViewAdapter.showLoadMore();
            /* send request to a server */
        }
    });
    

    Step 8: Create specials ViewBinders for other your items: AdViewBinder, SomeAnotherViewBinder and register them in your adapter. See the Step 1 and the Step 2.


    FYI: EndlessScrollListener - it is your implementation of LoadMoreListener

    Enjoy! :)

    If you need more details please see wiki-pages: Simple Items, Composite Items, Load More Indicator.

    0 讨论(0)
提交回复
热议问题