Android ListView分页,动态添加数据

时间:2022-07-04 19:29:07

1.ListView分页的实现,重点在于实现OnScrollListener接口,判断滑动到最后一项时,是否还有数据可以加载,

我们可以利用listView.addFootView(View v)方法进行提示

自定义一个ListView(这里本来想进行一些自定已修改的。。。实际代码并没有做)CornerListView

package com.example.testlistview;

import android.widget.AbsListView;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView; public class CornerListView extends ListView{ public CornerListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
} public CornerListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} public CornerListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}

包含ListView的配置文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testlistview.MainActivity" > <com.example.testlistview.CornerListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dip"
android:id="@+id/list"
>
</com.example.testlistview.CornerListView> </RelativeLayout>

ListView每一项的Item配置文件  listview_item.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/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"
android:gravity="center_horizontal"
></TextView>
</LinearLayout>

有更多数据时的配置文件more.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="match_parent" >
<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="更多"
/>
<ProgressBar
android:id="@+id/loading"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
/>
</RelativeLayout>

CorernerListView对应的Adapter适配器 CornerAdapter.java

package com.example.testlistview;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; public class CornerAdapter extends BaseAdapter{ LayoutInflater inflater;
public List<String> list;
private Context context; public CornerAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(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(context).inflate(R.layout.listview_item, null, false);
TextView tv1= (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tv = tv1;
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(list.get(position));
return convertView;
} class ViewHolder{
public TextView tv;
}
}

主Activity代码:

package com.example.testlistview;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView; public class MainActivity extends Activity implements OnScrollListener{ private int pageIndex = 1;
private int pageSize = 2;
private int pageCount = 2;//初始设置页数为2,因为刚开始不知道总页数
private CornerListView listView;
private CornerAdapter adapter;
private View footer,tvMsg,loading;
private boolean is_divPage = false;//是否分页
private List<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (CornerListView) findViewById(R.id.list);
listView.setOnScrollListener(this);
footer = View.inflate(this, R.layout.more, null);
tvMsg = footer.findViewById(R.id.tv_msg);
loading = footer.findViewById(R.id.loading);
listView.addFooterView(footer);//添加footerview
Thread thread = new Thread(runnable);//第一次访问服务器接口,返回第一页信息,并确定总页数信息
thread.start();
} //异步访问网络加载数据之类的。。
Runnable runnable = new Runnable() { @Override
public void run() {
//传递pageIndex,pageSize,URL等参数访问网络获取数据,这里省略。。。
//doSomething
String strJson = "";//假定strJson就是服务器返回的这一页Json数据
//假设这里返回总页数为5
pageCount = 5;
Message msg = new Message();
msg.obj = strJson;
handler.sendMessage(msg);//将消息返回给UI }
}; //处理返回的消息,设置ListView
Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
String strJson = msg.obj.toString();
//各种转换组合数据,这里我们就不做了,模拟一下数据
for(int i=0;i<pageSize; ++i)
{
if(adapter != null && adapter.list != null)//从获取第二页开始,直接修改适配器的list
{
adapter.list.add(i+"");
}
else//第一次适配器还没有构造
{
list.add(i+"");
}
}
if(pageIndex == 1)//第一次,设置适配器
{
adapter = new CornerAdapter(list, MainActivity.this);
listView.setAdapter(adapter);
//判断一下是否显示更多按钮
if(pageIndex == pageCount)
{
listView.removeFooterView(footer);
}
else if(pageIndex < pageCount)
{
//设置tvmsg显示,加载动画不显示
tvMsg.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
}
else
{
//直接调用方法更新数据
adapter.notifyDataSetChanged();
if(pageIndex == pageCount)
{
listView.removeFooterView(footer);
}
else if(pageIndex < pageCount)
{
//设置tvmsg显示,加载动画不显示
tvMsg.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
} } }; @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//需要分页,触摸停止,当前确实还有分页数据
if(is_divPage && scrollState == OnScrollListener.SCROLL_STATE_IDLE && pageIndex < pageCount)
{
pageIndex += 1;//设置请求当前页码加1
Thread thread = new Thread(runnable);
thread.start();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//现在 Bug初始情况下向下滑动也能翻页
int lastVisibleItem = firstVisibleItem + visibleItemCount;//当前滑动到最后一项
if(lastVisibleItem == totalItemCount && totalItemCount > 0)
{
is_divPage = true;
}
}
}