New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多布局代理, new MultiTypeDelegate #968

Closed
tysheng opened this Issue Apr 6, 2017 · 27 comments

Comments

Projects
None yet
@tysheng
Contributor

tysheng commented Apr 6, 2017

更便捷的多布局, 为 BaseQuickAdapter 设置代理

 public class MultiDelegateAdapter extends BaseQuickAdapter<Entity, BaseViewHolder> {

        public MultiDelegateAdapter() {
            super(null);
            //Step.1
            setMultiTypeDelegate(new MultiTypeDelegate<Entity>() {
                @Override
                protected int getItemType(Entity entity) {
                    //根据你的实体类来判断布局类型
                    return entity.type;
                }
            });
            //Step.2
            getMultiTypeDelegate()
                    .registerItemType(Entity.TEXT, R.layout.item_text_view)
                    .registerItemType(Entity.IMG, R.layout.item_image_view);
        }

        @Override
        protected void convert(BaseViewHolder helper, Entity entity) {
            //Step.3
            switch (helper.getItemViewType()) {
                case Entity.TEXT:
                    // do something
                    break;
                case Entity.IMG:
                    // do something
                    break;
            }
        }
    }

多布局代理很简单,在任何继承与BaseQuickAdapter的 adapter 上都可以实现,分为三步,如下:

Step.1

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

Step.2

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

Step.3

  • 分 type 进行 convert() 操作

OK, 多布局就可以顺利的运行了

@tysheng

This comment has been minimized.

Contributor

tysheng commented Apr 6, 2017

Make it easier for multi type situation, set a delegate for BaseQuickAdapter

 public class MultiDelegateAdapter extends BaseQuickAdapter<Entity, BaseViewHolder> {

        public MultiDelegateAdapter() {
            super(null);
            //Step.1
            setMultiTypeDelegate(new MultiTypeDelegate<Entity>() {
                @Override
                protected int getItemType(Entity entity) {
                    //infer the type by entity
                    return entity.type;
                }
            });
            //Step.2
            getMultiTypeDelegate()
                    .registerItemType(Entity.TEXT, R.layout.item_text_view)
                    .registerItemType(Entity.IMG, R.layout.item_image_view);
        }

        @Override
        protected void convert(BaseViewHolder helper, Entity entity) {
            //Step.3
            switch (helper.getItemViewType()) {
                case Entity.TEXT:
                    // do something
                    break;
                case Entity.IMG:
                    // do something
                    break;
            }
        }
    }

First, make you adapter extends BaseQuickAdapter , then achieve the following steps :

Step.1

  • setMultiTypeDelegate(), and override getItemType()
    Notice that there is no need for Entity to implement any interface , only for inferring which layout type is.

Step.2

  • getMultiTypeDelegate().registerItemType(), set layout id for every type.

Step.3

  • do convert() job for every type.

That's all.

@CymChad

This comment has been minimized.

Owner

CymChad commented Apr 6, 2017

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

@tysheng tysheng closed this Apr 6, 2017

@DaiFalin

This comment has been minimized.

DaiFalin commented Jun 14, 2017

return entity.type; 请问这里的entity.type是返回的什么呢?

@tysheng

This comment has been minimized.

Contributor

tysheng commented Jun 14, 2017

你自己写的,区分type

@DaiFalin

This comment has been minimized.

DaiFalin commented Jun 14, 2017

我的实体类使用的Retrifot添加Gson转换器直接存入实体类了,可以说下采用代理的话,实体类里面怎么写呢,小白一枚。我现在安装这样写出来,RecyclerView空白。

@tysheng

This comment has been minimized.

Contributor

tysheng commented Jun 14, 2017

@z2700589

This comment has been minimized.

z2700589 commented Jun 23, 2017

有四种布局,但是只有两个bean,外面套一个类,应该怎么套呢,小白求大神指导。。。。

@tysheng

This comment has been minimized.

Contributor

tysheng commented Jun 23, 2017

