Listview实现不同类型的布局

打开各种客户端发现 Listview的布局多种多样,在我以前的认知中listview不是只能放一种item布局嘛,我就震惊了,现在我自己的项目上要用到这种方式那么就去做下

原理是listview 的adapter中 有这两个方法

// 返回当前布局的样式type
@Override
public int getItemViewType(int position) {
return listDatas.get(position).type;
} // 返回你有多少个不同的类型的数量
@Override
public int getViewTypeCount() {
return 3;
}

过程 1 定义一个javabeen对象

package com.example.listviewdemo;

import java.util.HashMap;
import java.util.Map; /**
* Created by Ace on 2016/2/13.
*/
public class lIstViewItem {
// 用于区分listview显示的不同item,告诉适配器我这是什么类型,listview适配器根据type决定怎么显示
public int type;
// 将要显示的数据用HashMap包装好
public HashMap<String, Object> map; public lIstViewItem(int type, HashMap<String, Object> map) {
this.type = type;
this.map = map;
}
}

2 把数据封装在泛型是我们JAVABeen对象的的数组里

package com.example.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView; import java.util.ArrayList;
import java.util.HashMap; /**
* Created by Ace on 2016/2/13.
*/ public class MainActivity extends Activity {
private ListView listView; // 页面listview
private ListViewAdapter listViewAdapter; // listview适配器
private ArrayList<lIstViewItem> viewItemsArraylist; // Arraylist主要装载的是传给适配器的数据集合 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
// 为listview设置适配器
// ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas()); } private void initData() { } // 初始化页面组件及一些布局
private void initView() {
listView = (ListView) this.findViewById(R.id.listview);
listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas());
listView.setAdapter(listViewAdapter);
} /**
* 这里我们用三种不同的样式进行测试
**/
private ArrayList<lIstViewItem> getDatas() { viewItemsArraylist = new ArrayList<lIstViewItem>();
viewItemsArraylist.add(new lIstViewItem(2, getHashMapThirdType("美人鱼", "没有买卖就没有杀害", "2016-2-13")));
viewItemsArraylist.add(new lIstViewItem(1, getHashMapSecondType("单身狗", "情人节自己过")));
viewItemsArraylist.add(new lIstViewItem(0, getHashMapFirstType("周杰伦")));
viewItemsArraylist.add(new lIstViewItem(0, getHashMapFirstType("周星驰")));
viewItemsArraylist.add(new lIstViewItem(1, getHashMapSecondType("猴子", "齐天大圣孙悟空"))); return viewItemsArraylist;
} // 第一种样式,只传输一个数据
private HashMap<String, Object> getHashMapFirstType(String firstTheme) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("Theme", firstTheme);
return hashMap;
} // 第二种样式,传输两个数据
private HashMap<String, Object> getHashMapSecondType(String secondTheme, String secondContent) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("Theme", secondTheme);
hashMap.put("Content", secondContent);
return hashMap;
} // 第三种样式,传输三个数据
private HashMap<String, Object> getHashMapThirdType(String threeTheme, String threeContent, String date) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("Theme", threeTheme);
hashMap.put("Content", threeContent);
hashMap.put("Date", date);
return hashMap;
} }

adapter中设置我们现实的条目

package com.example.listviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView; import java.util.ArrayList; /**
* Created by Ace on 2016/2/13.
*/
public class ListViewAdapter extends BaseAdapter {
private LayoutInflater mLayoutInflater;
private Context context;
private ArrayList<lIstViewItem> listDatas; public ListViewAdapter(Context context, ArrayList<lIstViewItem> listDatas) {
this.listDatas = listDatas;
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} // 返回当前布局的样式type
@Override
public int getItemViewType(int position) {
return listDatas.get(position).type;
} // 返回你有多少个不同的类型的数量
@Override
public int getViewTypeCount() {
return 3;
} @Override
public int getCount() {
return listDatas.size();
} @Override
public Object getItem(int position) {
return listDatas.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
lIstViewItem listItem = listDatas.get(position);
int Type = getItemViewType(position);//拿到当前条目的类型
ViewHolderfirstType viewHolderfirstType = null;
ViewHoldersecondType viewHoldersecondType = null;
ViewHolderThirdType viewHolderThirdType = null;
if (convertView == null) {//对类型进行判断
switch (Type) {
case 0:
viewHolderfirstType = new ViewHolderfirstType();
convertView = mLayoutInflater.inflate(R.layout.activity_first_type_item, null);
viewHolderfirstType.tv_first_theme = (TextView) convertView.findViewById(R.id.tv_first_theme);
viewHolderfirstType.tv_first_theme.setText(listItem.map.get("Theme").toString());
convertView.setTag(viewHolderfirstType); break;
case 1:
viewHoldersecondType = new ViewHoldersecondType();
convertView = mLayoutInflater.inflate(R.layout.activity_sencond_type_item, null);
viewHoldersecondType.tv_second_content = (TextView) convertView.findViewById(R.id.tv_second_content);
viewHoldersecondType.btn_second_theme = (Button) convertView.findViewById(R.id.btn_second_theme);
viewHoldersecondType.tv_second_content.setText(listItem.map.get("Theme").toString());
viewHoldersecondType.btn_second_theme.setText(listItem.map.get("Content").toString());
convertView.setTag(viewHoldersecondType); break;
case 2: viewHolderThirdType = new ViewHolderThirdType();
convertView = mLayoutInflater.inflate(R.layout.activity_third_type_item, null);
viewHolderThirdType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
viewHolderThirdType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
viewHolderThirdType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
viewHolderThirdType.et_three_theme.setText(listItem.map.get("Theme").toString());
viewHolderThirdType.tv_three_content.setText(listItem.map.get("Content").toString());
viewHolderThirdType.tv_three_time.setText(listItem.map.get("Date").toString());
convertView.setTag(viewHolderThirdType); break;
}
} else {
switch (Type) {
case 0:
viewHolderfirstType = (ViewHolderfirstType) convertView.getTag();
viewHolderfirstType.tv_first_theme.setText(listItem.map.get("Theme").toString());
break;
case 1:
viewHoldersecondType = (ViewHoldersecondType) convertView.getTag();
viewHoldersecondType.tv_second_content = (TextView) convertView.findViewById(R.id.tv_second_content);
viewHoldersecondType.btn_second_theme = (Button) convertView.findViewById(R.id.btn_second_theme);
viewHoldersecondType.tv_second_content.setText(listItem.map.get("Theme").toString());
viewHoldersecondType.btn_second_theme.setText(listItem.map.get("Content").toString());
break;
case 2:
viewHolderThirdType = (ViewHolderThirdType) convertView.getTag();
viewHolderThirdType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
viewHolderThirdType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
viewHolderThirdType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
viewHolderThirdType.et_three_theme.setText(listItem.map.get("Theme").toString());
viewHolderThirdType.tv_three_content.setText(listItem.map.get("Content").toString());
viewHolderThirdType.tv_three_time.setText(listItem.map.get("Date").toString());
break;
} }
return convertView;
} class ViewHolderfirstType {
TextView tv_first_theme;
} class ViewHoldersecondType {
TextView tv_second_content;
Button btn_second_theme;
} class ViewHolderThirdType {
EditText et_three_theme;
TextView tv_three_content;
TextView tv_three_time;
}
}

我的genymotion又挂了......................................    布局看viewholder就知道了..不写了

上一篇:iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)


下一篇:Swift3.0服务端开发(五) 记事本的开发(iOS端+服务端)