Expandablelistview With Viewpager Combination As Its Child
UPDATE - Added experimentation result Is it possible to implement an ExpandableListView to have a viewpager child? I tried to put viewpager as a child in ExpandableListView but it
Solution 1:
can you check this API https://developer.android.com/reference/android/widget/ExpandableListView
A view that shows items in a vertically scrolling two-level list
Solution 2:
I made a sample in Java. Hope that helps!
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btnClearChecks"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Clear Checks" /><Buttonandroid:id="@+id/btnPutOrder"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Put Order" /></LinearLayout><ExpandableListViewandroid:id="@+id/expandedListView"android:layout_width="match_parent"android:layout_height="match_parent"></ExpandableListView></LinearLayout>
expanded_list_group.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:descendantFocusability="blocksDescendants" ><CheckBoxandroid:id="@+id/cb_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:layout_gravity="center_vertical" /><TextViewandroid:id="@+id/tv_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Text"android:textSize="30sp" /></LinearLayout>
list_child_pager.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:paddingStart="60dp"><android.support.v4.view.ViewPagerandroid:id="@+id/layout_child"android:layout_width="match_parent"android:layout_height="200dp" /></LinearLayout>
list_pager_item.xml:
<?xml version="1.0" encoding="utf-8"?><GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:columnCount="@integer/pager_col_count"android:rowCount="@integer/pager_row_count"></GridLayout>
integers.xml:
<?xml version="1.0" encoding="utf-8"?><resources><integername="pager_col_count">3</integer><integername="pager_row_count">3</integer></resources>
ChildItemSample.java
publicclassChildItemSample {
privateboolean checked = false;
privateString name;
private int qty;
public int getQty() {
return qty;
}
publicvoidsetQty(int qty) {
this.qty = qty;
}
publicbooleanisChecked() {
return checked;
}
publicvoidsetChecked(boolean checked) {
this.checked = checked;
}
publicStringgetName() {
return name;
}
publicChildItemSample(String name, int qty){
this.name = name;
this.qty = qty;
}
}
MainActivity.java:
publicclassMainActivityextendsAppCompatActivity {
Button clearChecks, putOrder;
ExpandableListView expandableListView;
ExpandableListPagerAdapter expandableListAdapter;
int lastExpandedPosition = -1;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandedListView);
clearChecks = findViewById(R.id.btnClearChecks);
putOrder = findViewById(R.id.btnPutOrder);
List<String> listTitle = genGroupList();
expandableListAdapter = newExpandableListPagerAdapter(this, getSupportFragmentManager(), listTitle, genChildList(listTitle));
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(newExpandableListView.OnGroupExpandListener() {
@OverridepublicvoidonGroupExpand(int groupPosition) {
if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
clearChecks.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
expandableListAdapter.clearChecks();
}
});
putOrder.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
ArrayList<String> putOrder = expandableListAdapter.getOrderList();
StringBuilder msg = newStringBuilder();
for(int i=0; i<putOrder.size(); i++){
msg.append(putOrder.get(i));
msg.append("\n");
}
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
privateArrayList<String> genGroupList(){
ArrayList<String> listGroup = newArrayList<>();
for(int i=1; i<10; i++){
listGroup.add("Group: " + i);
}
return listGroup;
}
privateMap<String, List<ChildItemSample>> genChildList(List<String> header){
Map<String, List<ChildItemSample>> listChild = newHashMap<>();
for(int i=0; i<header.size(); i++){
List<ChildItemSample> testDataList = newArrayList<>();
int a = (int)(Math.random()*28);
for(int j=0; j<a; j++){
ChildItemSample testItem = newChildItemSample("Child " + (j + 1), 0);
testDataList.add(testItem);
}
listChild.put(header.get(i), testDataList);
}
return listChild;
}
}
ExpandableListPagerAdapter.java:
publicclassExpandableListPagerAdapterextendsBaseExpandableListAdapter {
privateint child_items_per_page;
private Context context;
private FragmentManager fm;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
privatestaticint checkedBoxesCount;
privateboolean[] checkedGroup;
ExpandableListPagerAdapter(Context context, FragmentManager manager, List<String> listGroup, Map<String,
List<ChildItemSample>> listChild) {
this.context = context;
fm = manager;
this.listGroup = listGroup;
this.listChild = listChild;
checkedBoxesCount = 0;
checkedGroup = newboolean[listGroup.size()];
child_items_per_page = context.getResources().getInteger(R.integer.pager_col_count) *
context.getResources().getInteger(R.integer.pager_row_count);
}
@OverridepublicintgetGroupCount() {
return listGroup.size();
}
@Override//******* Special *******publicintgetChildrenCount(int groupPosition) {
return1;
}
@Overridepublic String getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
@Overridepublic ChildItemSample getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
@OverridepubliclonggetGroupId(int groupPosition) {
return groupPosition;
}
@OverridepubliclonggetChildId(int groupPosition, int childPosition) {
return childPosition;
}
@OverridepublicbooleanhasStableIds() {
returnfalse;
}
@Overridepublic View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
StringitemGroup= getGroup(groupPosition);
GroupViewHolder groupViewHolder;
if(view == null){
LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expanded_list_group, null);
groupViewHolder = newGroupViewHolder();
groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
groupViewHolder.cbGroup.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
intpos= (int)view.getTag();
checkedGroup[pos] = !checkedGroup[pos];
for(ChildItemSample item : listChild.get(listGroup.get(pos))){
item.setChecked(checkedGroup[pos]);
}
notifyDataSetChanged();
}
});
view.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder)view.getTag();
}
groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, listChild.get(listGroup.get(groupPosition)).size()));
if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
else groupViewHolder.cbGroup.setChecked(false);
groupViewHolder.cbGroup.setTag(groupPosition);
return view;
}
@Overridepublic View getChildView(finalint groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewrowView= inflater.inflate(R.layout.list_child_pager, null);
ViewPagerchildLayout= rowView.findViewById(R.id.layout_child);
List<ChildItemSample> childItemSampleList = listChild.get(listGroup.get(groupPosition));
ChildPagerAdapteradapter=newChildPagerAdapter(fm, childItemSampleList, child_items_per_page);
childLayout.setAdapter(adapter);
return rowView;
}
publicclassChildPagerAdapterextendsFragmentStatePagerAdapter {
private List<ChildItemSample> pagerItemList;
privateint items_per_page;
ChildPagerAdapter(FragmentManager fm, List<ChildItemSample> pagerItemList, int items_per_page) {
super(fm);
this.pagerItemList = pagerItemList;
this.items_per_page = items_per_page;
}
@Overridepublic Fragment getItem(int position) {
return ChildFragment.newInstance(position, pagerItemList, items_per_page);
}
@OverridepublicintgetCount() {
intremainedItemCount= pagerItemList.size()%child_items_per_page;
if(remainedItemCount == 0)
return (pagerItemList.size()/child_items_per_page);
elsereturn (pagerItemList.size()/child_items_per_page + 1);
}
}
publicstaticclassChildFragmentextendsFragment {
privatestaticfinalStringSECTION_NUMBER="section_number";
privatestaticfinalStringITEMS_PER_PAGE="items/page";
privatestatic List<ChildItemSample> itemList;
publicChildFragment() {}
publicstatic ChildFragment newInstance(int sectionNumber, List<ChildItemSample> pagerItemList,
int itemPerPage) {
ChildFragmentfragment=newChildFragment();
Bundleargs=newBundle();
args.putInt(SECTION_NUMBER, sectionNumber);
args.putInt(ITEMS_PER_PAGE, itemPerPage);
fragment.setArguments(args);
itemList = pagerItemList;
return fragment;
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
intchild_items_per_page= getArguments().getInt(ITEMS_PER_PAGE);
intstart_item= getArguments().getInt(SECTION_NUMBER)*child_items_per_page;
intitemCount= itemList.size() - getArguments().getInt(SECTION_NUMBER)*child_items_per_page;
if(itemCount > child_items_per_page) itemCount = child_items_per_page;
itemCount += getArguments().getInt(SECTION_NUMBER)*child_items_per_page;
GridLayoutpageView= (GridLayout)inflater.inflate(R.layout.list_pager_item, container, false);
for(int i=start_item; i<itemCount; i++){
ChildItemSampleexpandedListText= itemList.get(i);
CheckBoxcbChild=newCheckBox(getContext());
GridLayout.LayoutParamsparams=newGridLayout.LayoutParams();
params.width = (int)(80 * getContext().getResources().getDisplayMetrics().density);
cbChild.setLayoutParams(params);
cbChild.setChecked(expandedListText.isChecked());
cbChild.setText(expandedListText.getName());
cbChild.setTag(i);
pageView.addView(cbChild);
cbChild.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
CheckBoxcb= (CheckBox) view;
intpos= (int) view.getTag();
ChildItemSampleselectedItem= itemList.get(pos);
selectedItem.setChecked(cb.isChecked());
if(cb.isChecked()){
checkedBoxesCount++;
Toast.makeText(getContext(),"Checked value is: " +
itemList.get(pos).getName(),
Toast.LENGTH_SHORT).show();
}else {
checkedBoxesCount--;
if(checkedBoxesCount == 0){
Toast.makeText(getContext(),"nothing checked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getContext(),"unchecked",Toast.LENGTH_SHORT).show();
}
}
}
});
}
return pageView;
}
}
publicvoidclearChecks() {
for(int i=0; i<checkedGroup.length; i++) checkedGroup[i] = false;
for(List<ChildItemSample> value : listChild.values()) {
for (ChildItemSample sample : value) {
sample.setChecked(false);
}
}
checkedBoxesCount = 0;
notifyDataSetChanged();
}
public ArrayList<String> getOrderList(){
ArrayList<String> overallOrder = newArrayList<>();
for(int i=0; i<getGroupCount(); i++){
//for(int j=0; j<getChildrenCount(i); j++){for(int j=0; j<listChild.get(getGroup(i)).size(); j++){
if(getChild(i,j).isChecked()){
StringnewOrder= getGroup(i) + ">" + getChild(i, j).getName();
overallOrder.add(newOrder);
}
}
}
return overallOrder;
}
@OverridepublicbooleanisChildSelectable(int groupPosition, int childPosition) {
returntrue;
}
privateclassGroupViewHolder {
CheckBox cbGroup;
TextView tvGroup;
}
}
Another sample adapter that has ViewPager child (Without fragment)
publicclassExpandableListPagerAdapter2extendsBaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
privateint checkedBoxesCount;
privateboolean[] checkedGroup;
privateint child_items_per_page;
ExpandableListPagerAdapter2(Context context, List<String> listGroup, Map<String,
List<ChildItemSample>> listChild) {
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
checkedBoxesCount = 0;
checkedGroup = newboolean[listGroup.size()];
child_items_per_page = context.getResources().getInteger(R.integer.pager_col_count) *
context.getResources().getInteger(R.integer.pager_row_count);
}
@OverridepublicintgetGroupCount() {
return listGroup.size();
}
// ******* Special *******@OverridepublicintgetChildrenCount(int groupPosition) {
return1;
}
@Overridepublic String getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
@Overridepublic ChildItemSample getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
@OverridepubliclonggetGroupId(int groupPosition) {
return groupPosition;
}
@OverridepubliclonggetChildId(int groupPosition, int childPosition) {
return childPosition;
}
@OverridepublicbooleanhasStableIds() {
returnfalse;
}
@Overridepublic View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
StringitemGroup= getGroup(groupPosition);
GroupViewHolder groupViewHolder;
if(view == null){
LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expanded_list_group, null);
groupViewHolder = newGroupViewHolder();
groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
groupViewHolder.cbGroup.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
intpos= (int)view.getTag();
checkedGroup[pos] = !checkedGroup[pos];
for(ChildItemSample item : listChild.get(listGroup.get(pos))){
item.setChecked(checkedGroup[pos]);
}
notifyDataSetChanged();
}
});
view.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder)view.getTag();
}
groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, listChild.get(listGroup.get(groupPosition)).size()));
if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
else groupViewHolder.cbGroup.setChecked(false);
groupViewHolder.cbGroup.setTag(groupPosition);
return view;
}
@Overridepublic View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewrowView= inflater.inflate(R.layout.list_child_pager, null);
ViewPagerchildLayout= rowView.findViewById(R.id.layout_child);
List<ChildItemSample> childItemSampleList = listChild.get(listGroup.get(groupPosition));
ChildPagerAdapteradapter=newChildPagerAdapter(childItemSampleList);
childLayout.setAdapter(adapter);
return rowView;
}
publicclassChildPagerAdapterextendsPagerAdapter {
private List<ChildItemSample> pagerItemList;
ChildPagerAdapter(List<ChildItemSample> pagerItemList) {
this.pagerItemList = pagerItemList;
}
@Overridepublic Object instantiateItem(ViewGroup container, int position) {
intstart_item= position*child_items_per_page;
intitemCount= pagerItemList.size() - start_item;
if(itemCount > child_items_per_page) itemCount = child_items_per_page;
itemCount += start_item;
LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GridLayoutpageView= (GridLayout)inflater.inflate(R.layout.list_pager_item, container, false);
for(int i=start_item; i<itemCount; i++){
ChildItemSampleexpandedListText= pagerItemList.get(i);
CheckBoxcbChild=newCheckBox(context);
GridLayout.LayoutParamsparams=newGridLayout.LayoutParams();
params.width = (int)(80 * context.getResources().getDisplayMetrics().density);
cbChild.setLayoutParams(params);
cbChild.setChecked(expandedListText.isChecked());
cbChild.setText(expandedListText.getName() + "(" + expandedListText.getQty() + ")");
cbChild.setTag(i);
pageView.addView(cbChild);
cbChild.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
intpos= (int) view.getTag();
finalChildItemSampleselectedItem= pagerItemList.get(pos);
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle(selectedItem.getName());
finalEditTexteditText=newEditText(context);
editText.setText("");
editText.append(String.valueOf(selectedItem.getQty()));
builder.setView(editText);
builder.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
selectedItem.setQty(Integer.parseInt(editText.getText().toString()));
ExpandableListPagerAdapter2.this.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", null);;
builder.show();
editText.requestFocus();
editText.postDelayed(newRunnable() {
@Overridepublicvoidrun() {
InputMethodManagerkeyboard= (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);
}
},200);
}
});
}
container.addView(pageView);
return pageView;
}
@OverridepublicvoiddestroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@OverridepublicintgetCount() {
intremainedItemCount= pagerItemList.size()%child_items_per_page;
if(remainedItemCount == 0)
return (pagerItemList.size()/child_items_per_page);
elsereturn (pagerItemList.size()/child_items_per_page + 1);
}
@OverridepublicbooleanisViewFromObject(View view, Object object) {
returnview== object;
}
}
publicvoidclearChecks() {
for(int i=0; i<checkedGroup.length; i++) checkedGroup[i] = false;
for(List<ChildItemSample> value : listChild.values()) {
for (ChildItemSample sample : value) {
sample.setChecked(false);
}
}
checkedBoxesCount = 0;
notifyDataSetChanged();
}
public ArrayList<ChildItemSample> getOrderList(){
ArrayList<ChildItemSample> overallOrder = newArrayList<>();
for(int i=0; i<getGroupCount(); i++){
for(int j=0; j<listChild.get(getGroup(i)).size(); j++){
if(getChild(i,j).getQty() > 0){
ChildItemSamplenewOrder=newChildItemSample(getGroup(i) + ">" +
getChild(i, j).getName(), getChild(i, j).getQty());
overallOrder.add(newOrder);
}
}
}
return overallOrder;
}
@OverridepublicbooleanisChildSelectable(int groupPosition, int childPosition) {
returntrue;
}
privateclassGroupViewHolder {
CheckBox cbGroup;
TextView tvGroup;
}
}
Post a Comment for "Expandablelistview With Viewpager Combination As Its Child"