listview作为Android阵营的重要控件,是安卓手机上展示内容的要控件之一(现在是recyclerview做主导),前几年的安卓面试listview作为一个控件,可以说,是面试的不可缺少的布局。虽然其江湖地位被新秀recyclerview代替,不过在并不需要很多炫酷的侧拉删除等功能的前提下,listview依然是大部分开发者的首选。
既然想用listview实现首页,那么listview的基本用法肯定是知道的,而listview的多item则需要在原本的五个必须实现的方法上再重写两个,getItemViewType和getViewTypeCount;对于这两个方法谷歌已经做了见名之意。所以你不需要去看源码解释它是什么意思。
getItemViewType就类似快递仓库进行包裹分类,getViewTypeCount就是对类型数量进行统计。
这是activity的布局
<RelativeLayout 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"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
然后为了看到效果,准备一些数据进行模拟请求的json的数据集合,创建数据前先分析一下数据结构,现在的首页一般都是轮播图模块,分类模块,列表展示块;于是创建一个集合,并把泛型指定为Object。
private ArrayList<Object> mData = null;
并向集合添加数据,
mData = new ArrayList<Object>();
mData.add(new ListB(banners));
mData.add(new Book("第二种item", "我是纯文字"));
mData.add(new App(R.mipmap.iv_icon_baidu, "百度"));
mData.add(new App(R.mipmap.iv_icon_baidu, "百度"));
mData.add(new ListB(banners));
mData.add(new Book("第二种item", "我是纯文字"));
mData.add(new App(R.mipmap.iv_icon_baidu, "百度"));
mData.add(new Book("第二种item", "我是纯文字"));
在这个集合中,我将不同的数据用bean装了起来,在getItemViewType判断时就不用判断postion,而直接判断bean,因为如果是判断postion,那么你必须固定返回的数据的顺序,如某天模块顺序打乱了,那么数据就无法显示了,用bean就解决了这个问题。
public class App {
private int aIcon;
private String aName;}
public class banner {
private int str;}
public class Book {
private String bName;
private String bAuthor;}
bean类代码我就是贴出来数据类型,并没有进行get和set,请自行实现。
item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/img_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@mipmap/iv_icon_baidu" />
<TextView
android:id="@+id/txt_aname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="百度"
android:textSize="20sp" />
</LinearLayout>
第二种布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.recker.flybanner.FlyBanner
android:id="@+id/banner_1"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
第三种布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/txt_bname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="《第一行代码Android》"
android:textColor="#F3684A"
android:textSize="18sp" />
<TextView
android:id="@+id/txt_bauthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="郭霖"
android:textColor="#44BDED"
android:textSize="18sp" />
</LinearLayout>
Adapter的设置
public class MutiLayoutAdapter extends BaseAdapter {
//定义两个类别标志
private static final int TYPE_BOOK = 0;
private static final int TYPE_APP = 1;
private static final int TYPE_BANNER = 2;
private Context mContext;
private ArrayList<Object> mData = null;
public MutiLayoutAdapter(Context mContext, ArrayList<Object> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//多布局的核心,通过这个判断类别
@Override
public int getItemViewType(int position) {
if (mData.get(position) instanceof App) {
return TYPE_APP;
} else if (mData.get(position) instanceof Book) {
return TYPE_BOOK;
} else if (mData.get(position) instanceof ListB) {
return TYPE_BANNER;
} else {
return super.getItemViewType(position);
}
}
//就是有几种item,这个是直接写,
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
ViewHolder3 holder3 = null;
if (convertView == null) {
switch (type) {
case TYPE_APP:
holder1 = new ViewHolder1();
convertView=
LayoutInflater.from(mContext).inflate(R.layout.item_one, parent, false);
holder1.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
holder1.txt_aname = (TextView) convertView.findViewById(R.id.txt_aname);
convertView.setTag(R.id.Tag_APP, holder1);
break;
case TYPE_BOOK:
holder2 = new ViewHolder2();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);
holder2.txt_bname = (TextView) convertView.findViewById(R.id.txt_bname);
holder2.txt_bauthor = (TextView) convertView.findViewById(R.id.txt_bauthor);
convertView.setTag(R.id.Tag_Book, holder2);
break;
case TYPE_BANNER:
holder3 = new ViewHolder3();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_three, parent, false);
holder3.mBanner = (FlyBanner) convertView.findViewById(R.id.banner_1);
convertView.setTag(R.id.Tag_Banner, holder3);
break;
}
} else {
switch (type) {
case TYPE_APP:
holder1 = (ViewHolder1) convertView.getTag(R.id.Tag_APP);
break;
case TYPE_BOOK:
holder2 = (ViewHolder2) convertView.getTag(R.id.Tag_Book);
break;
case TYPE_BANNER:
holder3 = (ViewHolder3) convertView.getTag(R.id.Tag_Banner);
}
}
Object obj = mData.get(position)
switch (type) {
case TYPE_APP:
App app = (App) obj;
if (app != null) {
holder1.img_icon.setImageResource(app.getaIcon());
holder1.txt_aname.setText(app.getaName());
}
break;
case TYPE_BOOK:
Book book = (Book) obj;
if (book != null) {
holder2.txt_bname.setText(book.getbName());
holder2.txt_bauthor.setText(book.getbAuthor());
}
break;
case TYPE_BANNER:
ListB mbanner = (ListB) obj;
if (mbanner != null) {
holder3.mBanner.setImagesUrl(mbanner.getListB());
holder3.mBanner.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
@Override
public void onItemClick(int position) {
int positions = position + 1;
Toast.makeText(mContext, "点击了第" + positions + "张图片",Toast.LENGTH_SHORT).show();
}
});
}
break;
}
return convertView;
}
}
为了代码的可阅读性,我已将viewholder放在了adapter外面,单独放在一个ViewHolder文件夹, 便于管理。
ViewHolder1
public class ViewHolder1 {
public ImageView img_icon;
public TextView txt_aname;
}
}
ViewHolder1
public class ViewHolder2 {
public TextView txt_bname;
public TextView txt_bauthor;
}
```
```java
ViewHolder1
public class ViewHolder3 {
public FlyBanner mBanner;
网友评论