WP7 DataBound ListBox中针对UIElement的事件绑定

在很多使用DataBound的ListBox案例中,我们都监听了它的 SelectionChanged 事件,当我们用手指点击某一项时,可以从 ListBox.SelectedItem 属性上很容易获得这个被点击的对象。然而,万一你的ListBox的单项里面有很多类似于 Button, TextBlock 这样的控件,而你刚好又要捕获这些控件的点击事件时,那你该这么做?不过我将在下面的文章中谈谈一个简单的解决方法。
为了演示,先创建一个简单的 Person 类:
public class Person
{
 public Person(string firstName, string lastName, int age)
 {
  FirstName = firstName;
  LastName = lastName;
  Age = age;
 }

 public string FirstName { get; set; }
 public string LastName { get; set; }
 public int Age { get; set; }

 public override string ToString()
 {
  return LastName + ", " + FirstName + "{" + Age + "}";
 }
}
接着来写XAML布局代码,先添加一个3列的ListBox,如下代码所示。
<phone:PhoneApplicationPage
    x:Class="DataboundMultiListBoxSelection.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <ListBox x:Name="PeopleListBox" SelectionChanged="PeopleListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150" />
                            <ColumnDefinition Width="150" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Button Grid.Column="0" Content="{Binding FirstName}" Click="FirstNameButton_Click" />
                        <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding LastName}" MouseLeftButtonUp="LastNameTextBlock_MouseLeftButtonUp" />
                        <Rectangle Grid.Column="2" Width="36" Height="36" Fill="Red" MouseLeftButtonUp="AgeRect_MouseLeftButtonUp" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</phone:PhoneApplicationPage>
然后在C#后台代码中,我们在Page_Load事件中为ListBox添加数据。
public partial class MainPage : PhoneApplicationPage
{
 // Constructor
 public MainPage()
 {
  InitializeComponent();
  Loaded += new RoutedEventHandler(MainPage_Loaded);
 }

 void MainPage_Loaded(object sender, RoutedEventArgs e)
 {
  List<Person> people = new List<Person>();

  people.Add(new Person("Tim", "Mgee", 36));
  people.Add(new Person("Frank", "Solo", 77));
  people.Add(new Person("Hanna", "Jones", 77));

  PeopleListBox.ItemsSource = people;
 }
运行程序,结果如下图所示:
接着。为Button实现点击事件的处理:
{
 Person selectedPerson = ((sender as Button).DataContext as Person);
 MessageBox.Show("First Name Button clicked: " + selectedPerson.FirstName);
}
比较有意思的是我们这里首先将 sender 强制转换成 Button, 然后获取 Button.DataContext 属性。问题来了,什么事 DataContext? 当你将数据绑定到ListBox时,每一项被分配一个 DataContext 数据来表示绑定的单项数据。在今天这个范例中,DataContext对应的是每一个Person。而且DataContext我们可以称为路由属性,在ListBoxItem的每一个子控件中都可以被获取。所以这也是为什么Button.DataContext就是我们所需要的Person的原因。
好了,现在你点击任何按钮,比如点击这个”Tim”按钮,我们将能看到下图:
OK,现在来看看每一项的TextBlock以及Rectangle被点击后的事件处理:
private void LastNameTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
 Person selectedPerson = ((sender as TextBlock).DataContext as Person);
 MessageBox.Show("Last Name TextBlock clicked: " + selectedPerson.LastName);

 e.Handled = true;
}

private void AgeRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
 Person selectedPerson = ((sender as Rectangle).DataContext as Person);
 MessageBox.Show("Age Rectangle clicked: " + selectedPerson.Age);

 e.Handled = true;
}
注意我们这里设置 Handled 属性为true,这一点与Button的点击事件处理可不一样,因为 MouseLeftButtonUp 事件如果不手动停止的话,会无限路由下去。所以当然要设置 Handled = true 来标记该事件已经完成。
 
