当每次界面滑动时,获取androidx.recyclerview.widget.RecyclerView 逐个framelayout,下面 id 为 tv_text textview的text. 和推荐的商品信息
回答
RecyclerView 滑动获取商品信息的实现方案
核心思路
- 定位 RecyclerView 控件
- 遍历每个 FrameLayout item
- 在每个 item 中查找指定 id 的 TextView
- 滑动页面并重复获取
完整代码示例
// 文档地址 https://docs.hamibot.com/
// 获取 RecyclerView 控件
var recyclerView = className('androidx.recyclerview.widget.RecyclerView')
.findOne();
if (recyclerView != null) {
// 获取当前可见的 item 数量
var itemCount = recyclerView.childCount();
// 遍历每个 item
for (var i = 0; i < itemCount; i++) {
var item = recyclerView.getChild(i);
// 在 item 中查找 id 为 tv_text 的 TextView
var textView = item.findOne(id('tv_text'));
if (textView != null) {
var text = textView.text();
log('第' + i + '个商品:' + text);
// 获取其他商品信息(根据实际控件调整)
var priceView = item.findOne(id('tv_price'));
if (priceView != null) {
log('价格:' + priceView.text());
}
}
}
// 向下滑动加载更多
recyclerView.scrollForward();
sleep(1000);
}
持续滑动获取所有商品
// 文档地址 https://docs.hamibot.com/
var recyclerView = className('androidx.recyclerview.widget.RecyclerView')
.findOne();
var maxScroll = 10; // 最大滑动次数
var scrollCount = 0;
var lastTextList = [];
while (scrollCount < maxScroll) {
var currentTextList = [];
var itemCount = recyclerView.childCount();
for (var i = 0; i < itemCount; i++) {
var item = recyclerView.getChild(i);
var textView = item.findOne(id('tv_text'));
if (textView != null) {
var text = textView.text();
currentTextList.push(text);
log('商品:' + text);
}
}
// 判断是否还有新内容
if (currentTextList.length == lastTextList.length) {
var isSame = true;
for (var j = 0; j < currentTextList.length; j++) {
if (currentTextList[j] != lastTextList[j]) {
isSame = false;
break;
}
}
if (isSame) {
log('已到达列表底部');
break;
}
}
lastTextList = currentTextList;
recyclerView.scrollForward();
sleep(1000);
scrollCount++;
}
关键 API 说明
| 方法 | 说明 |
|---|---|
className() |
选择指定类名的控件 |
findOne() |
查找第一个匹配的控件 |
childCount() |
获取子控件数量 |
getChild(i) |
获取第 i 个子控件 |
id() |
通过资源 id 选择控件 |
text() |
获取控件文本内容 |
scrollForward() |
向前滑动列表 |
注意事项
- 控件类名简化:
androidx.recyclerview.widget.RecyclerView不能省略前缀,只有android.widget.开头的类名才能省略 - 滑动等待:每次滑动后需要
sleep()等待内容加载 - 空值判断:获取控件前要判断是否为 null
- 终止条件:需要设置最大滑动次数或检测是否到达列表底部
- 兼容性:代码使用 Rhino 1.7.15 兼容的语法,避免箭头函数和默认参数
调试建议
- 使用
log()输出每一步的执行结果 - 先用
className('RecyclerView').findOne()确认能找到列表 - 使用
item.children()查看 item 内所有控件结构 - 确认
tv_text的 id 是否正确,可用item.findOne(textContains('关键词'))验证
ℹ️ 本回答由 AI 生成,内容仅供参考