先上效果图
本图为上拉到顶部实现数据加载
下图为下拉到底部,实现数据加载
下面展示代码,其中用到了三方开源控件 pulltoRefresh,朋友们可以在网上自行下载
public class MainActivity extends Activity implements OnScrollListener { private List<String> list = new ArrayList<String>(); private MyAdapter adapter; private Handler handle; private RelativeLayout pro; private Thread thread; private Thread threadUp; private static int positionUp = 0; private static int positionDown = 0; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final PullToRefreshListView lv = (PullToRefreshListView) findViewById(R.id.lv); pro = (RelativeLayout) findViewById(R.id.pro); pro.setOnClickListener(null); handle = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (msg.what == 1) { // 数据加载完毕,适配器刷新 adapter.notifyDataSetChanged(); // lv.setAdapter(adapter); pro.setVisibility(View.GONE); }else if (msg.what==2) { // 数据加载完毕,适配器刷新 adapter.notifyDataSetChanged(); lv.onRefreshComplete(); } } }; addLastList(); adapter = new MyAdapter(getBaseContext(), list); lv.setAdapter(adapter); lv.setOnScrollListener(this); lv.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // TODO Auto-generated method stub if (threadUp == null || !threadUp.isAlive()) { threadUp = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("-----XXXXX"); addFirstList(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handle.sendEmptyMessage(2); } }); threadUp.start(); } } }); } // 向集合尾部添加数据(代替数据加载功能) private void addLastList() { int len = positionDown + 1; for (int i = len; i < len + 30; i++) { list.add("测试数据" + i); positionDown++; } } // 向集合顶部添加数据 private void addFirstList() { int len = positionUp - 1; for (int i = len; i > len - 30; i--) { list.add(0, "测试数据" + i); positionUp--; } } class MyAdapter extends BaseAdapter { Context context; List<String> list; public MyAdapter(Context context, List<String> list) { // TODO Auto-generated constructor stub this.context = context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = View.inflate(getBaseContext(), R.layout.item, null); TextView tv = (TextView) convertView.findViewById(R.id.tv); tv.setText( list.get(position)); return convertView; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } } @Override public void onScroll(AbsListView arg0, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub // 当滑动到当前数据列表底部时,进行数据加载 if (firstVisibleItem + visibleItemCount == totalItemCount) { pro.setVisibility(View.VISIBLE); if (thread == null || !thread.isAlive()) { // 数据加载 thread = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // 数据加载 addLastList(); try { Thread.sleep(1000); handle.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); } } } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub } }
布局xml文件
activity_main.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" > <com.markupartist.android.widget.PullToRefreshListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/pro" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="数据加载中..." android:textColor="#7F007F" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
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/tv" android:layout_width="match_parent" android:layout_height="40dp" android:text="text" android:gravity="center" android:textSize="18sp" /> </LinearLayout>
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>