android开发笔记之 向ListView添加一个页脚

你也许见过有的软件的列表界面当你拖动到最后一列的时候会出现一个“正在加载…”或者“更多”的按钮。然后列表会自动加载更多内容病自动显示。这是怎么实现的呢?原来就是在ListView的底部添加了一个页脚,用的是addFooterView().

怎么实现呢?首先,我们需要准备一些布局文档。分别有listview_xml(列表),listview_playlist_xml(列表布局),listview_foot_xml(列表页脚)

listview_xml 如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView
    	android:id="@+id/listView"
    	android:layout_height="wrap_content"
    	android:layout_width="fill_parent">
    </ListView>
</LinearLayout>

listview_playlist_xml 如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:id="@+id/line"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView
    	android:text="name"
    	android:textSize="20dip"
    	android:textStyle="bold"
    	android:id="@+id/name_textView"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content">
    </TextView>
    <TextView
    	android:text="number"
    	android:id="@+id/number_textView"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

listview_foot_xml 如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:gravity="center"
  android:id="@+id/loading_line"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <LinearLayout
    	android:gravity="center"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content" >
        <ProgressBar
        	android:layout_width="wrap_content"
        	android:id="@+id/progressBar1"
        	android:layout_height="wrap_content"
        	style="?android:attr/progressBarStyleSmall">
        </ProgressBar>
        <TextView
        	android:text="正在加载..."
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content">
        </TextView>
    </LinearLayout>
</LinearLayout>

MListViewActivity.java 如下

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MListViewActivity extends Activity {
	/** Called when the activity is first created. */

	private ArrayList> ListItem = new ArrayList>();
	private SimpleAdapter listAdap;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview_xml);

		ListView mListView = (ListView) findViewById(R.id.listView);
		LayoutInflater infla = LayoutInflater.from(this);
		//载入列表布局
		infla.inflate(R.layout.listview_playlist_xml,
				(ViewGroup) mListView.getEmptyView());
		//这里就是向列表添加一个页脚的关键语句了
		View foot = getLayoutInflater().inflate(R.layout.listview_foot_xml, null);
		mListView.addFooterView(foot);
		for (int i = 0; i < 20; i++) {
			HashMap map = new HashMap();
			map.put("name", "名字" + i);
			map.put("number", "号码" + i);
			ListItem.add(map);
		}
		//适配器,没什么好解释的
		listAdap = new SimpleAdapter(this, ListItem,
				R.layout.listview_playlist_xml,
				new String[] { "name", "number" }, new int[] {
						R.id.name_textView, R.id.number_textView });
		mListView.setAdapter(listAdap);
	}
}

好了,列表底部的页脚已经添加完毕了。当你滑动到列表的底端的时候就可以看到一个在旋转的圆圈和提示文字了。效果如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

