autojs之RecycleView抽丝剥茧

简介: 作者: 牙叔使用情景: 解析RecycleView常用方法

作者:  牙叔


使用情景: 解析RecycleView常用方法


前置知识


  1. RecyclerView 复用 Item 的工作 Google 已经封装
  2. 自定义Adapter必须重写三个方法:
  • onCreateViewHolder
  • onBindViewHolder
  • getItemCount


了解每个方法的作用


  1. onCreateViewHolder 视图创建
  2. onBindViewHolder 数据绑定
  3. getItemCount 数据总数


类比


  1. 视图创建: 视图比作礼盒, 礼盒里面有一些格子, 规划成为一个一个固定的格子, 格子类型可以一样, 也可以不一样
  2. 数据绑定: 把棒棒糖, 瓜子, 花生这些零食放到格子里


adapter


adapter中包含holder, holder处理view

微信图片_20220624130601.jpg


代码分小段


1.主布局, 就一个RecyclerView

ui.layout(
  <vertical>
    <androidx.recyclerview.widget.RecyclerView id="recyclerView" h="*" w="*" />
  </vertical>
);


2. 两种礼盒布局

let redGiftBoxXml = (
  <card cardCornerRadius="10" cardElevation="10dp" margin="10">
    <horizontal bg="#88ff0000" padding="10" layout_width="wrap_content" layout_height="wrap_content">
      <img src="@drawable/ic_brightness_4_black_48dp"></img>
      <text>棒棒糖</text>
      <text id="lollipops"></text>
      <text>花生</text>
      <text id="melonSeeds"></text>
      <text>瓜子</text>
      <text id="peanut"></text>
    </horizontal>
  </card>
);
let blueGiftBoxXml = (
  <horizontal layout_width="match_parent" layout_height="wrap_content">
    <View layout_weight="1"></View>
    <card cardCornerRadius="10" cardElevation="6dp" margin="10">
      <horizontal bg="#550000ff" gravity="right" padding="10">
        <text>棒棒糖</text>
        <text id="lollipops">11111111111</text>
        <text>花生</text>
        <text id="melonSeeds"></text>
        <text>瓜子</text>
        <text id="peanut"></text>
        <img src="@drawable/ic_brightness_5_black_48dp"></img>
      </horizontal>
    </card>
  </horizontal>
);


3. adapter

function createGiftBoxAdapter(giftBoxList) {
  return RecyclerView.Adapter({
    onCreateViewHolder: function (parent, viewType) {
      // 视图创建
      let view;
      let holder;
      if (viewType === 0) {
        view = ui.inflate(redGiftBoxXml, parent, false);
        holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
      } else {
        view = ui.inflate(blueGiftBoxXml, parent, false);
        // Holder
        holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
      }
      return holder;
    },
    onBindViewHolder: function (holder, position) {
      // 数据绑定
      giftBox = giftBoxList[position];
      holder.itemView.lollipops.setText(giftBox.getLollipops());
      holder.itemView.melonSeeds.setText(giftBox.getMelonSeeds());
      holder.itemView.peanut.setText(giftBox.getPeanut());
    },
    getItemCount: function () {
      return giftBoxList.length;
    },
    getItemViewType: function (position) {
      return giftBoxList[position].getGiftType();
    },
  });
}


4. view有了, adapter也有了(holder在adapter中), 我们就可以加载数据了


5. 创建数据

let giftBoxList = [];
for (var i = 0; i < 10; i++) {
  giftBoxList.push(new GiftBox(i + "个", i * 2 + "斤", i * 3 + "袋"));
}


6. 加载数据

recycleAdapter = createGiftBoxAdapter(giftBoxList);
recyclerView.setAdapter(recycleAdapter);


7. 效果展示

微信图片_20220624104448.jpg


完整代码

