How to create scrollable page of carousels in Android?

后端 未结 5 955
再見小時候
再見小時候 2020-12-12 15:36

I am attempting to build a UI for my Android app which contains a vertically scrollable page of horizontally scrollable carousels (something like what the Netflix app does).

5条回答
  •  再見小時候
    2020-12-12 16:11

    I would suggest the Recycler view.

    You can create horizontal and vertical list or gridviews. In my opinion the viewpager can become complicated at times.

    I'm working on video on demand application and this saved me.

    In your case it will be easy to set up. I will give you some code.

    You will need the following:
    XML View - Where the recycle layout is declared.
    Adapter - You will need a view to populate the adapter and fill the recycleview.

    Creating the view

    
    

    Declare this where you want the carousel to display.

    Next you want to create the adapter:

    public class HorizontalCarouselItemAdapter extends RecyclerView.Adapter {
    
        List items;
        int itemLayout;
    
        public HorizontalCarouselItemAdapter(Context context, int itemLayout, List items) {
            this.context = context;
            this.itemLayout = itemLayout;
            this.items = items;
    
        }
    
        @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
            return new ViewHolder(v);
        }
    
    
        @Override public void onBindViewHolder(final ViewHolder holder, final int position) {
    
            this.holders = holder;
            final GenericAsset itemAdapter = items.get(position);
            holder.itemImage.setDrawable //manipulate variables here
    
    
        }
    
    
        @Override public int getItemCount() {
            return items.size();
        }
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView itemImage;
    
    
    
            public ViewHolder(View itemView) {
                super(itemView);
                itemImage = (ImageView) itemView.findViewById(R.id.carousel_cell_holder_image);
    
    
            }
        }
    

    This is where you feed the data to the adapter to populate each carousel item.
    Finally declare it and call the adapter:

    recyclerView = (RecyclerView)findViewById(R.id.recycle_view);
    ListLayoutManager manager = new ListLayoutManager(getApplication(), ListLayoutManager.Orientation.HORIZONTAL);
    recyclerView.setLayoutManager(manager);
    
    CustomAdpater adapter = new CustomAdapter(getApplication(), data);
    recyclerView.setAdapter(adapter);
    

    You can create a listview with recycle views to achieve what you want.
    This class is great for smooth scrolling and memory optimisation.

    This is the link for it:

    https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

    I hope this helps you.

提交回复
热议问题