ListView使用小技巧

编程入门 行业动态 更新时间:2024-10-07 14:29:56

ListView使用<a href=https://www.elefans.com/category/jswz/34/1769317.html style=小技巧"/>

ListView使用小技巧

1.如何取消Listview的滚动条? 
  setVerticalScrollBarEnabled(false) 
2.白色的背景,ListView滚屏进行中的时候,背景会变成黑色,解决办法? 
  android:cacheColorHint="#00000000" 
3.ListView滚动条怎么一直都显示? 
  android:fadeScrollbars="false" 
4.ListView隔行变色: 
  int[] colors={Color.BLUE,Color.CYAN}; 
  convertView.setBackgroundColor(colors[position%2]); 
5.ListView中嵌套了checkbox,焦点会到checkbox身上,解决办法 
  holder.checkBox.setFocusable(false); 
6.listView滚动轴图片更改? 
   android:scrollbarThumbHorizontal=""   //滚动轴游标 
   android:scrollbarTrackVertical=""  //滚动轴背景、 
   引用的是9patch图片 
7.ListView设置EmptyView? 
   mListView.serEmptyView(); 
   Sets the view to show if the adapter is empty这个就是对此方法的描述 
    当listview的adapter为null的时候,就会显示所设置的view。 
    需要注意的有两点:在调用setAdapter()之前调这个方法;设置的emptyview必须放 在listview的直接父布局(getParent())里,比如说listview嵌在一个LinearLayout里面的话,需要在代码里面写  mLinearLayout.addContentView(你的emptyview); 
   否则的话是没效果的。 
8.ListView设置FooterView? 
   在调用setAdapter()之前调这个方法 
   footerView可以单独写点击触发事件,但是注意最好写在listview的 OnItemClickListener()里面,然后根据点击的位置 
  if(position == mListView.getCount - 1){"触发footerview点击事件"} 
         如果是单独给它一个View.onClickListener的话,点击是可以触发事件的,但是没有listview的item按下的变黄色效果。 
9.android 自定义listview无法响应点击事件OnItemClickListener 
    如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了 

    我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendants”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItemClick()方法了。


ListView有一个公开的方法:setEmptyView(View v)可是这个方法的设置是有限制的,就是设置View必需在当前的View hierarchy里,亦即这个View需要被add到当前一个View Tree的一个结点上,如果没有添加到结点上的话,调用setEmptyView(View v)是没有任何效果的。

它的过程大概是:
ListView listview = (ListView) findViewById(R.id.list);
View emptyView = findViewById(R.id.empty);
ViewGroup parentView = (ViewGroup) listview.getParent();
parentView.addView(newEmptyView, 2); // 你需要在这儿设置正确的位置,以达到你需要的效果。
listview.setEmptyView(emptyView);

另:如果你直接设置在XML中也就不需要额外的添加到View Tree中了,因为它已经在那儿了,比如你的Layout是
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android=""
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

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


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:fastScrollEnabled="true"
        android:textSize="18sp" />

    <TextView
        android:id="@+/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="15dip"
        android:text="@string/text_no_song"
        android:textSize="22sp"
        android:visibility="gone" />
</LinearLayout>

那你只需要以下的代码就可以了:
ListView listview = (ListView) findViewById(R.id.list);
View emptyView = findViewById(R.id.empty);
listview.setEmptyView(emptyView);

更多推荐

ListView使用小技巧

本文发布于:2024-02-11 21:15:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小技巧   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!