安卓系统中使用listview实现qq/微信好友列表

  

<强>首先附上运行结果:

  

 Android中使用listview实现qq/微信好友列表

  

如果你没有学过listview请你先看一看基本知识。不想再说的那么细了太多了。

  

<强>首先是listview布局

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; ListView xmlns: android=" http://schemas.android.com/apk/res/android "   android: id=癅 + id/lv_view”   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: cacheColorHint=" # 00000000 "   android:背景=" @drawable/?   面向android:=按怪薄北?   & lt;/ListView>之前      

在这里我为什么这样设置

        android: cacheColorHint=" # 00000000 "   android:背景=" @drawable/?/pre>      

在Android中,视图是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性Android:背景=癅drawable/bg”,不过不要高兴地太早,当你这么做以后,发现背景是变了,但是当你拖动,或者点击列表空白位置的时候发现列都变成黑色的了,破坏了整体效果。
  

  

这是什么原因导致的呢?起初我以为是因为我把背景设置成了白色,然后产生色差导致的,后来查阅资料发现,其实这个要从列表视图的效果说起,默认列的背景是透明的,而视图的背景是固定不变的,所以在滚动条滚动的过程中如果实时地去将当前每个条目的显示内容跟背景进行混合运算,所以android系统为了优化这个过程用,就使用了一个叫做android: cacheColorHint的属性,在黑色主题下默认的颜色值是# 191919,所以就出现了刚才的画面,有一半是黑色的,那怎么办呢?

  

如果你只是换背景的颜色的话,可以直接指定android: cacheColorHint为你所要的颜色,如果你是用图片做背景的话,那也只要将android: cacheColorHint指定为透明(# 00000000)就可以了,当然为了美化是要牺牲一些效率的。

  

由于我们使用的好友列表向比较复杂,一个ImageView两个TextView,所以要自定义适配器。下面的TextView是我自己创建的TextView为了实现昵称和个性签名的滚动效果。由于占用资源太多不推荐所有人的昵称和个性签名都滚动哦,只要获得焦点的滚动就好了。我就不改了交给你了

  

<强> MyTextView.class

        包com.example.friendlist.mytextview;   进口android.content.Context;   进口android.util.AttributeSet;   进口android.widget.TextView;      公开课MyTextView延伸TextView {   公共MyTextView(上下文语境、AttributeSet attrs int defStyle) {   超级(上下文、attrs defStyle);//TODO自动生成构造函数存根   }   公共MyTextView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);//TODO自动生成构造函数存根   }   公共MyTextView(上下文语境){   超级(上下文);//TODO自动生成构造函数存根   }//一直返回true   @Override   公共布尔isFocused () {//TODO自动生成方法存根   返回true;   }   }   之前      

<>强自定义适配器引用的布局文件

        & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: paddingBottom=" @dimen/activity_vertical_margin”   android: paddingLeft=" @dimen/activity_horizontal_margin”   android: paddingRight=" @dimen/activity_horizontal_margin”   android: paddingTop=" @dimen/activity_vertical_margin”   工具:上下文="。祝辞FirendListActivity”;      & lt; ImageView   android: id=癅 + id/iv_picture”   android: layout_alignParentLeft=" true "   android: layout_width=" 60 dp "   android: layout_height=" 60 dp "   android: src=" https://www.yisu.com/zixun/@drawable pic1 "/比;   & lt; com.example.friendlist.mytextview.MyTextView   android: id=癅 + id/tv_nickname”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_toRightOf=" @ id/iv_picture”   android: maxEms=" 4 "   android:单行模式=" true "   android: ellipsize=罢信啤?   android: layout_centerInParent=" true "/比;   & lt; com.example.friendlist.mytextview.MyTextView   android: id=癅 + id/tv_description”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_alignParentRight=" true "   android: layout_centerInParent=" true "   android: maxEms=" 8 "   android:单行模式=" true "   android: ellipsize=把】颉?祝辞   & lt;/RelativeLayout>   

安卓系统中使用listview实现qq/微信好友列表