最终实现的效果图:
数据展示为每页10条数据,翻页按钮跟随数据的下方
在数据展示中:使用了 list(列表)界面布局,包裹card (卡片)布局
<list id="list">
<card w="*" h="60" margin="10 2 2 10" cardCornerRadius="2dp" cardElevation="1dp" foreground="?selectableItemBackground">
<horizontal gravity="center_vertical">
<View bg="#002EA6" h="*" w="10" />
<vertical padding="10 8" h="auto" w="0" layout_weight="1">
<linear>
<text text="{{name}}" textColor="#222222" textSize="16sp"/>
<text text="{{gender}}" padding="20 0" textColor="{{gender_color}}" textSize="14sp"/>
</linear>
<linear>
<text text="{{label}}" textColor="#222222" textSize="12sp"/>
<text text="id: {{short_id}}" padding="20 0" textColor="#222222" textSize="12sp"/>
<text text="粉丝: {{follower}}" padding="20 0" textColor="#222222" textSize="12sp"/>
</linear>
</vertical>
<button id="open_dy" marginLeft="4" marginRight="6" text="打开" style="Widget.AppCompat.Button.Colored" />
</horizontal>
</card>
</list>
数据正常展示后,跟随翻页按钮
<linear gravity="center" padding="10 20">
<button id="front_page">上一页</button>
<button id="next_page">下一页</button>
</linear>
这里的数据我选择了请求数据进行展示,翻页按钮的逻辑代码,
ui.front_page.on("click", ()=>{
page -= 1
// 避免页数为负数
if (page < 1){
page = 1
}
get_data() // 更新数据
});
ui.next_page.on("click", ()=>{
page += 1
get_data() // 更新数据
});
数据请求展示
threads.start(function(){
var url = 'http://****************/get_data?page=' + page;
var res = http.get(url);
if(res.statusCode == 200){
items = JSON.parse(res.body.string())
ui.run(function () {
ui.list.setDataSource(items);
});
}else{
toast("请求失败:" + res.statusMessage);
}
});
注释:网络阻塞请求是要使用线程来做处理的,但是在线程中并不能改变UI界面,就无法在请求到数据时更新页面,在网上找到了ui.run(function () {ui.list.setDataSource(items);});的方法,完美解决问题