RecyclerView不同Cardlayouts [英] RecyclerView with different Cardlayouts

查看:246
本文介绍了RecyclerView不同Cardlayouts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的

目前,我正在玩 RecyclerView CardView 的。现在我写了一个 RecyclerView.Adapter 上,我可以显示相同CardView多次与不同的内容 - 模拟到的ListView BaseAdapter

At the moment I'm playing around with RecyclerView and CardView's. For now I wrote a RecyclerView.Adapter on which I can display the same CardView multiple times with different content - analog to the ListView with a BaseAdapter.

现在我想写一个 RecyclerView 不同的谷歌风格的 CardView-布局的(现在)。我已经搜查了教程,但并没有发现任何关于该主题的有用的。是否有人知道,如何需要实现?需要做什么来实现呢?

Now I'd like to write a RecyclerView with different CardView-Layout's (in style of Google Now). I've already searched for tutorials but didn't found anything useful about that topic. Does someone know, how this needs to be implemented? What needs to be done to realize that?

推荐答案

要实现你想要的,你需要重写<一个href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType%28int%29">getItemViewType(position)您RecyclerView.Adapter。在那里你会返回一个int告诉你什么样的看法将被用来重新present这个位置。

To achieve what you want you need to override getItemViewType(position) on your RecyclerView.Adapter. Where you'll return an int telling you what kind of view will be used to represent this position.

接下来,您将创建<不同ViewHolders href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#createViewHolder%28android.view.ViewGroup,%20int%29"> createViewHolder(父母,viewType),这会留在你的情况下,引用的每个不同CardLayout。

Next you will create different ViewHolders on createViewHolder (parent,viewType) which will keep the references to each distinct CardLayout in your case.

然后在<一个href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#bindViewHolder%28VH,%20int%29">bindViewHolder(holder,位置)您可以创建一个switch语句,或者其他情况下要经过你的可能视图列表,并用数据填充它们。

Then on bindViewHolder(holder, position) you can create a switch statement or if else cases to go through your list of possible views and fill them with data.

下面的示例code:

public GeneralViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    GeneralViewHolder holder;
    View v;
    Context context = viewGroup.getContext();

    if (viewType == FIRST_TYPE) {
        v = LayoutInflater.from(context)
                .inflate(R.layout.first_card, viewGroup, false);

        holder = new FirstTypeViewHolder(v); //Of type GeneralViewHolder
    } else {
        v = LayoutInflater.from(context)
                .inflate(R.layout.second_card, viewGroup, false);
        holder = new SecondTypeViewHolder(v);
    }

    return holder;
}

public void onBindViewHolder(GeneralViewHolder viewHolder, int i) {
    if(getItemViewType(i)==FIRST_TYPE) {
        FirstTypeViewHolder holder1 = (FirstTypeViewHolder)viewHolder;
    } else {
        SecondTypeViewHolder holder1 = (SecondTypeViewHolder)viewHolder;
    }
}

public int getItemViewType (int position) {
    //Some logic to know which type will come next;
    return Math.random()<0.5 ? FIRST_TYPE : SECOND_TYPE;
}

这篇关于RecyclerView不同Cardlayouts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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