展会信息港展会大全

android listview 自动加载
来源:互联网   发布日期:2015-11-26 10:21:11   浏览:870次  

导读:简单模拟ListView自动加载。java代码:package com.example.testlistview;import java.util.ArrayList;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.ap......

简单模拟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;

@Override

public 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++;

}

@Override

public 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 {

@Override

public 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>

赞助本站

人工智能实验室

相关热词: 自动加载

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港