yqritc / recyclerview-multipleviewtypesadapter Goto Github PK
View Code? Open in Web Editor NEWAndroid library defining adapter classes of RecyclerView to manage multiple view types
License: Apache License 2.0
Android library defining adapter classes of RecyclerView to manage multiple view types
License: Apache License 2.0
Hi, if you have 3 binders in your recyclerview like your example and i want to delete binder 2. How should i do this?
Thanks in advance!
Yilzer
All viewtype1 are in one order and type2 are in on order
thank you for your great multipleViewTypesAdapter.
but when i used it wanting to show a StaggeredGrid UI, i found something undesirably.
it can not show staggerGrid but the different children has the same height .
So how to implement StaggeredGrid with multipleViewTypesAdapter,thanks a lot.
as my code:
MainActivity.java
onCreate(Bundle savedInstanceState) {
...
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_main);
SampleEnumListAdapter adapter = new SampleEnumListAdapter();
recyclerView.setAdapter(adapter);
// recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 瀑布流
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL));
// 自定义分割线
// SpacesItemDecoration decoration = new SpacesItemDecoration(this,
// SpacesItemDecoration.VERTICAL_LIST);
// recyclerView.addItemDecoration(decoration);
// -----------//
List sampleData = getSampleData();
adapter.setSample2Data(sampleData);
...
}
and the different binder
Sample1Binder.java
@OverRide
public void bindViewHolder(ViewHolder holder, int position) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数
params.height = 50;// 把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);// 把params设置给item布局
Picasso.with(holder.mImageView.getContext())
.load(R.drawable.bird)
.into(holder.mImageView);
}
Simple2Binder.java
@OverRide
public void bindViewHolder(ViewHolder holder, int position) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数
params.height = 100;// 把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);// 把params设置给item布局
SampleData data = mDataSet.get(position);
holder.mTitleText.setText(data.mTitle);
holder.mContent.setText(data.mContent);
holder.mImageView.setImageResource(R.drawable.animal1);
}
Sample3Binder.java
public void bindViewHolder(ViewHolder holder, int position) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数
params.height = 150;// 把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);// 把params设置给item布局
holder.mImageView.setImageResource(R.drawable.animal3);
}
Does this library support two dynamic lists? For example if I have two different binders that each have a dynamic list, would I need to do anything differently to get it to work?
Currently I'm getting indexing issues when using ListBindAdapter.
What's the best way to do this? thanks in advance
I get this error when using clear() in Sample2Binder
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{4266fe38 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
Hi,
May I ask if the DataBinder has any capability of an unbindViewHolder callback?
I am using EnumMapBindAdapter to display userposts list .Here I am using 4 binders ,each binder has a list of size 3 .
I have problem with getEnumFromPosition() ,According to my assumptions number of executions of
getEnumFromPosition() method should be equal to number of binders (4) . But it is not happening .I have total 4 binders and I assume getEnumFromPosition()
method should execute 4 times only (0,1,2,3) ,but executions are being excedeed . My assumption may be wrong ,If I am wrong then how can
I manage 4 binders inside getEnumFromPosition() for different positions . If I put else section then it will return a single view when position exceeds .
Here is the code of SampleEnumMapAdapter.java
public class SampleEnumMapAdapter extends EnumMapBindAdapter<SampleEnumMapAdapter.SampleViewType> {
enum SampleViewType{
TEXT,IMAGE_AND_TEXT, AUDIO_AND_TEXT, VIDEO_AND_TEXT
}
public SampleEnumMapAdapter(){
putBinder(SampleViewType.TEXT, new TextBinder(this));
putBinder(SampleViewType.IMAGE_AND_TEXT, new ImageAndTextBinder(this));
putBinder(SampleViewType.AUDIO_AND_TEXT, new AudioAndTextBinder(this));
putBinder(SampleViewType.VIDEO_AND_TEXT, new VideoAndTextBinder(this));
}
public void setData(List<TextData> dataSetText ,List<ImageAndTextData> dataSetTextAndImage ,List<AudioAndTextData> dataSetTextAndAudio ,List<VideoAndTextData> dataSetTextAndVideo ) {
((TextBinder) getDataBinder(SampleViewType.TEXT)).addAll(dataSetText);
((ImageAndTextBinder) getDataBinder(SampleViewType.IMAGE_AND_TEXT)).addAll(dataSetTextAndImage);
((AudioAndTextBinder) getDataBinder(SampleViewType.AUDIO_AND_TEXT)).addAll(dataSetTextAndAudio);
((VideoAndTextBinder) getDataBinder(SampleViewType.VIDEO_AND_TEXT)).addAll(dataSetTextAndVideo);
}
@Override
public SampleViewType getEnumFromPosition(int position){
if (position == 0) {
return SampleViewType.TEXT;
} else if (position == 1) {
return SampleViewType.IMAGE_AND_TEXT;
} else if(position == 2){
return SampleViewType.AUDIO_AND_TEXT;
}else if(position == 3){
return SampleViewType.VIDEO_AND_TEXT;
}
return null;
}
@Override
public SampleViewType getEnumFromOrdinal(int ordinal) {
return SampleViewType.values()[ordinal];
}
}
//TYPE1
public void bindViewHolder(ViewHolder holder, int position) {
Log.w("bindViewHolder",position+" "+getPosition(this,position));
}
0 1
1 1
2 2
3 3
should be:
0 1
1 2
2 3
3 4
data example:
TYPE0
TYPE1
TYPE1
TYPE1
in getPosition
--binderPosition;
if(binderPosition <= 0) {
when positionOfBinder==0, after1 loop, -1<=0 so return 1 (correct)
when positionOfBinder==1, after1 loop, 0<=0 so return 1 (wrong, it should be 2)
i think binderPosition should not <0, so i fix it with
if(binderPosition-- <= 0) {
when positionOfBinder==0, after1 loop, 0<=0 so return 1 (correct)
when positionOfBinder==1, first loop 1<=0 is false , second loop, 0<=0 so return 2 (correct)
when flip
the surface very slow
有点卡顿
Hi,
I'm implementing a clever approach to notifyBinderDataSetChanged - so that calling this will automatically figure out if it should add/remove or update the items.
Would you be interested in a PR once I get this working or you see this as besides the scope of this lib ?
User case here would be that users can simply add/remove/update items in a binder and simply call notifyBinderDataSetChanged and not having to worry about what actions they took.
How do I listen to clicks on different viewholders?
First of all, thanks for this fantastic Library! I have some Problem with adding multiple different Binders (ViewTypes)
I´m using Retrofit, so i have to wait until i get my Response with my Data.
So this is my Response inside my Adapter:
@Override
public void onResponse(Call<ObjectModel> call, Response<ObjectModel> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
nodeDataObject = response.body();
blockModelList = nodeDataObject.blocksModel;
for(int i = 0;i < blockModelList.size(); ++i) {
switch (blockModelList.get(i).viewTypeId) {
case ApiConstants.PAGE_TITLE:
addBinder(new PageTitleHolderItem(this,(PageTitleModel)blockModelList.get(i)));
break;
case ApiConstants.PAGE_TEXT:
addBinder(new TextHolderItem(this,(TextBlockModel)blockModelList.get(i)));
break;
}
}
notifyDataSetChanged();
}
}
Both Databinder are added, but only inside my PageTitleHolderItem the newViewHolder
and the bindViewholder
are called.
Inside my TextHolderItem only the Constructor gets called and nothing more? I´m using ListBindAdapter
. Whats wrong?
What does getITemViewType
do in EnumMapBindAdapter
exactly? and what if I have one arraylist of different object types and not separate lists, how can I change view types based on object type?, if I use separate binders then i have to have separate lists, so I tried using one binder and override getItemViewType in it, but turned out I can't since it extends ViewHolder.
is it possible to add a new binder dynamically after already initializing EnumListBindAdapter
and using addAllBinder
in the constructor? I have a different row type that i want to add if a certain condition is met, and removed if not.
Every now and then I end up getting a class cast exception such as:
java.lang.ClassCastException: com.vivino.android.marketsection.binders.ComputationalTopListsBinder$ViewHolder cannot be cast to com.vivino.android.marketsection.binders.HighlightedOfferBinder$ViewHolder
Seems its picking the wrong binder at the wrong time and then things go south...
This happens only when I use my custom adapter with support for removeBinder - So not really your wrong doing..
Hey,
I am using setBinder(0, newBinder) to replace the first item on my adapter (ListBindAdapter), and then called notifyBinderItemRangeChanged(newBinder, 0, 1). It is unable to replace the first DataBinder.
It crashed at com.yqritc.recyclerviewmultipleviewtypesadapter.DataBindAdapter.onBindViewHolder(DataBindAdapter.java:21)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.