Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableRecyclerAdapter How to force item to move up while expanding an item

Tags:

android

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 bottom of the page and click on item it expands, but exapnded child item doesn't shows to user because it is bottom in the screen.

I want to move that item up in the screen and show expanded items to user.

How can i do that?

like image 773
Devesh Agrawal Avatar asked May 14 '16 10:05

Devesh Agrawal


1 Answers

You can simply use the method setSelectedGroup()

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 came out with a solution for your ExpandableRecyclerAdapter also. Simply put this method inside your adapter implementation. Also you will require the reference of the recyclerView inside the adapter which you can pass to the adapter at the time of 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 issue at https://github.com/bignerdranch/expandable-recycler-view/issues/156 . Although the solution given there didn't work. Slight tweaking to that make it work.

like image 136
Ankit Aggarwal Avatar answered Nov 03 '22 15:11

Ankit Aggarwal