最后,我们看看ListBox.SelectionChanged事件的做法。这里的处理是捕获所有的没有点击到按钮,TextBlock以及Rectangle的点击事件。
private void PeopleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 if (PeopleListBox.SelectedIndex == -1)
  return;

 Person selectedPerson = ((sender as ListBox).SelectedItem as Person);
 MessageBox.Show("ListBox selected: " + selectedPerson);

 PeopleListBox.SelectedIndex = -1;
}
注意上面代码一开始去检测 SelectedIndex 是否为 -1 ,而且最后还强行设置 SelectedIndex = -1,如果你不这样做,那么SelectionChanged事件在你触碰到ListBox后将不会按照顺序被触发。即Selection不会被改变。那么我们将看到下图这个样子。
原文转载自:http://www.oschina.net/question/213217_52095

wp7开发资源汇总

没有评论

本文将Windows Phone 7常用的资源进行了整理,方便大家使用。后续将会经常更新其中的资源,大家如果有好的资源请留言,我好编辑到本文中。

1.工具下载

在线安装包

离线安装包(推荐)

2.微软官方资料

Windows Phone 7中文开发中心

MSDN Windows Phone首页

微软Windows Phone Portal

微软Windows Phone Home

Windows Phone Newsroom

Windows Phoen团队博客

Silverlight for Windows Phone

Expression Blend 4 for Windows Phone

Windows Phone开发者网站

MSDN在线帮助文档

Windows Phone 7 开发者训练包

Windows Phone 7 开发者训练包(中文版)

 示例代码

Windows Phone 7 Jump Start视频教程(英文)

Inside Windows Phone视频教程(英文)

Microsoft Silverlight 4 脱机文档(简体中文)

Microsoft Silverlight 4 在线帮助文档(MSDN)(简体中文)

 3.论坛

MSDN Windows Phone 7论坛(英文)

MSDN Windows 移动设备论坛(中文)

Windows Phone论坛

Windows Phone资讯站

4.WP7常用站点

Codewp7 卤面网

智机网

Codeplex Windows Phone 7

Windows Phone 7 – CodeProject

 

转载自:http://www.cnblogs.com/sonyye/archive/2012/02/26/2368467.html

android中action大全

2 条评论

String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式。. “android.intent.action.ADD_SHORTCUT”

String ALL_APPS_ACTION 动作:列举所有可用的应用。

输入:无。 “android.intent.action.ALL_APPS”

String ALTERNATIVE_CATEGORY 类别:说明 activity 是用户正在浏览的数据的一个可选操作。 “android.intent.category.ALTERNATIVE”

String ANSWER_ACTION 动作:处理拨入的电话。 “android.intent.action.ANSWER”

String BATTERY_CHANGED_ACTION 广播:充电状态,或者电池的电量发生变化。 “android.intent.action.BATTERY_CHANGED”

String BOOT_COMPLETED_ACTION 广播:在系统启动后。
这个动作被广播一次(只有一次)。 “android.intent.action.BOOT_COMPLETED”

String BROWSABLE_CATEGORY 类别:能够被浏览器安全使用的 activities 必须支持这个类别。 “android.intent.category.BROWSABLE”

String BUG_REPORT_ACTION 动作:显示 activity 报告错误。 “android.intent.action.BUG_REPORT”

String CALL_ACTION 动作:拨打电话。
被呼叫的联系人在数据中指定。 “android.intent.action.CALL”

String CALL_FORWARDING_STATE_CHANGED_ACTION 广播:语音电话的呼叫转移状态已经改变。 “android.intent.action.CFF”

String CLEAR_CREDENTIALS_ACTION 动作:清除登陆凭证 (credential)。 “android.intent.action.CLEAR_CREDENTIALS”

String CONFIGURATION_CHANGED_ACTION 广播:设备的配置信息已经改变,参见 Resources.Configuration. “android.intent.action.CONFIGURATION_CHANGED”

Creator CREATOR 无 无

String DATA_ACTIVITY_STATE_CHANGED_ACTION 广播:电话的数据活动(data activity)状态(即收发数据的状态)已经改变。 “android.intent.action.DATA_ACTIVITY”

String DATA_CONNECTION_STATE_CHANGED_ACTION 广播:电话的数据连接状态已经改变。 “android.intent.action.DATA_STATE”

String DATE_CHANGED_ACTION 广播:日期被改变。 “android.intent.action.DATE_CHANGED”

String DEFAULT_ACTION 动作:和 VIEW_ACTION 相同,是在数据上执行的标准动作。 “android.intent.action.VIEW”

