简单模拟ListView自动加载。

java代码:

package com.example.testlistview;import java.util.ArrayList;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private static final String tag = MainActivity.class.getSimpleName();ArrayAdapter<CharSequence> aa = null;private int state = 0; // 0:等待, 1:正在提取数据并更新ListView lview = null;View footview = null;ArrayList<String> buff = new ArrayList<String>();private int count = 0;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.init();Log.w(tag, "init: state value[" + state + "]");}private void init() {ArrayList<CharSequence> data = new ArrayList<CharSequence>();for (int k = 0; k < 15; k++) {data.add("hello - " + k);}lview = (ListView) this.findViewById(R.id.listview);aa = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1, data);LayoutInflater ll = this.getLayoutInflater();footview = ll.inflate(R.layout.lfootview, null);lview.addFooterView(footview);final MyHandler myHandler = new MyHandler();lview.setOnScrollListener(new OnScrollListener() {private boolean state_idle = false;public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {if (!this.state_idle)return;// Log.w(tag,// "firstVisibleItem["+firstVisibleItem+"], visibleItemCount["+visibleItemCount+"], totalItemCount["+totalItemCount+"]");if (firstVisibleItem == 0|| (firstVisibleItem + visibleItemCount) != totalItemCount) {return;}Log.w(tag, "提取数据 ..., count[" + count + "]");state = 1;MyThread mth = new MyThread(myHandler);Thread th = new Thread(mth);th.start();}public void onScrollStateChanged(AbsListView view, int scrollState) {Log.w(tag, "scroll_state[" + scrollState + "], state[" + state+ "]");if (OnScrollListener.SCROLL_STATE_IDLE != scrollState) {return;}if (0 != state)return;this.state_idle = true;}});lview.setAdapter(aa);}public void updateData() {Log.w(tag, "更新数据 ...");Log.w(tag, "删除多余数据 .");while (aa.getCount() > 45) {aa.remove(aa.getItem(0));}for (int k = 0; k < buff.size(); k++) {aa.add(buff.get(k));}this.aa.notifyDataSetChanged();state = 0;Log.w(tag, "更新数据完毕 .");count++;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}class MyThread implements java.lang.Runnable {private MyHandler handler;public MyThread(MyHandler handler) {this.handler = handler;}public void run() {for (int k = 0; k < 5; k++) {String value = "hello - " + (aa.getCount() + 1);buff.add(value);Log.w(tag, "value[" + value + "]");try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}}Message msg = Message.obtain();Bundle b = new Bundle();b.putInt("result", 200);msg.setData(b);handler.sendMessage(msg);Log.w(tag, "消息发送完毕 .");}}class MyHandler extends Handler {@Overridepublic void handleMessage(Message msg) {int result = msg.getData().getInt("result");Log.w(tag, "result:" + result);if (200 == result) {MainActivity.this.updateData();}super.handleMessage(msg);}}}

二、布局文件:

1、activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        android:layout_gravity="center"        tools:context=".MainActivity" />    <RelativeLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:layout_marginTop="5dp"        >        <ListView             android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:id="@+id/listview"            android:background="@android:color/transparent"            ></ListView>            </RelativeLayout>        </LinearLayout>

2、lfootview.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="100dp"     >       <ProgressBar        android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_centerInParent="true"       style="?android:attr/progressBarStyleInverse"       /></RelativeLayout>

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. DataBinding
  4. Android获取包相关信息--code
  5. Android(安卓)SQLiteDatabase的使用
  6. android SQLite数据库基本操作示例
  7. android中的状态保存
  8. Android动态添加删除recycleview并动态保存recycleview中的的数
  9. Android异步线程OkHttp Post请求Json数据并解析

随机推荐

  1. 文件的读写操作
  2. Android设计和开发系列第二篇:Action Bar(
  3. Android获取本机电话号码的简单方法
  4. android 中禁止输入特殊字符小结
  5. Android(安卓)Binder进程间通信-ServiceM
  6. 升级到Android(安卓)Studio 3.2.1 ,报 Cou
  7. Android(安卓)Fragment基础之动态添加Fra
  8. android 图片文字轮播效果(图片和文字自动
  9. Android之计时器(Chronometer)的用法
  10. Android(安卓)Gallery3D效果 教程 案例