VIDEO
import libs
compile 'com.bignerdranch.android:expandablerecyclerview:2.1.1'
activity_expand
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :orientation= "vertical" >
<android.support.v7.widget.RecyclerView
android :id= "@+id/recycler_view"
android :layout_width= "match_parent"
android :layout_height= "match_parent" />
</LinearLayout >
item_subcategory_fragment_elv_group
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :orientation= "vertical"
android :layout_height= "48dp" >
<TextView
android :id= "@+id/lblListHeader"
android :text= "abc"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
</LinearLayout >
item_subcategory_fragment_elv_child
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :orientation= "vertical"
android :layout_height= "48dp" >
<TextView
android :layout_width= "wrap_content"
android :id= "@+id/lblListItem"
android :layout_marginLeft= "70dp"
android :layout_height= "wrap_content" />
</LinearLayout >
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity" ;
RecyclerView recycler_view ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_expand );
List<ParentListItem> parentListItems = new ArrayList<>();
List<SubcategoryParentListItem> listItems = new ArrayList<>();
for (int i = 0 ; i < 50 ; i++) {
SubcategoryParentListItem ob = new SubcategoryParentListItem();
listItems.add(ob);
}
for (int i = 0 ; i < 100 ; i++) {
SubcategoryParentListItem ob = new SubcategoryParentListItem();
ob.setChildItemList(listItems);
parentListItems.add(ob);
}
recycler_view = (RecyclerView) findViewById(R.id.recycler_view );
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recycler_view .setLayoutManager(mLayoutManager);
recycler_view .setAdapter(new SubCategoryExpandableRecyclerAdapter(this , parentListItems));
}
}
public class SubCategoryExpandableRecyclerAdapter extends ExpandableRecyclerAdapter<SubCategoryExpandableRecyclerAdapter.MyParentViewHolder, SubCategoryExpandableRecyclerAdapter.MyChildViewHolder> {
private LayoutInflater mInflater ;
public SubCategoryExpandableRecyclerAdapter(Context context, List<ParentListItem> itemList) {
super (itemList);
mInflater = LayoutInflater.from (context);
}
@Override
public MyParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
View view = mInflater .inflate(R.layout.item_subcategory_fragment_elv_group , viewGroup, false );
return new MyParentViewHolder(view);
}
@Override
public MyChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
View view = mInflater .inflate(R.layout.item_subcategory_fragment_elv_child , viewGroup, false );
return new MyChildViewHolder(view);
}
@Override
public void onBindParentViewHolder(MyParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
SubcategoryParentListItem subcategoryParentListItem = (SubcategoryParentListItem) parentListItem;
// handler for row parent
parentViewHolder.lblListHeader .setText(subcategoryParentListItem.mTitle );
}
@Override
public void onBindChildViewHolder(MyChildViewHolder childViewHolder, int position, Object childListItem) {
SubcategoryParentListItem subcategoryChildListItem = (SubcategoryParentListItem) childListItem;
// handler for row child
childViewHolder.txtListChild .setText(subcategoryChildListItem.mTitle );
}
public class MyParentViewHolder extends ParentViewHolder {
public TextView lblListHeader ;
public MyParentViewHolder(View itemView) {
super (itemView);
// init view parent
lblListHeader = (TextView) itemView.findViewById(R.id.lblListHeader );
}
}
public class MyChildViewHolder extends ChildViewHolder {
public TextView txtListChild ;
public MyChildViewHolder(View itemView) {
super (itemView);
// init view child list
txtListChild = (TextView) itemView.findViewById(R.id.lblListItem );
}
}
}
public class SubcategoryParentListItem implements ParentListItem {
// object public String mTitle ;
public List<SubcategoryParentListItem > mChildItemList ;
public SubcategoryParentListItem() {
mTitle = "SubcategoryParentListItem" ;
}
@Override
public List<SubcategoryParentListItem > getChildItemList() {
return mChildItemList ;
}
public void setChildItemList(List<SubcategoryParentListItem > list) {
mChildItemList = list;
}
@Override public boolean isInitiallyExpanded() {
return false ;
}
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehow to get detail for child item ?
ReplyDelete