2.6 ListView Item多布局的实现

时间:2021-07-17 18:05:08

一、布局文件

1、MainActivity.xml 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<ListView
android:id="@+id/list_book"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"/>


</RelativeLayout>

2、新建一个布局文件:item_one.xml,布局代码如下:

<?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">


<ImageView
android:id="@+id/img_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@mipmap/ic_launcher" />


<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>

3、新建一个布局文件:item_two.xml,布局代码如下:

<?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">


<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>

Java代码

1、新建一个java类,命名为:App.java,添加代码:

package com.example.yuancan.test093001;

/**
* Created by yuancan on 16/9/30.
*/

public class App {
private int aIcon;
private String aName;

public App() {
}

public App(int aIcon, String aName) {
this.aIcon = aIcon;
this.aName = aName;
}

public int getaIcon() {
return aIcon;
}

public String getaName() {
return aName;
}

public void setaIcon(int aIcon) {
this.aIcon = aIcon;
}

public void setaName(String aName) {
this.aName = aName;
}
}

2、新建一个java类,命名为:Book.java,添加代码:

package com.example.yuancan.test093001;

/**
* Created by yuancan on 16/9/30.
*/

public class Book {
private String bName;
private String bAuthor;

public Book() {
}

public Book(String bName, String bAuthor) {
this.bName = bName;
this.bAuthor = bAuthor;
}

public String getbName() {
return bName;
}

public String getbAuthor() {
return bAuthor;
}

public void setbName(String bName) {
this.bName = bName;
}

public void setbAuthor(String bAuthor) {
this.bAuthor = bAuthor;
}
}

3、新建一个继承BaseAdapter的java类,命名为:MutiLayoutAdapter,添加代码:

package com.example.yuancan.test093001;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.util.SparseArray;
import android.widget.TextView;
import android.view.LayoutInflater;


import java.util.ArrayList;

/**
* Created by yuancan on 16/9/30.
*/

public class MutiLayoutAdapter extends BaseAdapter {

private static final int TYPE_BOOK = 0;
private static final int TYPE_APP = 1;
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 {
return super.getItemViewType(position);
}
}

//类别数目
@Override
public int getViewTypeCount() {
return 2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
int type = getItemViewType(position);
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = 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;
}
}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;
}
}

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;
}
return convertView;
}

//两个不同的ViewHolder
private static class ViewHolder1{
ImageView img_icon;
TextView txt_aname;
}

private static class ViewHolder2{
TextView txt_bname;
TextView txt_bauthor;
}

}

4、在Strings.xml中添加代码:

<resources>
<string name="app_name">Test093001</string>

<item name="Tag_APP" type="id"></item>
<item name="Tag_Book" type="id"></item>

</resources>

完成。