ExpandableRecyclerAdapter How to make an element move upward when expanding an element

This is my ExpandableRecyclerAdapter adapter.

public class MyAdapter extends ExpandableRecyclerAdapter<MyAdapter.ProductParentViewHolder, MyAdapter.ProductChildViewHolder> { private LayoutInflater mInflater; private Context context; private List<? extends ParentListItem> mParentItemList; public MyAdapter(Context context, List<ParentListItem> itemList) { super(itemList); mInflater = LayoutInflater.from(context); this.context = context; this.mParentItemList = itemList; } @Override public ProductParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) { View view = mInflater.inflate(R.layout.list_item_crime_parent, viewGroup, false); return new ProductParentViewHolder(view); } @Override public ProductChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) { View view = mInflater.inflate(R.layout.list_item_crime_child, viewGroup, false); return new ProductChildViewHolder(view); } @Override public void onBindParentViewHolder(ProductParentViewHolder crimeParentViewHolder, int i, ParentListItem parentListItem) { Product product = (Product) parentListItem; crimeParentViewHolder.productName.setText(product.getBrandName() + " " + product.getProductName()); Glide.with(context) .load(product.getProductImagePath()) .placeholder(R.drawable.placeholder) .error(R.drawable.placeholder) .into(crimeParentViewHolder.thumbnail); } @Override public void onBindChildViewHolder(ProductChildViewHolder productChildViewHolder, int i, Object childListItem) { final ProductVariant productVariant = (ProductVariant) childListItem; productChildViewHolder.mCrimeDateText.setText(productVariant.getVariantName()); productChildViewHolder.variantMrp.setText(context.getString(R.string.positive_amount, productVariant.getMRP())); productChildViewHolder.variantMrp.setPaintFlags(productChildViewHolder.variantMrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); productChildViewHolder.variantSellPrice.setText(context.getString(R.string.positive_amount, productVariant.getSellPrice())); //productChildViewHolder.variantMrp.setText(productVariant.getMRP().toString()); //productChildViewHolder.variantSellPrice.setText(productVariant.getSellPrice().toString()); if (productVariant.getInCart() == 0) { productChildViewHolder.btnProductDetailAddToCart.setVisibility(View.VISIBLE); productChildViewHolder.btnProductDetailMinus.setVisibility(View.GONE); productChildViewHolder.btnProductDetailQty.setVisibility(View.GONE); productChildViewHolder.btnProductDetailPlus.setVisibility(View.GONE); } else { productChildViewHolder.btnProductDetailAddToCart.setVisibility(View.GONE); productChildViewHolder.btnProductDetailMinus.setVisibility(View.VISIBLE); productChildViewHolder.btnProductDetailQty.setVisibility(View.VISIBLE); productChildViewHolder.btnProductDetailPlus.setVisibility(View.VISIBLE); } int quantity = productVariant.getInCart(); productChildViewHolder.btnProductDetailQty.setText(Integer.toString(quantity)); productChildViewHolder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { productVariant.setInCart(1); //Utility.loadShoppingCartItems(); notifyDataSetChanged(); invalidateOptionsMenu(); //holder.db.addItem(new CartItem(1, productVariant.getProductID(), productVariant.getVariantID(), 1)); } }); productChildViewHolder.btnProductDetailPlus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { productVariant.setInCart(1 + productVariant.getInCart()); notifyDataSetChanged(); invalidateOptionsMenu(); //if (productVariant.getInCart() > 0) { //int count = holder.db.updateSingleRow(productVariant.getProductID(), productVariant.getVariantID(), productVariant.getInCart()); //} } }); productChildViewHolder.btnProductDetailMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { productVariant.setInCart(productVariant.getInCart() - 1); notifyDataSetChanged(); invalidateOptionsMenu(); if (productVariant.getInCart() == 0) { //int count = holder.db.deleteSingleRow(productVariant.getProductID(), productVariant.getVariantID()); } else if (productVariant.getInCart() > 0) { //int count = holder.db.updateSingleRow(productVariant.getProductID(), productVariant.getVariantID(), productVariant.getInCart()); } //Utility.displayToast(holder.db.getItemsCount() + ""); } }); //crimeChildViewHolder.mCrimeSolvedCheckBox.setChecked(productVariant.isSolved()); } public class ProductParentViewHolder extends ParentViewHolder { private static final float INITIAL_POSITION = 0.0f; private static final float ROTATED_POSITION = 180f; private final boolean HONEYCOMB_AND_ABOVE = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; public TextView productName; public ImageView thumbnail; public ImageButton mParentDropDownArrow; public ProductParentViewHolder(View itemView) { super(itemView); productName = (TextView) itemView.findViewById(R.id.productName); thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail); // mParentDropDownArrow = (ImageButton) itemView.findViewById(R.id.parent_list_item_expand_arrow); } @SuppressLint("NewApi") @Override public void setExpanded(boolean expanded) { super.setExpanded(expanded); if (!HONEYCOMB_AND_ABOVE) { return; } if (expanded) { // mParentDropDownArrow.setRotation(ROTATED_POSITION); } else { // mParentDropDownArrow.setRotation(INITIAL_POSITION); } } } public class ProductChildViewHolder extends ChildViewHolder { public TextView mCrimeDateText; public TextView variantMrp; public TextView variantSellPrice; public Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailMinus; public TextView btnProductDetailQty; public ProductChildViewHolder(View itemView) { super(itemView); mCrimeDateText = (TextView) itemView.findViewById(R.id.variantName); variantMrp = (TextView) itemView.findViewById(R.id.productVariantMrp); variantSellPrice = (TextView) itemView.findViewById(R.id.productVariantSellPrice); btnProductDetailAddToCart = (Button) itemView.findViewById(R.id.btnProductDetailAddToCart); btnProductDetailPlus = (Button) itemView.findViewById(R.id.btnProductDetailPlus); btnProductDetailMinus = (Button) itemView.findViewById(R.id.btnProductDetailMinus); btnProductDetailQty = (TextView) itemView.findViewById(R.id.btnProductDetailQty); } } } 

When I am at the bottom of the page and click on the element that it expands, but the exapnded child is not displayed to the user because it is at the bottom of the screen.

I want to move this item to the screen and show the user advanced items.

How can i do this?

+6
source share
3 answers

You can simply use the setSelectedGroup() method

 expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { expandableListView.setSelectedGroup(groupPosition); return true; } }); 

This will move the selected group to the top.

EDIT Finally, I also got a solution for your ExpandableRecyclerAdapter. Just put this method inside your adapter implementation. You will also need the recyclerView link inside the adapter, which you can pass to the adapter during initialization.

 int lastPos = -1; @Override public void onParentListItemExpanded(int position) { List<? extends ParentListItem> parentItemList = this.getParentItemList(); collapseAllParents(); int finalPos = position; if (lastPos != -1 && lastPos < position) { finalPos = position - parentItemList.get(lastPos).getChildItemList().size(); } expandParent(finalPos); mRecyclerView.smoothScrollToPosition(finalPos); lastPos = position; } 

I found this problem at https://github.com/bignerdranch/expandable-recycler-view/issues/156 . Although the solution given there did not work. A small tweak that makes it work.

+1
source

Use this following code in the listview dropdown. Do something to your liking.

 yourExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public boolean onGroupClick(final ExpandableListView parent, View v, final int groupPosition, long id) { .... Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { parent.smoothScrollToPositionFromTop(groupPosition + 1, 0); } },100); .... return true; } }); 
0
source

All Articles