android - RecyclerView 莫名奇妙自动滚动的问题
天蓬老师
天蓬老师 2017-04-17 17:26:34
[Android讨论组]

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就解决了.....
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号