`
hulianwang2014
  • 浏览: 681092 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

ListView中几个需要注意的属性

 
阅读更多

本文主要介绍Android ListView控件在使用中需要注意的几个特殊的属性。

关于ListView的详细介绍,请移步ListView的属性及方法详解

关于ListView的常见用法介绍,请移步Android ListView的常见用法


一、stackFromBottom

若把ListView的XML属性android:stackFromBottom设为"true",则ListView中的内容将从底部开始显示。效果如图:


二、cacheColorHint

当你在点击ListView或者拖动它的时候,是不是发现ListView中的条目变黑了?这点大大影响了整体UI的和谐统一。其实解决方法很简单,利用ListView的父类AbsListView提供的XML属性----android:cacheColorHint就可以解决。cacheColorHint表明列表将始终以单一、固定的颜色来绘制背景,如果想与整体的背景色统一,只需要将其设为透明(#00000000)即可。

运行效果见下一个属性。

三、scrollBars

当你需要一个快速滑动条时,android:scrollBars属性就可以满足你的需要。

将android:scrollBars='Vertical'与调用ListView的方法setVerticalScrollBarEnabled(true)效果是一样的。

如图所示:

可以注意到屏幕右侧出现了快速滚动条,另外,在滚动时ListView没有变成黑色,就是因为设置了属性android:cacheColorHint="#00000000".

如果你需要让ScrollBar一直显示,可以调用ListView的方法setScrollBarFaddingEnabled(false);

如果你需要让ScrollBar在滚动时显示,并在滚动结束时一段时间后自动消失,可以设置XML属性----android:scrollBarFadeDuration="500",500表示在滚动后500毫秒后ScrollBar消失

Ps:哪位大神可以告诉我android:fastScrollEnabled与ScrollBars有何区别

四、footerView和headerView

1. 可随ListView滚动的footView和headView

(1)创建HeaderView的XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/headertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000" />

</LinearLayout>

(2)在Activity方法中添加以下代码(只添加了setHeaderandFooter方法,可以根据上下文自行理解):

private void setHeaderandFooter() {
		LayoutInflater lif = LayoutInflater.from(this);
		try {
			View headerView = lif.inflate(R.layout.header, myListView, false);

			TextView header = (TextView) headerView.findViewById(R.id.headertext);
			header.setText("HeaderView");
			myListView.addHeaderView(headerView);
		} catch (Exception e) {
			// TODO: handle exception
			Log.v("List", e.getMessage());
		}
	}

(3)运行结果:

(4)需要注意的地方

第一点是addHeaderView必须在setAdapter之前调用,否则headerview将不会显示出来

在这里需要注意的第二点是,添加完headerView之后,会导致OnItemClickListener等监听器的position移位

例如,我处理点击事件的代码为:

myListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(ListViewTestActivity.this)
						.setMessage("Clicked Line No." + arg2)
						.setPositiveButton("Confirm", null).show();
			}
		});


而当我点击了显示数字0的那一条,结果为:


可以看出position将headerview也计入在内了,这显然不是我们所预期的结果。为了解决这个问题,在此提供一种解决方法:

手动计算真实的position

final int headerCount = myListView.getHeaderViewsCount();
		myListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(ListViewTestActivity.this)
						.setMessage("Clicked Line No." + (arg2 - headerCount))
						.setPositiveButton("Confirm", null).show();
			}
		});

2.固定的HeaderView

固定的HeaderView,即不随ListView滚动的HeaderView。实现效果如下:



其实做法很简单,但是有一些小细节需要注意。

首先来看main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#58ACFA"
    android:orientation="vertical" >

    <include layout="@layout/header" />


    <ListView
        android:id="@+id/myListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:fastScrollEnabled="true"
        android:scrollbarFadeDuration="1000"
        android:scrollbarStyle="outsideOverlay" >
    </ListView>



</LinearLayout>
可以看到,在ListView之上添加了一个<include>标签,指向header布局。意为将header布局放在ListView上方。

header.xml

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

    <TextView
        android:id="@+id/headertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:text="HeaderText" />

</LinearLayout>

在此需要注意,header中的LinearLayout的layout_height一旦设为"fill_parents"将会使得ListView无法显示出来。(为此纠结了好久……)

而以这种方式添加headerView的话,OnItemClickListener中的position就不会发生移位现象了,因为严格意义上来说,这里的headerView并不属于ListView这个ViewGroup中,而是另外一个独立的布局。


分享到:
评论
Global site tag (gtag.js) - Google Analytics