ListView[1] 显示效果
【参考链接】
ListView的HeaderView http://blog.****.net/caiwenfeng_for_23/article/details/38276403
item
1、ListView的item的布局文件,最外层的View/ViewGroup,不管布局文件中是如何写的,最终layout_width会被自动置为match_parent,layout_height会被置为wrap_content。
以如下代码为例,Activity的布局文件中,ListView的layout_width为800px
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BaseAdapterActivity">
<View
android:layout_width="800px"
android:layout_height="200px"
android:background="#FF0000"/>
<ListView
android:id="@+id/lv"
android:layout_width="800px"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
/>
</LinearLayout>
item的布局文件,最外层的LinearLayout,layout_width=400px, layout_height=200px
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400px"
android:layout_height="200px"
android:orientation="horizontal"
android:background="#0000FF">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="400px"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large
Text"
/>
</LinearLayout>
这是因为Adapter的getView()函数中,inflate()的rootView参数必须为null,否则会报异常
而当rootView参数为null时,inflate()出来的View并没有设置LayoutParams。此时ListView会自动给设置MATCH_PARENT x WRAP_CONTENT的LayoutParams。
2、即使ListView的layout_width设置的是wrap_content,显示出来的效果依然是match_parent
如下ListView和Item的layout_width都设置的是wrap_content
这是因为ListView的onMeasure()中,最后会调用setMeasuredDimension(),而其中的widthSize,不管是wrap_content还是match_parent,其值都是parentSize。
dividerHeight
divider
默认item之间会有一条1dp左右的分割线,颜色为白色或黑色
可以使用dividerHeight设置其高度,使用divider设置图片
listSelector
默认情况下,ListView的item点击以后会有一个背景颜色的变化效果,如果希望移除这个效果,可以修改listSelector属性
android:listSelector="@android:color/transparent"
scrollbars
fadeScrollbars
默认情况下scrollbars=true, fadeScrollbars=true,表示显示滚动条,并在停止滑动一段时间后消失
设置成scrollbars=false则不显示滚动条,设置成scrollbars=true,fadeScrollbars=false则表示一直显示滚动条
addHeaderView()
addFooterView()
可以在item的最上面、最下面再添加一些View
1. 必须在setAdapter()之前调用
2. 第2个参数表示传递给View的数据,可以设置为null,第3个参数表示是否相应OnItemClickListener,不过不建议使用OnItemClickListener,所以可以传false。但不管HeaderView/FooterView是否响应OnItemClickListener,都会导致OnItemClickListener中的position发生位移
3. inflate()中的rootView参数会影响HeaderView、FootView的LayoutParams,为null时为被修改为MATCH_PARENT x WRAP_CONTENT,不为null时使用xml中的设置。
4. HeaderView/FooterView跟Item之间也有divider,如果设置了的话
5. 可以添加多次,依次往下排列
6. 后续可以使用removeHeaderView()、removeFooterView()移除
//inflate()的第2个参数会影响headerView的LayoutParams
View headerView =getLayoutInflater().inflate(R.layout.base_adapter_item1,
mListView, false);
//addHeaderView的第3个参数影响是否响应OnItemClickListener
//虽然不建议使用OnItemClickListener
mListView.addHeaderView(headerView,
null, true);