String DEFAULT_CATEGORY 类别:如果 activity 是对数据执行确省动作(点击, center press)的一个选项,需要设置这个类别。 “android.intent.category.DEFAULT”

String DELETE_ACTION 动作:从容器中删除给定的数据。 “android.intent.action.DELETE”

String DEVELOPMENT_PREFERENCE_CATEGORY 类别:说明 activity 是一个设置面板 (development preference panel). “android.intent.category.DEVELOPMENT_PREFERENCE”

String DIAL_ACTION 动作:拨打数据中指定的电话号码。 “android.intent.action.DIAL”

String EDIT_ACTION 动作:为制定的数据显示可编辑界面。 “android.intent.action.EDIT”

String EMBED_CATEGORY 类别:能够在上级(父)activity 中运行。 “android.intent.category.EMBED”

String EMERGENCY_DIAL_ACTION 动作:拨打紧急电话号码。 “android.intent.action.EMERGENCY_DIAL”

int FORWARD_RESULT_LAUNCH 启动标记:如果这个标记被设置。
而且被一个已经存在的 activity 用来启动新的 activity,已有 activity 的回复目标 (reply target) 会被转移给新的 activity。 16 0×00000010

String FOTA_CANCEL_ACTION 广播:取消所有被挂起的 (pending) 更新下载。 “android.server.checkin.FOTA_CANCEL”

String FOTA_INSTALL_ACTION 广播:更新已经被确认,马上就要开始安装。 “android.server.checkin.FOTA_INSTALL”

String FOTA_READY_ACTION 广播:更新已经被下载。
可以开始安装。 “android.server.checkin.FOTA_READY”

String FOTA_RESTART_ACTION 广播:恢复已经停止的更新下载。 “android.server.checkin.FOTA_RESTART”

String FOTA_UPDATE_ACTION 广播:通过 OTA 下载并安装操作系统更新。 “android.server.checkin.FOTA_UPDATE”

String FRAMEWORK_INSTRUMENTATION_TEST_CATEGORY 类别:To be used as code under test for framework instrumentation tests. “android.intent.category.FRAMEWORK_INSTRUMENTATION _TEST”

String GADGET_CATEGORY 类别:这个 activity 可以被嵌入宿主 activity (activity that is hosting gadgets)。 “android.intent.category.GADGET”

String GET_CONTENT_ACTION 动作:让用户选择数据并返回。 “android.intent.action.GET_CONTENT”

String HOME_CATEGORY 类别:主屏幕 (activity)。
设备启动后显示的第一个 activity。 “android.intent.category.HOME”

String INSERT_ACTION 动作:在容器中插入一个空项 (item)。 “android.intent.action.INSERT”

String INTENT_EXTRA 附加数据:和 PICK_ACTIVITY_ACTION 一起使用时,说明用户选择的用来显示的 activity;和 ADD_SHORTCUT_ACTION 一起使用的时候,描述要添加的快捷方式。 “android.intent.extra.INTENT”

String LABEL_EXTRA 附加数据:大写字母开头的字符标签,和 ADD_SHORTCUT_ACTION 一起使用。 “android.intent.extra.LABEL”

String LAUNCHER_CATEGORY 类别:Activity 应该被显示在顶级的 launcher 中。 “android.intent.category.LAUNCHER”

String LOGIN_ACTION 动作:获取登录凭证。 “android.intent.action.LOGIN”

String MAIN_ACTION 动作:作为主入口点启动,不需要数据。 “android.intent.action.MAIN”

String MEDIABUTTON_ACTION 广播:用户按下了“Media Button”。 “android.intent.action.MEDIABUTTON”

String MEDIA_BAD_REMOVAL_ACTION 广播:扩展介质(扩展卡)已经从 SD 卡插槽拔出,但是挂载点 (mount point) 还没解除 (unmount)。 “android.intent.action.MEDIA_BAD_REMOVAL”

String MEDIA_EJECT_ACTION 广播:用户想要移除扩展介质(拔掉扩展卡)。 “android.intent.action.MEDIA_EJECT”

String MEDIA_MOUNTED_ACTION 广播:扩展介质被插入,而且已经被挂载。 “android.intent.action.MEDIA_MOUNTED”

String MEDIA_REMOVED_ACTION 广播:扩展介质被移除。 “android.intent.action.MEDIA_REMOVED”

