Android ListView自定义适配器实现仿QQ界面

  

PS: listview中有一些简单使用的适配器,如:SimpleAdapter:构造方法SimpleAdapter(上下文语境,List   

  

如图:   

 Android ListView自定义适配器实现仿QQ界面

  

<强> 1,首先写布局文件
  

  

activity_layout。xml
  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=癿atch_parent”比;   & lt;列表视图   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: id=癅 + id/id_lv”比;      & lt;/ListView>      & lt;/LinearLayout>   之前      

没一个条目的样式:itemstyle_layout.xml 其中的图片自己随便找个即可。
  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   面向android:="水平"   android: layout_height=癿atch_parent”比;      & lt; ImageView   android: layout_width=" 70 dp”   android: layout_height=" 80 dp”   android: src=" https://www.yisu.com/zixun/@drawable“金银铜/比;   LinearLayout & lt;   android: layout_width=" 0 dp”   android: layout_weight=" 0.9 "   android: layout_height=" 80 dp”   面向android:=按怪薄北?   & lt; TextView   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: textSize=" 35 dp”   android:文本=" 11111 "   android: id=癅 + id/id_item_tv”/比;   & lt; TextView   android: layout_marginTop=" 5 dp "   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: textSize=" 20 dp”   android:文本=" 11111 "   android: id=癅 + id/id_item_tv2”/比;            & lt;/LinearLayout>   LinearLayout & lt;   android: layout_width=" 0 dp”   android: layout_height=" 80 dp”   android: layout_weight=" 0.1 "   面向android:=按怪薄北?   & lt; ImageView   android: layout_width=" match_parent "   android: layout_height=" 50 dp”   android: src=" https://www.yisu.com/zixun/@drawable xin1 "/比;         & lt;/LinearLayout>            & lt;/LinearLayout>   之前      

<强> 2,创建适配器MyAdapter.java ,强(已优化)
  

  

申明一点,适配中有优化方法,如果不优化的话,有时也可以加载出来,但有时就会加载错乱,没调用一次就会多浪费创建一个视图对象,如:
  

        视图项=LayoutInflater.from(上下文).inflate (R.layout.itemstyle_layout, null);//TextView信息=(TextView) item.findViewById (R.id.id_item_tv);//info.setText (list.get(位置)+ " ");   之前      

优化后(加入了Viewholder)
  

        公开课MyAdapter延伸BaseAdapter {//公共List比;列表;   公共环境上下文;      公共MyAdapter(上下文语境,List比;列表){   this.context=上下文;   this.list=列表;      }   之前            @Override   公共int getCount () {   返回list.size();//返回个数   }      @Override   公共对象getItem (int位置){   返回list.get(位置);//返回项   }      @Override   公共长getItemId (int位置){   返回位置;//角标   }      @Override   公众视线getView (int位置、视图convertView ViewGroup父){//视图项=LayoutInflater.from(上下文).inflate (R.layout.itemstyle_layout, null);//TextView信息=(TextView) item.findViewById (R.id.id_item_tv);//info.setText (list.get(位置)+ " ");      ViewHolder ViewHolder;   如果(convertView==null) {   viewHolder=new viewHolder ();   convertView=LayoutInflater.from(上下文).inflate (R.layout.itemstyle_layout, null);//加载布的局      viewHolder。tv1=(TextView) convertView.findViewById (R.id.id_item_tv);   viewHolder。tv2=(TextView) convertView.findViewById (R.id.id_item_tv2);   convertView.setTag (viewHolder);   其他}{   viewHolder=(viewHolder) convertView.getTag ();   }      viewHolder.tv1.setText (list.get(位置). get (“tv1”) + " ");   viewHolder.tv2.setText (list.get(位置). get(“天空”)+ " ");         返回convertView;   }      静态类ViewHolder   {   ImageView第四;   TextView tv1;   TextView tv2;   }   

Android ListView自定义适配器实现仿QQ界面