Android功能组件源代码分析之《XListView》

XListView是目前android使用非常广泛一个ListView组件。主要是提供表头下拉刷新列表和表尾加载更多的功能。


XListView组件主要有两部分组成,一部分就是XListView,另外一部分是对应XListAdapter。下面,我们就分别对两部分进行详解,已达到完全该组件的目标。

(1)XListView

该类继承于ListView,主要有三部分组成,header,中间listview的内容,footer。关于ListView的header和footer可以去网上查点资料了解下。大体上来说,如果使用ListView的addHeader(view)函数,此时view将会作为ListView的一部分呈现出来,footer原理也是一样。

(1.1)Header

该view主要有几个常用的图片和文字组成,图片是一个箭头,文字包括下拉刷新和上次刷新时间等等。Header被定义成三个状态

//定义三种状态:正常,ready,和正在更新
public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_REFRESHING = 2;

STATE_NORMAL:文字提示”向下刷新“,箭头图标向下

STATE_READY:文字提示“松开刷新数据”,箭头图标向上

STATE_REFRESHING:文字提示”正在刷新中“,箭头小时,progress bar出现



(1.2)Footer

主要包括文字,“查看更多”和progress bar。状态设置跟Header中基本上一样,这里不重复了。


(1.3)Body

ListView中间那段,这段主要XListAdapter来决定,里面最关键的就是getView函数

public View getView(int position, View convertView, ViewGroup parent) {
	if (convertView == null) {
		convertView = getLayoutInflater().inflate(
				R.layout.forum_display_item, null);
	}
	TextView title = (TextView) convertView
			.findViewById(R.id.forumDisplayTitle);//标题
	TextView replyCnt = (TextView) convertView
			.findViewById(R.id.replyCnt);//回复次数
	TextView viewsCnt = (TextView) convertView
			.findViewById(R.id.viewsCnt);//查看次数
	TextView userName = (TextView) convertView
			.findViewById(R.id.postUserName);//post用户名
	ImageViewWithCache img = (ImageViewWithCache) convertView
			.findViewById(R.id.headImg);//头像

	JSONObject item = m_model.getJSONObject(position);
	String threadTitle = item.getString("threadtitle");
	// 如果当前为新贴版块,不加置顶帖提示信息
	if (m_id != Api.NEW_FORUM_ID) {
		if (item.getInteger("globalsticky") == Api.GLOBAL_TOP_FORUM) {
			threadTitle = "<font color=\"red\">[总顶] </font>"
					+ threadTitle;
		} else if (item.getInteger("globalsticky") == Api.AREA_TOP_FORUM) {
			threadTitle = "<font color=\"red\">[区顶] </font>"
					+ threadTitle;
		} else if (item.getInteger("sticky") == Api.TOP_FORUM) {
			threadTitle = "<font color=\"red\">[置顶] </font>"
					+ threadTitle;
		}
	}
	title.setText(Html.fromHtml(threadTitle));//根据版本进行不同的标识
	
	replyCnt.setText(item.getString("replycount"));
	viewsCnt.setText(item.getString("views"));
	userName.setText(Html.fromHtml(item.getString("postusername")));
	if (item.getInteger("avatar") == 1) {
		try {
			img.setImageUrl(new URL(Api.getInstance()
					.getUserHeadImageUrl(item.getInteger("postuserid"))));
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	} else {
		img.setImageResource(R.drawable.default_user_head_img);
	}
	
	//是否开放,否加锁
	convertView.findViewById(R.id.forumDisplayLock).setVisibility(
			(item.getInteger("open") == 0) ? View.VISIBLE : View.GONE);

	return convertView;
}

从上面代码可以,就是View,里面显示各种post信息,包括post主题,poster等等


而代码中m_model则为需要加载post条目,每条post都如上图所示。

(2)使用XListView

使用XListView就可以当做是普通的ListView来使用就行了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值