String MEDIA_SCANNER_FINISHED_ACTION 广播:已经扫描完介质的一个目录。 “android.intent.action.MEDIA_SCANNER_FINISHED”

String MEDIA_SCANNER_STARTED_ACTION 广播:开始扫描介质的一个目录。 “android.intent.action.MEDIA_SCANNER_STARTED”

String MEDIA_SHARED_ACTION 广播:扩展介质的挂载被解除 (unmount)。
因为它已经作为 USB 大容量存储被共享。 “android.intent.action.MEDIA_SHARED”

String MEDIA_UNMOUNTED_ACTION 广播:扩展介质存在,但是还没有被挂载 (mount)。 “android.intent.action.MEDIA_UNMOUNTED”

String MESSAGE_WAITING_STATE_CHANGED_ACTION 广播:电话的消息等待(语音邮件)状态已经改变。 “android.intent.action.MWI”

int MULTIPLE_TASK_LAUNCH 启动标记:和 NEW_TASK_LAUNCH 联合使用。
禁止将已有的任务改变为前景任务 (foreground)。 8 0×00000008

String NETWORK_TICKLE_RECEIVED_ACTION 广播:设备收到了新的网络 “tickle” 通知。 “android.intent.action.NETWORK_TICKLE_RECEIVED”

int NEW_TASK_LAUNCH 启动标记:设置以后,activity 将成为历史堆栈中的第一个新任务(栈顶)。 4 0×00000004

int NO_HISTORY_LAUNCH 启动标记:设置以后,新的 activity 不会被保存在历史堆栈中。 1 0×00000001

String PACKAGE_ADDED_ACTION 广播:设备上新安装了一个应用程序包。 “android.intent.action.PACKAGE_ADDED”

String PACKAGE_REMOVED_ACTION 广播:设备上删除了一个应用程序包。 “android.intent.action.PACKAGE_REMOVED”

String PHONE_STATE_CHANGED_ACTION 广播:电话状态已经改变。 “android.intent.action.PHONE_STATE”

String PICK_ACTION 动作:从数据中选择一个项目 (item),将被选中的项目返回。 “android.intent.action.PICK”

String PICK_ACTIVITY_ACTION 动作:选择一个 activity,返回被选择的 activity 的类(名)。 “android.intent.action.PICK_ACTIVITY”

String PREFERENCE_CATEGORY 类别:activity是一个设置面板 (preference panel)。 “android.intent.category.PREFERENCE”

String PROVIDER_CHANGED_ACTION 广播:更新将要(真正)被安装。 “android.intent.action.PROVIDER_CHANGED”

String PROVISIONING_CHECK_ACTION 广播:要求 polling of provisioning service 下载最新的设置。 “android.intent.action.PROVISIONING_CHECK”

String RUN_ACTION 动作:运行数据(指定的应用),无论它(应用)是什么。 “android.intent.action.RUN”

String SAMPLE_CODE_CATEGORY 类别:To be used as an sample code example (not part of the normal user experience). “android.intent.category.SAMPLE_CODE”

String SCREEN_OFF_ACTION 广播:屏幕被关闭。 “android.intent.action.SCREEN_OFF”

String SCREEN_ON_ACTION 广播:屏幕已经被打开。 “android.intent.action.SCREEN_ON”

String SELECTED_ALTERNATIVE_CATEGORY 类别:对于被用户选中的数据。
activity 是它的一个可选操作。 “android.intent.category.SELECTED_ALTERNATIVE”

String SENDTO_ACTION 动作:向 data 指定的接收者发送一个消息。 “android.intent.action.SENDTO”

String SERVICE_STATE_CHANGED_ACTION 广播:电话服务的状态已经改变。 “android.intent.action.SERVICE_STATE”

String SETTINGS_ACTION 动作:显示系统设置。输入:无。 “android.intent.action.SETTINGS”

String SIGNAL_STRENGTH_CHANGED_ACTION 广播:电话的信号强度已经改变。 “android.intent.action.SIG_STR”

int SINGLE_TOP_LAUNCH 启动标记:设置以后,如果 activity 已经启动。
而且位于历史堆栈的顶端,将不再启动(不重新启动) activity。 2 0×00000002

