Android使用使用listview实现分页刷新(线程休眠模拟)实现分页刷新(线程休眠模拟)
当要显示的数据过多时,为了更好的提升用户感知,在很多APP中都会使用分页刷新显示,比如浏览新闻,向下滑动到当前
ListView的最后一条信息(item)时,会提示刷新加载,然后加载更新后的内容。此过程大致分以下几步:
1.当前Activity implements OnScallListenner;
2.实现接口的方法;
3.ListView注册滚动监听;
4. Adapter(自定义或者安卓自带)为每个item填充数据;
5.获得第二页以后的数据后,adater增加数据并刷新notifyDateSetChanged();(需要用到Handler)
现在我们就通过线程休眠的的方式模拟ListView页面刷新的实现(每次加载10条信息,向下滑动会分页刷新加载)
显示效果(设置显示十条后开启刷新,添加使用AlertDialog浏览示例):
Layout中ListView布局activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.day08.MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Layout中item(填充ListView每行)布局item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Tile"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<TextView
android:text="Message"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
</LinearLayout>