当然,显示一个页脚是不够的,我们还要配合多线程实现当滑动到页脚的时候自动加载内容。这我们就要用上ListView的setOnScrollListener来监听ListView的滑动。
对MListViewActivity.java的修改如下:

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MListViewActivity extends Activity {
	/** Called when the activity is first created. */

	private ArrayList> ListItem = new ArrayList>();
	private SimpleAdapter listAdap;
	private ListView mListView;
	private View foot;
	private Thread newThread;
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case 1:
				listAdap.notifyDataSetChanged();
				break;
			case 2:
				mListView.removeFooterView(foot);
				break;
			}
			super.handleMessage(msg);
		}

	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview_xml);

		mListView = (ListView) findViewById(R.id.listView);
		LayoutInflater infla = LayoutInflater.from(this);
		// 载入列表布局
		infla.inflate(R.layout.listview_playlist_xml,
				(ViewGroup) mListView.getEmptyView());
		// 这里就是向列表添加一个页脚的关键语句了
		foot = getLayoutInflater().inflate(R.layout.listview_foot_xml, null);
		mListView.addFooterView(foot);
		for (int i = 0; i < 20; i++) {
			HashMap map = new HashMap();
			map.put("name", "名字" + i);
			map.put("number", "号码" + i);
			ListItem.add(map);
		}
		// 适配器,没什么好解释的
		listAdap = new SimpleAdapter(this, ListItem,
				R.layout.listview_playlist_xml,
				new String[] { "name", "number" }, new int[] {
						R.id.name_textView, R.id.number_textView });
		mListView.setAdapter(listAdap);
		// 监听mListView
		mListView.setOnScrollListener(new OnScrollListener() {

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				if (firstVisibleItem + visibleItemCount == totalItemCount) {
					if (newThread == null || !newThread.isAlive()) {
						newThread = new newThread();
						newThread.start();
					}
				}

			}

			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

			}

		});
	}

	private void ListViewMore() {
		// TODO Auto-generated method stub
		Message msg = new Message();
		if (ListItem.size() < 50) {
			msg.what = 1;
			for (int i = 20; i < 50; i++) {
				HashMap map = new HashMap();
				map.put("name", "名字" + i);
				map.put("number", "号码" + i);
				ListItem.add(map);

			}
		} else {
			msg.what = 2;
		}
		mHandler.sendMessage(msg);
	}

	public class newThread extends Thread{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				Thread.sleep(3000);
				ListViewMore();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

以上是android菜鸟的心得,纯属写下来怕以后自己忘了。呵呵,高手莫拍啊。提供实例下载
mListView

爱邮信android版1.0版塞班论坛发布了

以前在塞班平台做开发的时候的一个创意,利用139邮箱的免费短信提醒来实现免费发短信的软件爱邮信,感觉移动这个东西还是很不错的。
最近正好在学习android开发,便拿这个软件来练练手,通过这个软件的开发也多少掌握些了android的开发门路了。不过也只限于一些api。其它没有涉及到的就没有什么成果了。
软件使用也比较简单,只要添加了一个联系人就可以了,前提是该联系人开通了139邮箱,并设置了新邮件短信提醒。
软件的聊天界面也采用了气泡聊天的界面,感觉android的这个功能很不错。
本软件比较适合情侣用吧,不过现在大家都用飞信了。就是飞信有时候让人很蛋疼。

Q围脖1.3.0beta公测版发布啦~!

经过一段时间的内测,Q围脖终于通过了新浪官方的审核,而且软件也趋于稳定了,所以开放公测了,Q围脖主页也开放注册了。详情请前往Q围脖主页:http://ifetion.dospy.com/weibo/

Q围脖客户端内测版打包完成

Q围脖客户端内测版已经打包完成了,在Q围脖主页已经可以下载了,但是,我暂时关闭了自主注册,如果想参加内测的,可以私密我你要注册的帐号和注册邮箱,我后台开号,可以发邮件给我:liurenqiu520@gmail.com

Q微博的注册和登录页面做好了

Q微博的web端基本上已经做好了,以后再慢慢的优化,现在开放注册啦!欢迎大家踊跃注册啊,应用会尽快上线的!
点此前往注册

征集Q微博客户端开发人员

Q微博的网络部分基本上已经搞定了,用的新浪微博的aouth认证方式,现征集客户端开发人员,有熟悉java,j2me,python for s60和mshell的开发人员有兴趣的可以联系我,我的联系邮箱:liurenqiu520@gmail.com

更新一下博客…

博客的访问量已经趋向0了…
自从没做爱飞信之后就几乎没人再来看望我了…
也不是我不想再做爱飞信了,只是移动升级了协议之后,新协议的图片验证码问题一直攻克不了,而wap飞信协议又面临很严重的获取列表缺失和不完整的现象,协议问题搞不定,客户端做出来也没意义,听说移动过段时间也会开放飞信平台,到时候再看看能不能再做爱飞信吧!
最近这两天开始研究起新浪微博来了,费了两天的功夫,也多少有了些成果了,弄了一个叫Q微博的应用,接口方面,自己折腾了一天,加上新浪提供的SDK已经搞定了Oauth验证大关了,其他的接口也就随之搞定了…
客户端方面,折腾了两天也简单的做了一个,只有发微博和发图片微博的功能,发图也有相机拍照和本地图片两种…等明天把接口再对应到客户端,就基本上大功告成了…
新浪微博放弃了HTTP普通验证,抛弃了传统的输入用户名和密码登陆的方式,美其名曰保护用户隐私,给桌面应用和手机应用带来了很大的不便!
Aouth认证有一个步骤是要通过网页跳转到新浪微博的应用授权网站的,所以必须是web访问,授权成功以后获得两个重要的值,就这两个值就是后面所有接口需要的值,这两个值也是属于你的微博的唯一值。然而要获取到这两个值就必须web访问,显然手机应用客户端不太可能做到,除非内嵌一个web浏览器,这太费功夫了。所以我采用的方法是网页注册…
我自己建了一个Q微博网页,接受用户注册,数据库是跟爱飞信官网同一个数据库,但是我新建了个表,新用户注册登陆后,在网页上执行了一次Aouth认证以后,那两个重要值就保存到数据库了,使用我的手机客户端的时候只要填写你的Q微博账号和密码就可以发布微博了…
虽然这样麻烦了点,但是为了响应新浪保护用户隐私的号召只能这样做了…呵呵…
欢迎大家踊跃使用我的应用…
明天再优化下Q微博网站的登陆和注册页面就开放注册,应用程序争取早日上线,敬请期待…

【DDC狂人组小球球出品】计划任务v1.02 测试版

初学mshell语言,编写了一款工具类的小软件。计划任务是一款可以定时执行某项计划的软件,比如定时启动和关闭某程序,并在启动后自动执行设定好的按键动作,完成自动启动软件并操作再关闭的流水作业。1.02测试版加入了定时发送短信的功能。

由于明天要去石家庄实习了,所以软件开发的比较匆忙,可能存在比较多的BUG,发现BUG可以回帖告诉我,欢迎大家踊跃测试测试!

本软件非Py软件,不需要什么平台的支持。自签名或者XX手机安装即可。

点此下载

现在真的很忙…

等寒假了,一定给各位爱飞信的粉丝一个交代,现在真的很忙,烦死我了最近….

工作的事总算是搞定了…

今天湘电集团盖好公章的就业协议书邮寄过来了,我的工作也终于签订完毕了…以后就是湘电集团的人了