String STATISTICS_REPORT_ACTION 广播:要求 receivers 报告自己的统计信息。 “android.intent.action.STATISTICS_REPORT”

String STATISTICS_STATE_CHANGED_ACTION 广播:统计信息服务的状态已经改变。 “android.intent.action.STATISTICS_STATE_CHANGED”

String SYNC_ACTION 动作:执行数据同步。 “android.intent.action.SYNC”

String TAB_CATEGORY 类别:这个 activity 应该在 TabActivity 中作为一个 tab 使用。 “android.intent.category.TAB”

String TEMPLATE_EXTRA 附加数据:新记录的初始化模板。 “android.intent.extra.TEMPLATE”

String TEST_CATEGORY 类别:作为测试目的使用,不是正常的用户体验的一部分。 “android.intent.category.TEST”

String TIMEZONE_CHANGED_ACTION 广播:时区已经改变。 “android.intent.action.TIMEZONE_CHANGED”

String TIME_CHANGED_ACTION 广播:时间已经改变(重新设置)。 “android.intent.action.TIME_SET”

String TIME_TICK_ACTION 广播:当前时间已经变化(正常的时间流逝)。 “android.intent.action.TIME_TICK”

String UMS_CONNECTED_ACTION 广播:设备进入 USB 大容量存储模式。 “android.intent.action.UMS_CONNECTED”

String UMS_DISCONNECTED_ACTION 广播:设备从 USB 大容量存储模式退出。 “android.intent.action.UMS_DISCONNECTED”

String UNIT_TEST_CATEGORY 类别:应该被用作单元测试(通过 test harness 运行)。 “android.intent.category.UNIT_TEST”

String VIEW_ACTION 动作:向用户显示数据。 “android.intent.action.VIEW”

String WALLPAPER_CATEGORY 类别:这个 activity 能过为设备设置墙纸。 “android.intent.category.WALLPAPER”

String WALLPAPER_CHANGED_ACTION 广播:系统的墙纸已经改变。 “android.intent.action.WALLPAPER_CHANGED”

String WALLPAPER_SETTINGS_ACTION 动作:显示选择墙纸的设置界面。输入:无。 “android.intent.action.WALLPAPER_SETTINGS”

String WEB_SEARCH_ACTION 动作:执行 web 搜索。 “android.intent.action.WEB_SEARCH”

String XMPP_CONNECTED_ACTION 广播:XMPP 连接已经被建立。 “android.intent.action.XMPP_CONNECTED”

String XMPP_DISCONNECTED_ACTION 广播:XMPP 连接已经被断开。 “android.intent.action.XMPP_DI

 

转载自:http://yangguangfu.iteye.com/blog/864906

星星微博wp7版第一版完工啦!

7 条评论

经过一个多星期的开发,星星微博wp7版也基本上完工了,第一版,基本上继承了星星微博android版的功能。UI方面也采用的android版的UI,以后将保持统一的UI。手机有wp7的手机的童鞋可以下下来玩玩…

爱飞信wp7客户端发布啦!

11 条评论

由于工作需要,开始学习了wp7应用开发,在开发公司项目之余,也顺便搞了搞爱飞信,第一个版本,功能比较简单,但是还是秉承了爱飞信的宗旨,就是简单的发送飞信。其余的什么功能都是多余,实用才是王道。

点此下载xap附件

不多说了,看几张截图吧,呵呵

Q围脖安卓版已经大致完工了

3 条评论
logo

由于工作性质,接触了安卓开发,经过一段时间的学习和实践,还是能够开发出一些简单的安卓项目了。突然想起了我曾经的一个Q围脖的项目,以前是用pys60写的,适用于塞班的机子。现如今安卓大行其道,也弄一个吧,也当做是锻炼自己的能力了。

客户端的UI吧,别提有多惨了。反正,我自己设计的UI,这里模仿下,那里抄一抄,混杂一下,就成这个样子了。我的美学鉴赏能力也就这水平了,请大家见谅了。

上图吧,不多说了。

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

9 条评论

你也许见过有的软件的列表界面当你拖动到最后一列的时候会出现一个“正在加载…”或者“更多”的按钮。然后列表会自动加载更多内容病自动显示。这是怎么实现的呢?原来就是在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版塞班论坛发布了

9 条评论

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

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

10 条评论

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

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

11 条评论

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