"ui";
importClass(Packages.androidx.recyclerview.widget.RecyclerView);
importClass(Packages.androidx.recyclerview.widget.LinearLayoutManager);
ui.layout(
  <vertical>
    <androidx.recyclerview.widget.RecyclerView id="recyclerView" h="*" w="*" />
  </vertical>
);
// lollipops: // 棒棒糖
// melonSeeds: //瓜子
// peanut: //花生
// 过年买东西, 瓜子,花生,棒棒糖
function GiftBox(lollipops, melonSeeds, peanut) {
  this.lollipops = lollipops;
  this.melonSeeds = melonSeeds;
  this.peanut = peanut;
}
GiftBox.prototype.getLollipops = function () {
  return this.lollipops;
};
GiftBox.prototype.getMelonSeeds = function () {
  return this.melonSeeds;
};
GiftBox.prototype.getPeanut = function () {
  return this.peanut;
};
GiftBox.prototype.getGiftType = function () {
  return random(0, 1);
};
// 礼盒界面
let redGiftBoxXml = (
  <card cardCornerRadius="10" cardElevation="10dp" margin="10">
    <horizontal bg="#88ff0000" padding="10" layout_width="wrap_content" layout_height="wrap_content">
      <img src="@drawable/ic_brightness_4_black_48dp"></img>
      <text>棒棒糖</text>
      <text id="lollipops"></text>
      <text>花生</text>
      <text id="melonSeeds"></text>
      <text>瓜子</text>
      <text id="peanut"></text>
    </horizontal>
  </card>
);
let blueGiftBoxXml = (
  <horizontal layout_width="match_parent" layout_height="wrap_content">
    <View layout_weight="1"></View>
    <card cardCornerRadius="10" cardElevation="6dp" margin="10">
      <horizontal bg="#550000ff" gravity="right" padding="10">
        <text>棒棒糖</text>
        <text id="lollipops">11111111111</text>
        <text>花生</text>
        <text id="melonSeeds"></text>
        <text>瓜子</text>
        <text id="peanut"></text>
        <img src="@drawable/ic_brightness_5_black_48dp"></img>
      </horizontal>
    </card>
  </horizontal>
);
function createGiftBoxAdapter(giftBoxList) {
  return RecyclerView.Adapter({
    onCreateViewHolder: function (parent, viewType) {
      // 视图创建
      let view;
      let holder;
      if (viewType === 0) {
        view = ui.inflate(redGiftBoxXml, parent, false);
        holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
      } else {
        view = ui.inflate(blueGiftBoxXml, parent, false);
        // Holder
        holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
      }
      return holder;
    },
    onBindViewHolder: function (holder, position) {
      // 数据绑定
      giftBox = giftBoxList[position];
      holder.itemView.lollipops.setText(giftBox.getLollipops());
      holder.itemView.melonSeeds.setText(giftBox.getMelonSeeds());
      holder.itemView.peanut.setText(giftBox.getPeanut());
    },
    getItemCount: function () {
      return giftBoxList.length;
    },
    getItemViewType: function (position) {
      return giftBoxList[position].getGiftType();
    },
  });
}
let recyclerView = ui.recyclerView;
// //设置布局管理器
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
let giftBoxList = [];
for (var i = 0; i < 10; i++) {
  giftBoxList.push(new GiftBox(i + "个", i * 2 + "斤", i * 3 + "袋"));
}
//设置Adapter
recycleAdapter = createGiftBoxAdapter(giftBoxList);
recyclerView.setAdapter(recycleAdapter);











相关文章
|
开发工具
女朋友想要听歌,我反手用Flutter做了2个音乐播放器,给她拿捏了🎧
有很多小伙伴和我说,网上关于Flutter的音乐播放器资料太少了,我反手掉了10根头发给他们做了这样的音乐播放器,你就说得不得劲吧😎
女朋友想要听歌,我反手用Flutter做了2个音乐播放器,给她拿捏了🎧
|
Android开发
看清安卓自定义view中触摸事件的“盲区”
今天的工作仍然在思考自定义view,因为需要调服务器接口,而这需要一些参数去其他地方拿,不在我调自定义view的页面,所以我希望能封装好这个自定义view,对外只要开放相应监听接口即可
102 0
|
缓存 Android开发
聊聊RecyclerView新出的ConcatAdapter如何使用
聊聊RecyclerView新出的ConcatAdapter如何使用
聊聊RecyclerView新出的ConcatAdapter如何使用
|
Java 数据库 Android开发
Android性能优化指南
Android性能优化指南
166 0
|
XML Android开发 开发者
Android开发中基础动画技巧的应用(一)
Android开发中基础动画技巧的应用
136 0
Android开发中基础动画技巧的应用(一)
|
XML Android开发 数据格式
Android开发中基础动画技巧的应用(二)
Android开发中基础动画技巧的应用
142 0
Android开发中基础动画技巧的应用(二)
|
XML Android开发 开发者
Android开发中基础动画技巧的应用(三)
Android开发中基础动画技巧的应用
171 0
Android开发中基础动画技巧的应用(三)
|
机器学习/深度学习 Java Android开发
Android性能优化
针对Android的性能优化,主要有以下几个有效的优化方法: 1.布局优化 2.绘制优化 3.内存泄漏优化 4.响应速度优化 5.ListView/RecycleView及Bitmap优化 6.线程优化 7.其他性能优化的建议 下面我们具体来介绍关于以上这几个方面优化的具体思路及解决方案。
|
Java Android开发
PopupWindow 使用详解(二) Popwindow 制作常见花哨效果
PopupWindow 详解的第二篇,制作一些相对简单的效果。
4749 0