thoughtbot / expandable-recycler-view Goto Github PK
View Code? Open in Web Editor NEWCustom Android RecyclerViewAdapters that collapse and expand
Home Page: https://robots.thoughtbot.com/introducing-expandablerecyclerview
License: MIT License
Custom Android RecyclerViewAdapters that collapse and expand
Home Page: https://robots.thoughtbot.com/introducing-expandablerecyclerview
License: MIT License
Hi I want add group to the list when I clicked on the button but when click on button app is crash and I received the error length 2 index 2
this is my code in MainActivity
List<CommentTitle> commentTitles = new ArrayList<>();
List<Comment> comments1 = new ArrayList<>();
List<Comment> comments2 = new ArrayList<>();
comments1.add(new Comment("1395/12/8", "moein", "hello", 0, 10, 10));
comments1.add(new Comment("1395/12/8", "moein", "hello", 1, 10, 10));
comments1.add(new Comment("1395/12/8", "moein", "hello", 2, 10, 10));
commentTitles.add(new CommentTitle("comment 1", comments1));
comments2.add(new Comment("1395/12/8", "ali", "hello", 0, 10, 10));
comments2.add(new Comment("1395/12/8", "reza", "hello", 1, 10, 10));
comments2.add(new Comment("1395/12/8", "hassan", "hello", 2, 10, 10));
commentTitles.add(new CommentTitle("comment 1", comments2));
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.commentList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
commentAdapter = new CommentAdapter(commentTitles, context);
recyclerView.setAdapter(commentAdapter);
Button send = (Button) findViewById(R.id.button);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Comment> comments3 = new ArrayList<>();
comments3.add(new Comment("1395/12/8", "moein","Hello World!", 0, 10, 10));
commentTitles.add(new CommentTitle("c", comments3));
commentAdapter.notifyDataSetChanged();
}
});
Maybe have IExpandableGroup that is an interface which has exact contract that ExpandableGroup has.
This would allow developers to decide what their "Group" model extends from.
In my case, I need to extend BaseModel from dbFlow. I mean, of course I can create a wrapper class that contains my model and extends ExpandableGroup, but then I'm not sure that would even be compatible with my existing client code so a lot of classes might have to adapt.
By having IExpandableGroup, I just have to implement it in my existing model class and respect its contract.
ExpandableGroup can continue being the base class that people can use.
Yes, I know that's asking a lot.. Besides, I see some classes having to change when implementing this request, for example ExpandableRecyclerViewAdapter.onBindChildViewHolder(). It's a breaking change for sure.
Trying to toggle all groups which have child item. using
adapter.toggleGroup(POSITION_WHICH_HAS_CHILD_ITEM);
Is it possible to have SingleCheckExpandableGroup and MultiCheckExpandableGroup in same adapter?
Iw tried using MultiTypeExpandableRecyclerViewAdapter but single checkable groups are not working(im able to check all of the items)
Tried this method on the adapter and got an exception. I just add a new Genre to the simple expand sample app.
i have two buttons and one textview in each child row. those two buttons are used as triggers to count number and then write the number in textview. my problem is, the value of textview is always changes when i scroll the recyclerview and sometimes change the textview on neighbour child row. anyone can help ? thank you.
when i click + on first row (1), textview on third row(2) is also changed.
How can I expand a group programmatically?
For example I have 3 groups. And when user opens an activity - the groups should be expanded already (without making user to expand each group manually).
Maybe something like:
recyclerView.getGroup(1).expand()
p.s. Thanks for this great lib guys!
How to notify when removing an item or inserting an item. i want to notify only particular group section to notify when changes happen not whole adapter. If anyone have a solution than please reply
Thank you in advance
Hi! I'm looking for a Expandable Recycler view that supports Data Binding. This library supports it?
the save state does not work with fragments
I have a group that has a pretty sizeable amount of child items. However when I scroll through the child items, the whole recycler view scrolls and it looks unnatural.
when i click on a group item, i need to make a network call (so dont rightaway expand that group). expand the group and POPULATE subgroup items with data, only on the success callback of that network call. is this possible?
Whenever i click on one of the checkboxes-there is quite a bit of delay till it accualy checks(and i dont mean animation) and i hate it, could u please fix it? it may have something to do with that it keeps trying to reload some of the groups (under the one thats child was checked)
When I scroll down list to the last item and click in group item the child cannot show, it out off screen. After collapsing that and clicking in another group item throw exception
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{949ddf4 position=17 id=-1, oldPos=16, pLpos:16 scrap [attachedScrap] tmpDetached no parent}
whether the item is null or not, it will all write an int into parcelable, in the read setting, only if the item is not null will call readInt, this will cause error in extended class
protected ExpandableGroup(Parcel in) {
title = in.readString();
if (in.readByte() == 0x01) {
int size = in.readInt();
items = new ArrayList(size);
Class type = (Class) in.readSerializable();
in.readList(items, type.getClassLoader());
} else {
items = null;
}
}
@OverRide
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
if (items == null) {
dest.writeByte((byte) (0x00));
dest.writeInt(0);
} else {
dest.writeByte((byte) (0x01));
dest.writeInt(items.size());
final Class<?> objectsType = items.get(0).getClass();
dest.writeSerializable(objectsType);
dest.writeList(items);
}
}
I have an expandable view in which I have a rotation animation similar to the code samples. On group/parent views which have few child items, there is no problem. When there are larger number of child items (i.e. 39 children in my case), the arrow animation on expanding the group is slow and jumps states. When collapsing, the animation is smooth.
when the child list of every group is empty you will get:
java.lang.RuntimeException: Unknown state at com.thoughtbot.expandablerecyclerview.models.ExpandableList.getUnflattenedPosition(ExpandableList.java:80) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:119) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:77) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupClick(ExpandableRecyclerViewAdapter.java:151) at com.thoughtbot.expandablerecyclerview.viewholders.GroupViewHolder.onClick(GroupViewHolder.java:28) at android.view.View.performClick(View.java:4881) at android.view.View$PerformClick.run(View.java:19592) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5756) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)
when you try to expand the last group item
In the following snippet
public class GenreViewHolder extends GroupViewHolder
private TextView genreTitle;
public GenreViewHolder(View itemView) {
super(itemView);
genreTitle = itemView.findViewById(R.id.genre_title);
}
public void setGenreTitle(ExpandableGroup group) {
genreTitle.setText(group.getTitle());
}
}
There is an error at
public class GenreViewHolder extends GroupViewHolder **{**
the opening bracket is missing.
hi good job
can it work like a expandable tree??
3 times expandable?
Currently it seems that I could put the checkboxes or radio buttons on the children, but I'm looking for a way to put the radio button on the parents only. Is this possible?
I want when the activity is open the groups is expandable without user click on them
but toggle group is not work perfectly
I want to have an onclicklistener for a specific group holder. However, when I add an onclicklistener - the previous group holders cannot open.
Hello and thanks for this nice library.
However there're several problems:
@Override public void onSaveInstanceState(Bundle outState) { outState.putParcelableArrayList(CHECKED_STATE_MAP, (ArrayList<? extends Parcelable>) expandableList.groups); super.onSaveInstanceState(outState); }
I tried to fix this by changing List to ArrayList where required, but there's another problem:
Cheers!
is there any child click listener?
Hi,
I want expand only one item at a time when I click on another list item previous should automatically collapse.
I needed an expandable recyclerview which allows only one group to be expanded at the time. I added the follwoing code to GenreAdapter in your sample project:
`private ExpandableGroup expandedGroup;
setOnGroupExpandCollapseListener(new GroupExpandCollapseListener() {
@OverRide
public void onGroupExpanded(ExpandableGroup group) {
if (expandedGroup != null){
toggleGroup(expandedGroup);
}
expandedGroup = group;
}
@Override
public void onGroupCollapsed(ExpandableGroup group) {
}
});`
It works fine when the previously expanded group is visible on the screen. If the group is off the screen, it will not collapse when calling toggleGroup on it. Unless I am doing something wrong?
Looks like there is an infinite loop:
12-23 11:46:07.395 25187-25187/com.thoughtbot.expandablerecyclerview.sample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thoughtbot.expandablerecyclerview.sample, PID: 25187
java.lang.StackOverflowError: stack size 8MB
at com.thoughtbot.expandablerecyclerview.models.ExpandableListPosition.getRecycledOrCreate(ExpandableListPosition.java:115)
at com.thoughtbot.expandablerecyclerview.models.ExpandableListPosition.obtain(ExpandableListPosition.java:101)
at com.thoughtbot.expandablerecyclerview.models.ExpandableList.getUnflattenedPosition(ExpandableList.java:73)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.isGroupExpanded(ExpandCollapseController.java:63)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:85)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44)
at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172)
at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25)
at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121)
at com.thoughtbot.expandablerecyclerview.ExpandColl
Is there any idea for expanding groups for the first time launching recyclerview, here is my code but it has problem when I click to collapse the second time, it go into animateExpand() so that my indication arrow not animate the right way:
`@Override
public ChatGroupParentVH onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.item_header, parent, false);
final ChatGroupParentVH holder = new ChatGroupParentVH(itemView);
new Handler().post(new Runnable() {
@OverRide
public void run() {
holder.expand();
onGroupClick(holder.getAdapterPosition());
}
});
return holder;
}
Thank you!`
When combining these two features the checked positions change unpredictably. I assume it is because for example, lets say that group viewholder of type A stores the information about the children's checked status, but then viewholder of type B doesn't have that information.
The method "toggleGroup" in the ExpandCollapseController should return true if the group is expanded, after the toggle, false if the group is now collapsed, but it returns true if the group was expanded before the toggle.
Hi. Thank for great library!
I implement this expandable-recycler-view for my project, but I don't know how to reload data for it.
You can write a tutorial or sample code?
Thank so much!
Hi,
I just spent a while to find out why my code was not working and noticed that CheckedTextView
is hardcoded.
Every Checkable
should implement toggle()
. So why is this hardcoded?
i did not see the difference between the MultiCheckBandAdapter and SingleCheckBandAdapter
There's some standard OSS repo templates we use that are found here: https://github.com/thoughtbot/templates
We should update this to include those before releasing it
when i try to clear the adapter i get a null pointer exception ,how i can clear the recyclerview ?
When you start an adapter with an empty list like:
ArrayList<GroupObject> groupObjectList = new ArrayList<GroupObject>
MyAdaper adapter = new MyAdapter(groupObjectList)
it crashes, so for example if you are downloading objects and get an empty list you have to do some workarounds to use the adapter. I think this is because the numberOfVisibleItemsInGroup
in ExpandableListClass
always returns 1 or more, so it´s getting a java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
I'm trying to implement an expandable list using this library but I need that my RecyclerView working with WRAP_CONTENT (auto update of height attribute).
I used hasFixedSize = true and setNestedScrollingEnabled=false but the LinearLayoutManager doesn't update the RecyclerView size.
My layout hierarchy will be like this:
= FrameLayout
== LinearLayout (Vertical)
=== LinearLayout (Horizontal)
=== RecyclerView
== LinearLayout (Vertical)
There are a custom layout manager to make this work? Thanks
Does the library support mutiple view types for the parent as well?
I am clicking an item (child), opens a dialog fragment. If CANCEL is pressed then nothing happens and if OK is pressed then the background color of the childview changes.
This is notified through notifyItemChanged()
. If the group is collapsed and expanded again then the data change gets reflect but it does not get reflected if the group remains expanded.
For the change to be reflected immediately I have tried the following
DefaultItemAnimator animator = new DefaultItemAnimator() { @Override public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) { return true; } }; rvFoodList.setItemAnimator(animator);
((DefaultItemAnimator) rvFoodList.getItemAnimator()).setSupportsChangeAnimations(false);
rvFoodList.setItemAnimator(null);
((SimpleItemAnimator) rvFoodList.getItemAnimator()).setSupportsChangeAnimations(true);
None of these work.
Please tell how can I achieve this?
Can you Reduce the minSdkVersion
to 14
?
how to get selected child value?
My scenario is as follow:
I have categories and subcategories and there is not limit to the depth of subcategories. (sub sub sub sub ..etc)
I also have products for each categories, these products have sub products, too.
Let's say I have 3 groups and I want the first one to already be expanded when I open the app.
Hi, I am filtering some objects using a searchview, is it posible to do it with this library? What would be a possible implementation
This error appear even if i unsure the data is not empty or null in the onSaveInstace method
The trace of the Error :
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.thoughtbot.expandablerecyclerview.models.ExpandableGroup.writeToParcel(ExpandableGroup.java:67)
at android.os.Parcel.writeParcelable(Parcel.java:1357)
When expanding (and collapsing) a group programmatically using "toggleGroup" method, the collapse and expand events are not triggered. It should be triggered to enable animation.
Hello, the documentation is great for the Java part of the library. Can you provide the layout files in the docs too?
The genre_title
, artist_name
, list_item_genre
, list_item_artist
and recycler_view
.
How does one talk to the other?
Or if there's another source of documentation with a full look at what the code looks like, can you point me in that direction?
Much appreciated.
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.