搜索

首页  >  问答  >  正文

android - RecyclerView 莫名奇妙自动滚动的问题

RecyclerView的Adapter里面有8个item。一旦下图中的“王三干杂”在上半部部分的时候,我点击最后四个中的任何一个的时候,图中的“王三干杂”自动滚动到中间去了。

    class CartGoodsViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_goods)
        SimpleDraweeView goodsImg;
        @Bind(R.id.tv_goods_name)
        TextView goodsTitle;
        @Bind(R.id.tv_price)
        TextView goodsPrice;
        @Bind(R.id.et_goods_number)
        AddAndSubView goodsQuantities;
        @Bind(R.id.cb_select_goods)
        CheckBox goodsSelected;
        @Bind(R.id.iv_delete_goods)
        ImageView ivDeleteGoods;
        @Bind(R.id.rl_goods)
        RelativeLayout rlGoods;


        public CartGoodsViewHolder(View itemView) {
            super(itemView);
            try {
                ButterKnife.bind(this,itemView);
            } catch (Exception e) {
                e.printStackTrace();
            }
            goodsSelected.setOnClickListener(v->EventBus.getDefault()
                    .post(new GoodsSelectedEvent(orderModels.get(getAdapterPosition()).getGoodsId()
                            ,orderModels.get(getAdapterPosition()).getShopId()
                            ,goodsSelected.isChecked())));
        }
    }

在Fragment中调用notify方法:

@Override
public void notifyShoppingCartStatus(float totalValue) {
    goodsAdapter.notifyDataSetChanged();
    tvTotal.setText(String.valueOf(totalValue));
}

在presenter中遍历,查找哪些变化了:
    @Subscribe
public void onGoodsSelected(GoodsSelectedEvent event) {
    long totalValue = 0;
    long goodsId = event.getGoodsId();
    long shopId = event.getShopId();
    boolean status = event.isSelected();
    int selectedCount = 0;
    int goodsCountInShop = 0;
    int goodsCountAll = 0;

    for(OrderModel item: orderModels) {

        if(item.getGoodsId() == goodsId) {
            item.setSelected(status);
        }

        if(item.getShopId() == shopId){
            goodsCountInShop++;
            if(item.isSelected()) {
                selectedCount++;
            }
        }

        if(item.isSelected()){
            goodsCountAll++;
            totalValue += item.getTotalValue();
        }
    }

    for(OrderModel item: orderModels) {
        if(item.getShopId() == shopId){
            item.setShopSelected(goodsCountInShop == selectedCount);
        }
    }

    EventBus.getDefault().post(new CheckAllStateChangeEvent(goodsCountAll == orderModels.size()));
    view.notifyShoppingCartStatus(totalValue);

}
补充内容: 把recyclerview 高度设为match_parent就解决了.....
天蓬老师天蓬老师2771 天前577

全部回复(0)我来回复

暂无回复
  • 取消回复