根据数据的内容创建RecyclerView项目布局 [英] Create RecyclerView item layout based on content of data

查看:239
本文介绍了根据数据的内容创建RecyclerView项目布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的数据对象的回收来看,这些对象会有所不同,例如:

1的OBJ
  - 标题字符串
  - 字符串说明
  - 图像

2的OBJ
  - 字符串说明

3的OBJ
 - 图片
  - 字符串链接

的OBJ 4
  - 字符串说明
  - 视频

所以我需要动态地创建我的项目布局,以适应每个数据对象。我不想创建一个项目的布局与所有可能的意见和组件和显示和隐藏,因为我觉得这是不好的做法。

所以我的问题是,我需要用才能在onBindViewHolder的位置来构建我在ViewHolder客舱布局从列表访问的对象。

我试图用我的onBindViewHolder类调用,并在其中我添加自定义视图到数组对象传递方法,同时设置里面的内容,但这并不工作。

有谁知道如何可以做到这一点?

问候


解决方案

首先你正确实施视图类型喜欢这里解释<一个href=\"http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type\">How与多个视图类型创建RecyclerView?
其实,你的问题是重复的,但我会只是写一些额外的东西组织,以帮助您处理较大的持有人数量。


  • 在这个答案我将使用奶油刀和毕加索libraris因为他们真棒: HTTP: //jakewharton.github.io/butterknife/ http://square.github.io/picasso /


  • 在您的项目持有,然后你把里面所有的观点持有者,下面是持有人的例子创建一个包>

  • 创建一个 AbstractHolder 公共无效bindData(的数据)这样的话你的适配器扩展RecyclerView.Adapter&LT; AbstractHolder&GT;


  • 创建人象下面这样:


例如 Holder1.java

 公共类Holder1扩展AbstractHolder {    //这是意见,这将用一个例子
    @Bind(R.id.text)TextView的文本;
    @Bind(R.id.image)ImageView的形象;    //构造正常:
    公共Holder1(查看ItemView控件){
        超(ItemView控件);
        ButterKnife.bind(这一点,ItemView控件); //初始化意见
    }    //从适配器调用此
    @覆盖公共无效bindData(的数据){
        text.setText(data.text);
        。Picasso.with(itemView.getContext())负载(data.url).into(图片);
    }    //在这里创建该持有人的实例,
    //这种方式,夹持器和它的相关联的布局的推移一起
    公共静态Holder1创建(父的ViewGroup){
        查看根= LayoutInflater.from(parent.getContext())膨胀(R.layout.holder1,父母,假的)。
        返回新Holder1(根);
    }}


  • 那么你的适配器code将是:

  @覆盖公共AbstractHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){
        开关(viewType){
            案例TYPE_HOLDER_1:返回Holder1.create(父);
            案例TYPE_HOLDER_2:返回Holder2.create(父);
            案例TYPE_HOLDER_3:返回Holder3.create(父);
            ...进行所有类型
        }
 }@覆盖公共无效onBindViewHolder(AbstractHolder持有人,INT位置){
    对象数据=的getItem(位置);
    holder.bindData(数据);
}
//的get类型是类似:
@覆盖
公众诠释getItemViewType(INT位置){
    对象数据=的getItem(位置);
    如果(...某些条件......)的返回TYPE_HOLDER_1;
    否则,如果(...其他条件......)的返回TYPE_HOLDER_2;
    否则,如果(...其他条件......)的返回TYPE_HOLDER_3;
    ...等...
}

的结论是:

通过这种方法,您适配器类只是一个集散地,为可能的类型和每种类型的知道如何创建自己以及如何处理它的数据。

这使你的code易于维护和很好的组织。

I'm creating a recycler view of data objects, each of these object will vary, eg.

Obj 1 - String Heading - String Desc - Image

Obj 2 - String Desc

Obj 3 - Image - String Link

Obj 4 - String Desc - Video

etc

So I need to create my item layout dynamically to suit each data object. I don't want to create an item layout with all the possible views and components and show and hide as I feel it's bad practice.

So my issue is I need to access an object from the list using the position in the onBindViewHolder in order to construct my layout in the ViewHolder class.

I tried using a method which I called in the onBindViewHolder class and passed in the object in which I added custom views to an array, setting the content inside at the same time but this didn't work.

Does anyone know how this can be done?

Regards

解决方案

first you proper implement view type like explained here How to create RecyclerView with multiple view type? actually, your question is duplicated of that, but I'll just write some extra organization stuff to help you deal with larger quantity of holders.

  • on this answer I'll be using ButterKnife and Picasso libraris because they're awesome: http://jakewharton.github.io/butterknife/ and http://square.github.io/picasso/

  • Create a package on your project holders and then all view holder you put inside there, below is an example of holder:

  • Create an AbstractHolder with public void bindData(Data data) so then your adapter extends RecyclerView.Adapter<AbstractHolder>:

  • Create holders like below:

example Holder1.java

public class Holder1 extends AbstractHolder {

    // that's an example of views this would use
    @Bind(R.id.text) TextView text;
    @Bind(R.id.image) ImageView image;

    // constructor as normal:
    public Holder1(View itemView){
        super(itemView);
        ButterKnife.bind(this, itemView); // init the views
    }

    // call this from the adapter
    @Override public void bindData(Data data){
        text.setText(data.text);
        Picasso.with(itemView.getContext()).load(data.url).into(image);        
    }

    // here you create an instance of this holder,
    // this way the holder and the layout it's associated with goes together
    public static Holder1 create(ViewGroup parent){
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder1, parent, false);
        return new Holder1(root);
    }

}

  • then your adapter code would be:

.

@Override public AbstractHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch(viewType){
            case TYPE_HOLDER_1: return Holder1.create(parent);
            case TYPE_HOLDER_2: return Holder2.create(parent);
            case TYPE_HOLDER_3: return Holder3.create(parent);
            ... carry on for all types   
        }
 }

@Override public void onBindViewHolder(AbstractHolder holder, int position) {
    Object data = getItem(position);
    holder.bindData(data);
}


// the get type would be similar to that:
@Override
public int getItemViewType(int position) {
    Object data = getItem(position);
    if( ... some condition...) return TYPE_HOLDER_1;
    else if( ... other condition...) return TYPE_HOLDER_2;
    else if( ... other condition...) return TYPE_HOLDER_3;
    ... etc ...
}

conclusion:

With this method your Adapter class is just a "distribution center" for the possible types and each type "knows" how to create itself and how to handle its data.

This makes your code easy to maintain and nicely organized.

这篇关于根据数据的内容创建RecyclerView项目布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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