美文网首页
listView记忆功能

listView记忆功能

作者: 牙叔教程 | 来源:发表于2021-05-07 16:08 被阅读0次

    牙叔教程 简单易学

    使用场景

    listView中有单选按钮的时候, 需要记住单选按钮的勾选状态,
    在测试list的时候, 如果list绑定的数据多了之后, 我们在滑动上下几次之后

    单选按钮的勾选状态就不正确了

    本来是勾选的, 变成了不勾选
    本来是不勾选的, 变成了勾选

    autojs版本

    8.7.7-0

    事出有因

    导致这个错误的原因是list的回收复用,
    当控件从可见到不可见, 再从不可见到可见的时候,
    会重新给控件绑定数据
    就这绑定数据这一步发成了错误

    解决办法

    绑定数据这一步需要重写,
    我们不适用list,
    而是使用原生的RecyclerView
    详细的类名 androidx.recyclerview.widget.RecyclerView

    代码讲解

    1. 导入类
    importClass(Packages.androidx.recyclerview.widget.RecyclerView);
    importClass(Packages.androidx.recyclerview.widget.LinearLayoutManager);
    
    2. 布局
    ui.layout(
      <vertical>
        <vertical>
          <text textSize="30sp" w="*" gravity="center" textStyle="bold">
            牙叔教程 简单易学
          </text>
          <button id="addUser">创建新房间</button>
          <button id="showData">显示房间数据</button>
          <androidx.recyclerview.widget.RecyclerView id="roomList" h="*" w="*" />
        </vertical>
      </vertical>
    );
    
    3. recycleView子控件视图
    let boxXml = (
      <vertical w="*" padding="8" margin="50">
        <radiogroup id="roomList" orientation="horizontal">
          <radio text="房间1号"></radio>
          <radio text="房间2号"></radio>
        </radiogroup>
        <text id="time"></text>
      </vertical>
    );
    
    4. 创建adapter
    function createGiftBoxAdapter(roomList) {
      return RecyclerView.Adapter({
        onCreateViewHolder: function (parent, viewType) {
          log("onCreateViewHolder");
          // 视图创建
          let view;
          let holder;
          view = ui.inflate(boxXml, parent, false);
          holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
          view.time.click(function () {
            toastLog(view.time.text());
          });
          view.roomList.setOnCheckedChangeListener({
            onCheckedChanged: function (radioGroup, checkedId) {
              let checkView = view.findViewById(checkedId);
              let room = roomList[holder.getPosition()];
              if (!checkView.isPressed()) {
                //没有被点击
                if (room.name) {
                  if (holder.itemView.roomList.getChildAt(0).getText().toString() === room.name) {
                    holder.itemView.roomList.getChildAt(0).checked = true;
                  } else if (holder.itemView.roomList.getChildAt(1).getText().toString() === room.name) {
                    holder.itemView.roomList.getChildAt(1).checked = true;
                  }
                } else {
                  holder.itemView.roomList.getChildAt(0).checked = false;
                  holder.itemView.roomList.getChildAt(1).checked = false;
                }
                return true;
              }
              if (checkView.checked) {
                room.name = checkView.getText().toString();
                toastLog("选中了" + room.name);
                log(room);
                log(roomList);
              } else {
                toastLog("没选中");
                room.name = "";
              }
            },
          });
    
          return holder;
        },
        onBindViewHolder: function (holder, position) {
          log("onBindViewHolder");
          // 数据绑定
          let room = roomList[position];
          holder.itemView.time.setText(room.time);
          if (room.name) {
            if (holder.itemView.roomList.getChildAt(0).getText().toString() === room.name) {
              holder.itemView.roomList.getChildAt(0).checked = true;
            } else if (holder.itemView.roomList.getChildAt(1).getText().toString() === room.name) {
              holder.itemView.roomList.getChildAt(1).checked = true;
            }
          } else {
            holder.itemView.roomList.getChildAt(0).checked = false;
            holder.itemView.roomList.getChildAt(1).checked = false;
          }
        },
        getItemCount: function () {
          return roomList.length;
        },
      });
    }
    
    5. 给recycleView设置adapter
    //设置Adapter
    recycleAdapter = createGiftBoxAdapter(roomList);
    recyclerView.setAdapter(recycleAdapter);
    
    6. 添加房间
    function addRoom() {
      log(arguments.callee.name);
      roomList.push(formatRoomInfo());
      ui.roomList.post(function () {
        ui.roomList.smoothScrollToPosition(roomList.length);
      });
      recycleAdapter.notifyDataSetChanged();
    }
    
    7. 格式化房间信息
    function addRoom() {
      log(arguments.callee.name);
      roomList.push(formatRoomInfo());
      ui.roomList.post(function () {
        ui.roomList.smoothScrollToPosition(roomList.length);
      });
      recycleAdapter.notifyDataSetChanged();
    }
    
    8. 控件随机背景色
    function getRndColor() {
      let color = new java.util.Random();
      return colors.toString((randomColor = colors.rgb(color.nextInt(256), color.nextInt(256), color.nextInt(256))));
    }
    

    参考文章

    声明

    部分内容来自网络

    给我个面子小图.jpg

    相关文章

      网友评论

          本文标题:listView记忆功能

          本文链接:https://www.haomeiwen.com/subject/mdxedltx.html