TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
android中Listview使我们经常使用的控件,一般常规的用法就是在布局文件中定义然后在Activity使用findViewById()找到,然后定义一个listview每一项的布局文件,在adapter中getView方法里Inflate该XML文件得到一个view返回,最后给listview设置该适配器。其中adapter有如下几种:
1、ArrayAdapter,只能用来适配listview中Item只有一个TextView的情况。
2、SimpleAdapter,用来适配item中既有TextView也有ImageView的情况。
3、SimpleCursorAdapter,用来结合数据库使用,除了数据源是一个Cursor外,其余的使用同上面的SimpleAdapter。
4、BaseAdapter,前面都是封装好的Adapter,我们只需要往构造方法传参数就可以使用,BaseAdapter是一个抽象类,需要重写里面的4个抽象方法才可以使用。
一般item的布局都是一样的,当item的条目需要显示多个布局文件的时候,我们必须要使用BaseAdapter,并且重写 getItemViewType(int position)和 getViewTypeCount()这两个方法,掌握了以上的使用,基本也是够用了,下面列出一些使用中遇到的问题及解决方法:
1、如果item条目中含有Button等能获取到焦点的子控件时,点击事件就会出现问题,这时我们只需要在布局文件的根布局上面加上android:descendantFocusability=”blocksDescendants”,就可以解决点击事件分发的问题。
2、如果要给listView添加分页功能,只需要给ListView添加 OnScrollListener监听,然后在onScroll中判断isBottom = (firstVisibleItem + visibleItemCount == totalItemCount);,如果到底底部就加载新的数据。
3、ListView需要利用Recycle机制进行优化,避免多次调用findViewById,具体是定义一个ViewHolder内部类,类的成员变量就是item中View的个数,然后在getView方法中判断convertView为空的时候实例化ViewHolder,并且把其成员变量和item中的View绑定,设置一个TAG标签,在不为空的时候利用GetTAG找到ViewHolder,然后直接给ViewHolder的成员变量设置数据即可,最后返回convertView就可以了。
4、在ListView中设置Selecter为null会报空指针,解决办法是mListView.setSelector(new ColorDrawable(Color.TRANSPARENT))。
5、为了减少调用getView方法的次数,请将listview的宽高设置成fillParent或者一个具体的值。
6、如果需要改变某一项的item视图而不是整个的视图都改变,只需要做下面的操作:
- <span style="font-size:10px;">private void updateView(int itemIndex){
- int visiblePosition = yourListView.getFirstVisiblePosition();
- View v = yourListView.getChildAt(itemIndex - visiblePosition);//Do something fancy with your listitem view
- TextView tv = (TextView)v.findViewById(R.id.sometextview);
- tv.setText("Hi! I updated you manually");
- }</span>
复制代码
7、如果使用一个可以折叠的ListView可以使用expandableListView.
8、让ListView长按时有些能弹出ContextMenu有些不能,解决这个问题的办法是为这个listView设置setOnItemLongClickListener监听,然后实现 :
- <span style="font-size:10px;">public boolean onItemLongClick(AdapterView<?> parent, View view,
- int position, long id) {
- if(id == 1){
- return true;
- }
- return false;
- }</span>
复制代码 9、让ListView选中和没选中的时候字体颜色不一致,只需要给布局文件中的TextView设置字体颜色,颜色的内容用一个XML文件定义,XML文件的内容为一个selector,如下所示:
- <?xml version="1.0" encoding="utf-8" ?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_enabled="false" android:color="@color/orange"></item>
- <item android:state_window_focused="false" android:color="@color/orange"></item>
- <item android:state_pressed="true" android:color="@color/white"></item>
- <item android:state_selected="true" android:color="@color/white"></item>
- <item android:color="@color/orange"></item>
- </selector>
复制代码 10、想让一个listview的每个条目出现一个可选框,只需要使用系统的布局文件即可,如下面所示可以实现单选多选等:

11、每一个ListView都有两个无效的位置,分别为第一个item的前面,和最后一个item的下一个位置,其值为一个产量,用 ListView.INVALID_POSITION表示。
12、ListView自定义滚动条样式:
- <ListView android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:stackFromBottom="true"//从下开始显示条目
- android:transcriptMode="normal"
- android:fastScrollEnabled="true"
- android:focusable="true"
- android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
- android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
- />
复制代码 /scrollbar_vertical_track,crollbar_vertical_thumb自定义的xml文件,放在Drawable中,track是指长条,thumb是指短条
13、去掉selector选中一闪的效果:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="@android:color/transparent"/>
- <corners android:radius="0dip" />
- </shape>
- //listview.setSelector(R.drawable.thisShape);
复制代码 或者重写isEnable()方法,返回false;
14、一些常用的属性:
android:stackFromBottom="true" ,从后往前显示数据。
android:transcriptMode="alwaysScroll" ,,需要用ListView或者其它显示大量Items的控件实时跟踪或者查看信息,并且希望最新的条目可以自动滚动到可视范围内。通过设置的控件transcriptMode属性可以将Android平台的控件(支持ScrollBar)自动滑动到最底部。
android:cacheColorHint,设置为透明色这样在ListView拖动的时候不破坏背景色。
android:fadingEdge="none",去掉上边和下边的阴影。
android:scrollbars="none"或者setVerticalScrollBarEnabled(true),滚动条在不活动的时候是否隐藏。
android:fastScrollEnabled="true"或者myListView.setFastScrollEnabled(true),右边的快速滑动条是否显示。
扫描关注我的微信公众号:

|
|