ExpandableRecyclerAdapter如何在展开项目时强制项目向上移动

时间:2016-05-14 10:24:53

标签: android

这是我的ExpandableRecyclerAdapter适配器

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);

        }
    }
}

当我在页面底部并单击项目时,它会展开,但是exapnded子项目不会显示给用户,因为它在屏幕底部。

我想在屏幕上移动该项目并向用户显示扩展项目。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

您只需使用方法setSelectedGroup()

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

这会将所选组移至顶部

修改 最后,我还为您的ExpandableRecyclerAdapter提供了解决方案。只需将此方法放在适配器实现中即可。此外,您还需要在适配器内引用recyclerView,您可以在初始化时将其传递给适配器。

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;
}

我在https://github.com/bignerdranch/expandable-recycler-view/issues/156找到了这个问题。虽然那里的解决方案没有奏效。稍作调整即可使其发挥作用。

答案 1 :(得分:0)

在展开式listview点击监听器中使用以下代码。做点事情

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;
        }
    });

答案 2 :(得分:0)