Android ListView分页简单实现
Android ListView分页简单实现
分页,开发应用中必不可少。那么,现在就来实现分页功能。
首先来想想实现它要有哪些步骤,
1, 实现的组件,
2、初始化第一页数据,
3,底部布局 ,
4,加载数据的条件
5、获取下一页的数据。
有了思路,我们一步步来实现就行了。先来想想我们用什么组件实现,我们知道列表UI我们常用ListView或者RecyclerView,初始化数据,我们就在通过一个for循环来准备数据,底部布局我们直接使ProgressBar控件和一个TextView来显示就可以了。至于,加载的数据,我们就使用OnScrollListener来监听滑动事件,然后在满足加载条件时,我们就可以加载下一页的数据了。
先来看看ListView实现:
自定义ListView (LoadListView.Java):
public class LoadListView extends ListView implements AbsListView.OnScrollListener{ private LayoutInflater mInflater; //判断是否滚动最后一行 private boolean isLastRow = false; //底部View布局 private View mFooter; //实现接口加载更多数据 public OnLoadMoreListener moreListener; public void setLoadMoreListener(OnLoadMoreListener moreListener){ this.moreListener = moreListener; } public LoadListView(Context context) { super(context); initView(); } public LoadListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView(){ mInflater = LayoutInflater.from(getContext()); mFooter = mInflater.inflate(R.layout. listview_footer,null); this.addFooterView(mFooter); mFooter.setVisibility(View.GONE); setOnScrollListener(this); } @Override public void onScrollStateChanged(AbsListView absListView, int scrollState) { //正在滚动时回调,回调2-3次,手指没抛则回调2次。scrollState = 2的这次不回调 //回调顺序如下 //第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动 //第2次:scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作(手指离开屏幕前,用力滑了一下) //第3次:scrollState = SCROLL_STATE_IDLE(0) 停止滚动 //当屏幕停止滚动时为0;当屏幕滚动且用户使用的触碰或手指还在屏幕上时为1; //由于用户的操作,屏幕产生惯性滑动时为2 //当滚到最后一行且停止滚动时,执行加载 if(isLastRow&&scrollState== OnScrollListener.SCROLL_STATE_IDLE){ mFooter.setVisibility(View.VISIBLE); isLastRow = false; if(moreListener!=null){ moreListener.loadMore(); } } } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。 //firstVisibleItem:当前能看见的第一个列表项ID(从0开始) //visibleItemCount:当前能看见的列表项个数(小半个也算) //totalItemCount:列表项共数 //判断是否滚到最后一行 if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) { isLastRow = true; } } public interface OnLoadMoreListener{ void loadMore(); } }
底部布局listfooter.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/id_loadmore" android:layout_marginRight="5dp" /> <TextView android:id="@+id/id_loadmore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载更多" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> </RelativeLayout>
MainActivity.java的源码:
public class MainActivity extends Activity implements LoadListView.OnLoadMoreListener { private List<News> list = new ArrayList<>() ; private loadAdapter mAdapter; private LoadListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadMoreData(); mListView =(LoadListView) findViewById(R.id.id_list_view); mAdapter = new loadAdapter(this,list); mListView.setAdapter(mAdapter); mListView.setLoadMoreListener(this); } private void loadMoreData(){ for(int i = 0 ; i <10;i++){ News news = new News(); news.setTitle("许巍"); news.setContent("蓝莲花"); list.add(news); } } @Override public void loadMore() { new Handler().postDelayed(new Runnable() { @Override public void run() { loadMoreData(); showLoadMore(); } },2000); } private void showLoadMore(){ mAdapter.notifyDataSetChanged(); } }
LoadAdapter.java
public class loadAdapter extends BaseAdapter { private List<News> list; private Context mContext; public loadAdapter(Context context,List<News> list){ this.list = list; this.mContext = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView==null){ viewHolder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item_news,null); viewHolder.title = (TextView) convertView.findViewById(R.id.id_title); viewHolder.content = (TextView)convertView.findViewById(R.id.id_content); convertView.setTag(viewHolder); }else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.title.setText(list.get(position).getTitle()); viewHolder.content.setText(list.get(position).getContent()); return convertView; } public class ViewHolder{ private TextView title; private TextView content; } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <view.LoadListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/id_list_view" /> </LinearLayout>
item_news.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/id_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:gravity="center" android:padding="5dp"/> <TextView android:id="@+id/id_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:gravity="center" android:padding="5dp"/> </LinearLayout>
其实只要掌握了OnScrollListener的回调,那么就很简单了。RecyclerView也出现很久了,慢慢的,RecyclerView也会替代了ListView,所以ListView分页似乎就过时了,我们想知道的是RecyclerView怎么实现分页。但是我们知道RecyclerView不能实现头部和底部的添加,所以我们要自己来实现一个能添加头部和底部的ReyclerView。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)