1
  • 我看了你的代码:

    1、这个地方

        @NonNull
        @Override
        public BillListAdapter.InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;
            mTable_bill_dao = new Table_Bill_Dao(parent.getContext());
            mTable_icon_dao = new Table_Icon_Dao(parent.getContext());
            if(viewType == VIEW_TYPE_BILL){//每个数据项
                view = View.inflate(parent.getContext(), R.layout.detail_list_layout_per,null);
                Log.d(TAG, "onCreateViewHolder: in"+VIEW_TYPE_BILL);
            }
            else{//统计数据项
                view = View.inflate(parent.getContext(), R.layout.detail_list_layout_count,null);
                Log.d(TAG, "onCreateViewHolder: in"+VIEW_TYPE_NOT_BILL);
            }
            return new InnerViewHolder(view);
        }
    


    这两个内容的创建,意义不大:

            mTable_bill_dao = new Table_Bill_Dao(parent.getContext());
            mTable_icon_dao = new Table_Icon_Dao(parent.getContext());
    

    因为onCreateViewHolder是多次调用的


    2、这里的删除我没理解


        public void notifyListDateDataChanged(List<BillDetail> newBillDetailsList){//外部更新list
            this.mBillDetailsList.clear();
            notifyItemRangeRemoved(0,mBillDetailsList.size());
            this.mBillDetailsList.addAll(newBillDetailsList);
            for (int i = 0; i <mBillDetailsList.size(); i++) {
                Log.d(TAG, "notifyListDateDataChanged: 第"+i+":"+mBillDetailsList.get(i).get_id());
            }
            notifyDataSetChanged();
        }
    

    为啥会有这个删除的

    notifyItemRangeRemoved(0,mBillDetailsList.size());
    


    3、这里的代码写得不好


        public InnerViewHolder(@NonNull View itemView) {//根据布局类型初始化
                super(itemView);
                if(itemView.findViewById(R.id.detail_list_notes)!=null){
                    tvListNote = itemView.findViewById(R.id.detail_list_notes);
                    tvListAmount = itemView.findViewById(R.id.detail_list_amount);
                    tvListSrc = itemView.findViewById(R.id.detail_list_src);
                }
                if(itemView.findViewById(R.id.detail_total_date)!=null){
                    tvTotalDate = itemView.findViewById(R.id.detail_total_date);
                    tvTotalOut = itemView.findViewById(R.id.detail_total_count_outgo);
                    tvTotalIn = itemView.findViewById(R.id.detail_total_count_income);
                }
            }
    


    多次findViewById了,同一个作用的呢。


    重复的原因是啥呢?


    问题应该是出现在这里:


        private TextView tvListNote,tvListAmount;
        private ImageView tvListSrc;
        private TextView tvTotalDate,tvTotalOut,tvTotalIn;
    


    这些控件是属于类的内部的,为啥整到外部类里呢?


    当onCreateViewHolder时,你去创建InnerViewHolder,就给以上这些变量赋值。


    这个是循环动作,意味着,表示最后一个。


    当这个方法onBindViewHolder回调时,你设置数据,设置的是谁的呀?当然是只能设置最后一个控件的。所以你前面所有的item项都没有设置到了。


    具体,结合你的UI现象分析一下。


    对适配器里的每一项理解不够。


    1153952789488054272  评论     打赏       拉大锯
    • Mofu  回复 @拉大锯
      好的我去试试
      Mofu 2024-03-19 17:51   回复 1374278154541199360
    • Mofu  回复 @拉大锯
      是变量位置问题,太不应该了,作用域变了自然乱了,谢谢大锯!!有些代码复用也做的不好
      Mofu 2024-03-19 17:56   回复 1374278154541199360
    • 拉大锯  回复 @Mofu
      我们有recyclerView的课程,可以去看看。https://www.sunofbeach.net/c/1451211864309538818
      拉大锯 2024-03-20 10:15   回复 1153952789488054272
    • Mofu  回复 @拉大锯
      之前看过,学的不太精0.0
      Mofu 2024-03-20 12:31   回复 1374278154541199360
  • 上全部代码吧。一般来说不会,还有一个api,就是更新第几行到第几行.

    1153952789488054272  评论     打赏       拉大锯
    • Mofu  回复 @拉大锯
      代码发了0.0,大锯哥
      Mofu 2024-03-19 12:31   回复 1374278154541199360
  • 只好新建,但是这样感觉效率很低

    1374278154541199360  评论     打赏       Mofu
    相关问题
    温油的老舅 · 求助
    2020-03-06 19:55 561 2
    哈哈哈嗝 · 求助
    2020-03-23 00:02 802 2
    摸鱼工程师 · 求助
    2020-04-15 22:59 1267 2
    哈哈哈嗝 · 求助
    2020-09-24 01:53 1612 4
    哈哈哈嗝 · 求助
    2020-10-14 02:25 696 2
    喜欢藏风里 · 求助
    2020-10-17 00:13 371 2
    黑羊一匹 · 后端求助
    2021-02-24 07:21 461 2
    独孤求学 · 求助
    2021-03-17 16:51 788 2
    摸摸鱼神 · 求助
    2021-08-13 18:37 364 10
    下雨天像 · 求助
    2021-08-24 07:11 282 2
    yesen · 求助
    2021-08-30 17:45 275 5
    coder · 求助
    2021-11-28 01:33 384 2