RecyclerView多布局实现


一、android 原生方法

Stpe1:adapter需要给每一个数据设置itemType根据布局界面设置等同的Type

private final intBANNER_VIEW_TYPE = 0;//轮播图
private final int CHANNEL_VIEW_TYPE =1;//频道
private final int GIRL_VIEW_TYPE =2;//表格布局
private final int NORMAL_VIEW_TYPE =3;//正常布局

 

 

Step2:根据RecyclerView.AdaptergetItemViewType(int position)方法来区分不同的类型,返回不同的int型的值在onCreateViewHolder方法中用viewType来创建不同的ViewHolder

if(position == 0) {//0个位置是轮播图
    returnBANNER_VIEW_TYPE;
} else if(position == 1) {//第一个是频道布局
    returnCHANNEL_VIEW_TYPE;
} else if(position == 2) {//2个位置是表格
    returnGIRL_VIEW_TYPE;
} else{//其他位置返回正常的布局
    returnNORMAL_VIEW_TYPE;
}

 

Stpe3:创建ViewHolder,根据getItemViewType方法里面返回的几种类型来创建

 

 

@Override
public RecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {
    View view;
    if (viewType ==BANNER_VIEW_TYPE) {//轮播图
        view = getView(R.layout.item_banner);
        BannerHolder bannerHolder = new BannerHolder(view);
        return bannerHolder;
    }else if (viewType ==CHANNEL_VIEW_TYPE) {//频道的type
        view = getView(R.layout.item_channel);
        return new ChannelHolder(view);
    }else if (viewType ==GIRL_VIEW_TYPE) {//表格布局
        view = getView(R.layout.item_girl);
        return new GirlHolder(view);
    }else {//正常
        view = getView(R.layout.item_normal);
        return newNormalHolder(view);
    }
}

 

Stpe4: 为不同的布局创建不同的ViewHolder

public static classNormalHolder extends RecyclerView.ViewHolder { }

 

Stpe5: 绑定数据在

onBindViewHolder(RecyclerView.ViewHolder holder, final int position)

方法中根据holder来绑定视图

 

最后在Activity中使用Adapter就可以实现多布局。

 

二、使用BRVAH完成多布局 (http://www.jianshu.com/p/b343fcff51b0)

Step1:

  compile'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'

 

Stpe2:

创建MulitipeItem实体类实现MultiItemEntity在设置数据的时候,需要给每一个数据设置itemType


    public static final intTEXT =1;      
    public static final intIMG =2;

    public static final intIMG_TEXT =3;
    private int itemType;
    public MultipleItem(intitemType) {
        this.itemType= itemType;
    }
    @Override
    public intgetItemType() {
        returnitemType;
    }

 

 

 

Stpe3:

创建MultipleItemQuickAdapter继承BaseMultiItemQuickAdapter在构造里面addItemType绑定type和layout的关系

publicMultipleItemQuickAdapter(List data) {
    super(data);
    addItemType(MultipleItem.TEXT,R.layout.text_view);
    addItemType(MultipleItem.IMG,R.layout.image_view);
}

@Override
protected void convert(BaseViewHolder helper,MultipleItem item) {
    switch(helper.getItemViewType()) {
        caseMultipleItem.TEXT:
            helper.setImageUrl(R.id.tv,item.getContent());
            break;
        case MultipleItem.IMG:
            helper.setImageUrl(R.id.iv,item.getContent());
            break;
    }
}

 

使用:

finalList data = DataServer.getMultipleItemData();
final MultipleItemQuickAdapter multipleItemAdapter =new MultipleItemQuickAdapter(

this,data);
final GridLayoutManager manager =new GridLayoutManager(this,4);
recyclerView.setLayoutManager(manager);
multipleItemAdapter.setSpanSizeLookup(newBaseQuickAdapter.SpanSizeLookup() {
    @Override
    public intgetSpanSize(GridLayoutManager gridLayoutManager, intposition) {
        returndata.get(position).getSpanSize();
    }
});
recyclerView.setAdapter(multipleItemAdapter);

 

由于第二种方法会跟实体类耦合,可以通过代理的方式实现

 

三、BaseQuickAdapter设置代理

 

 Step1setMultiTypeDelegate(),并重写getItemType()方法需要说明的Entity并不需要实现任何接口 ,只需要能够判断出该实体对应的是哪个布局类型即可

 Step2getMultiTypeDelegate().registerItemType() 设置每种type对应的布局

 Step3重写convert()的方法

 

classMultiDelegateAdapterextends BaseQuickAdapter<MultipleItem,BaseViewHolder> {

    publicMultiDelegateAdapter() {
        super(null);
        //Step.1
        setMultiTypeDelegate(newMultiTypeDelegate() {
            @Override
            protected intgetItemType(MultipleItem item) {
                //infer the type by entity
                returnitem.itemType

;
            }
        });
        //Step.2
        getMultiTypeDelegate()
               .registerItemType(MultipleItem .TEXT,R.layout.item_text_view)
               .registerItemType(MultipleItem .IMG,R.layout.item_image_view)

      .registerItemType(MultipleItem .IMG_TEXT,R.layout.item_img_text_view);


    }

    @Override
    protected voidconvert(BaseViewHolder helper,MultipleItem item) {
        //Step.3
        switch(helper.getItemViewType()) {
            caseMultipleItem .TEXT:
                // do something
                break;
            case MultipleItem .IMG:
                // do something
                break;

   caseMultipleItem .IMG_TEXT:
                // do something
                break;
        }
    }
}

你可能感兴趣的:(RecyclerView多布局实现)