@z2700589 写代码的思维灵活一点,都是很简单的
class AllBean{
int type;
BeanA b1;
BeanB b2;
}

@chaoDream

This comment has been minimized.

chaoDream commented Jul 11, 2017

recyclerview 嵌套recyclerview 报android.content.res.Resources$NotFoundException: Resource ID #0xfffffe6c
at android.content.res.Resources.getValue(Resources.java:1048) 这种错误,是由于什么引起的,求解决!

@FrankKwok

This comment has been minimized.

Contributor

FrankKwok commented Jul 11, 2017

@chaoDream 重新开个issue提问

@zouzhenglu

This comment has been minimized.

zouzhenglu commented Jul 12, 2017

没感觉这个委托的写法比原来的好啊

@moyokoo

This comment has been minimized.

moyokoo commented Jul 13, 2017

@zouzhenglu 解决了和实体类耦合的问题 这样就不需要强制去实现MultiItemEntity接口

@zouzhenglu

This comment has been minimized.

zouzhenglu commented Jul 13, 2017

那其实重写他的getItemType就可以了,这样啊还清晰点

@AaronYang23

This comment has been minimized.

AaronYang23 commented Aug 1, 2017

有一个疑问:这种代理情况是在哪里设置list数据集? 构造方法没有参数传入

@luao419

This comment has been minimized.

luao419 commented Aug 9, 2017

请问想根据行号设置布局可以做得到吗

@Lee-Jian

This comment has been minimized.

Lee-Jian commented Aug 14, 2017

在哪里传数据

@AaronYang23

This comment has been minimized.

AaronYang23 commented Aug 14, 2017

构造方法内调用setnewdata 或者 修改构造方法参数不传null,传单参数你的list

@SSSSunrise

This comment has been minimized.

SSSSunrise commented Sep 26, 2017

适配器进行 addFooterView(view) view不能有listview、Gridview、Recyclerview吗?我这边添加了不显示数据。

@z2700589

This comment has been minimized.

z2700589 commented Nov 10, 2017

@austenyad

This comment has been minimized.

austenyad commented Dec 13, 2017

这个多布局可以求一个简单的demo吗?

@hong-tang

This comment has been minimized.

hong-tang commented Jan 29, 2018

android.content.res.Resources$NotFoundException: Resource ID #0xfffffe6c
at android.content.res.Resources.getValue(Resources.java:1351)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
at android.content.res.Resources.getLayout(Resources.java:1165)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.chad.library.adapter.base.BaseQuickAdapter.getItemView(BaseQuickAdapter.java:1478)
at com.chad.library.adapter.base.BaseQuickAdapter.createBaseViewHolder(BaseQuickAdapter.java:989)
at com.chad.library.adapter.base.BaseQuickAdapter.onCreateDefViewHolder(BaseQuickAdapter.java:985)
at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:762)
at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:68)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:610)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at com.scwang.smartrefresh.layout.impl.RefreshContentWrapper.layout(RefreshContentWrapper.java:206)
at com.scwang.smartrefresh.layout.SmartRefreshLayout.onLayout(SmartRefreshLayout.java:690)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java

会出现这种问题,怎么解决?

@douya40

This comment has been minimized.

douya40 commented Mar 11, 2018

我四种布局四个接口,好像用这个实现不了

@zach-z

This comment has been minimized.

zach-z commented Apr 11, 2018

你好 我使用多布局时没有回调MultiTypeDelegate中的getItemType方法 数据是有的

@SuperKe

This comment has been minimized.

SuperKe commented May 11, 2018

android.content.res.Resources$NotFoundException: Resource ID #0xfffffe6c 是什么原因啊,老铁??

@wushangjun

This comment has been minimized.

wushangjun commented May 28, 2018

@SuperKe 布局类型找不到。检查一下代码

@aloe20

This comment has been minimized.

aloe20 commented Jul 27, 2018

在ItemProvider中如何处理子View的点击事件呢

@duoluodesonger

This comment has been minimized.

duoluodesonger commented Nov 6, 2018

可以根据position来返回不同的布局类型吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment