Wes
Wes

Reputation: 15

Android layout advice (RecyclerView, ViewPager and CardView)

I need some advice to implement a design for my android application.

I'm using a RecyclerView to display a list of CardView. No problem with that. But, I need a top section with a ViewPager that display other CardView with horizontal swipping:

See https://i.sstatic.net/akg2i.png

I've read that Viewpager is not very built to be in a RecyclerView, so maybe a better way is to create a ScrollView with a ViewPager inside then a RecyclerView below and disable the scrolling of the RecyclerView ?

Also, can I use ViewPager view along with CardView ?

EDIT:

Ok problem resolved: I tried with this solution from fmt.Println.MKO's comment

you can do this, define 2 Items types, one for your usual cards, and one for your 2.RecyclerView, with int getItemViewType(int position), you return the specific type. and in onCreateViewHolder(ViewGroup parent, int viewType) you create a viewholder depending on type, either just your card or the 2. RecyclerView

In my PostCardAdapter used by both RecyclerView:

   @Override
        public int getItemViewType(int position) {
            RecyclerItem p = items.get(position);
            if (p.isHeader){
                return TYPE_HEADER;
            }
            return TYPE_POST;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        switch (type) {
            case TYPE_HEADER:
                RecyclerView rv = (RecyclerView) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_header, viewGroup, false);
                rv.setHasFixedSize(true);
                PostsCardAdapter adapter2 = new PostsCardAdapter(this.headerPosts);
                rv.setAdapter(adapter2);
                LinearLayoutManager layoutManager = new LinearLayoutManager(viewGroup.getContext(), LinearLayoutManager.HORIZONTAL, false);
                rv.setLayoutManager(layoutManager);
                return new HeaderViewHolder(rv);

            default:
                View postView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.post, viewGroup, false);
                return new PostsViewHolder(postView);
        }

    }

Nothing specific to do in the onBindViewHolder for TYPE_HEADER

Just need to apply the scrollTo method to act like a viewPager and find a solution to display a component with circles indicating wich item is showed.

Upvotes: 1

Views: 4760

Answers (1)

fmt.Println.MKO
fmt.Println.MKO

Reputation: 2060

as I understood you have a horizontal scrolling view, and vertical scrolling view below.

todo this, you have to add two RecyclerView's in your activity / fragment layout.xml

on your first RecyclerView you add a LinearLayoutManager ,with a horizontal orientation.

you don't need a viewpager for that.

Upvotes: 1

Related Questions