Android基础 为listView增加更多按钮(分页显示)

来源:互联网 发布:江西淘宝大学电话 编辑:程序博客网 时间:2024/06/05 20:05
package com.su.morebutton;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class TestListViewGetMoreButtonActivity extends Activity {private LinearLayout home_more;private TextView home_tvmore;private LinearLayout loading;private ListView home_ListView;private SimpleAdapter simpleAdapter;private ArrayList<HashMap<String, String>> mylist;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);home_more = (LinearLayout) LayoutInflater.from(TestListViewGetMoreButtonActivity.this).inflate(R.layout.list_more, null);home_tvmore = (TextView) home_more.findViewById(R.id.more_tv);loading = (LinearLayout) home_more.findViewById(R.id.loading);loading.setVisibility(View.GONE);home_ListView = (ListView) findViewById(R.id.listView1);home_ListView.addFooterView(home_more, null, false);//增加那个 更多按钮home_tvmore.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {home_tvmore.setVisibility(View.GONE);loading.setVisibility(View.VISIBLE);run = true;Thread thread = new Thread(new ThreadDemo());//开启获取新数据的线程thread.start();}});// 初始化listviewmylist = new ArrayList<HashMap<String, String>>();for (int i = 0; i < 7; i++) {HashMap<String, String> map = new HashMap<String, String>();map.put("ItemTitle", "This is Title.....");map.put("ItemText", "This is text.....");mylist.add(map);}simpleAdapter = new SimpleAdapter(this, mylist, R.layout.item,new String[] { "ItemTitle", "ItemText" }, new int[] {R.id.ItemTitle, R.id.ItemText });home_ListView.setAdapter(simpleAdapter);}Handler endSessionHandle = new Handler() {@Overridepublic void handleMessage(Message msg) {finish();}};private boolean run = true;class ThreadDemo implements Runnable {Message msg = handle.obtainMessage();public void run() {while (run) {msg.obj = getDate();msg.what = 1;handle.sendMessage(msg);run = false;}}}Handler handle = new Handler() {@SuppressWarnings("unchecked")public void handleMessage(Message msg) {if (msg.what == 1) {Log.e("测试", "");mylist.addAll((ArrayList<HashMap<String, String>>) msg.obj);simpleAdapter.notifyDataSetChanged();home_tvmore.setVisibility(View.VISIBLE);// 再次回到以前状态loading.setVisibility(View.GONE);}}};/** * 获取list的数据一般是来自网络 */private ArrayList<HashMap<String, String>> getDate() {ArrayList<HashMap<String, String>> newList = new ArrayList<HashMap<String, String>>();try {for (int i = 0; i < 5; i++) {// 执行的增加数据方法HashMap<String, String> map = new HashMap<String, String>();map.put("ItemTitle", "Added Title.....");map.put("ItemText", "added text.....");newList.add(map);if (i == 4) {}Thread.sleep(500);}} catch (Exception e) {e.printStackTrace();Log.e("WeiboPub", e.getMessage());}return newList;}}


http://dl.vmall.com/c0jnos5z6k


原创粉丝点击