Android的布局建议(RecyclerView,ViewPager和CardView) [英] Android layout advice (RecyclerView, ViewPager and CardView)

查看:1102
本文介绍了Android的布局建议(RecyclerView,ViewPager和CardView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些建议来实现我的Andr​​oid应用程序的设计。

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

我使用的是 RecyclerView 显示 CardView 的列表。没问题这一点。但是,我需要一个顶部一个 ViewPager 可显示其他 CardView 与水平swipping:

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:

请参阅 http://i.stack.imgur.com/akg2i.png

我读过 Viewpager 不是很打造成为一个 RecyclerView ,也许更好的方法是创建一个滚动型 ViewPager 里面那么 RecyclerView 下面和禁用的滚动的 RecyclerView

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 ?

另外,我可以使用 ViewPager 视图与 CardView

Also, can I use ViewPager view along with CardView ?

编辑:

确定问题解决:我试图从fmt.Println.MKO的评论

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

您可以做到这一点,定义2个项目类型,一种为你平时的卡,以及一个用于您的2.RecyclerView,使用int getItemViewType(INT位置),返回的具体类型。在onCreateViewHolder(ViewGroup中的父母,INT viewType)您根据类型创建viewholder,要么只是你的卡或2 RecyclerView

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

在我的双方使用 PostCardAdapter 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);
        }

    }

具体在没有做 onBindViewHolder TYPE_HEADER

就需要应用scrollTo方法来像一个viewPager并找到解决的办法,以显示与指示至极项界的显示组件。

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.

推荐答案

我了解你有一个横向滚动视图,以下垂直滚动视图。

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

TODO这一点,你必须增加两个RecyclerView在您的活动/片断layout.xml

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

你的第一个RecyclerView您添加LinearLayoutManager,具有水平方向。

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

您不需要为一个viewpager。

you don't need a viewpager for that.

这篇关于Android的布局建议(RecyclerView,ViewPager和CardView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