Android ListView显示不同样式的item
先look图

我们再使用listview时,listview的item大多时候都是一种样式,在很多app中也很常见,但有时候根据需求,可能数据的数量不一样,同个类型的数据显示的位置不同,亦或者有的item需要图片,有的不需要,但是这些又必须在同一个listview中显示,这时我们就需要在listview中显示多种样式的item,首先我们需要考虑的是如何将不同数量的数据装载到ArrayList<~>中呢,先看看下面的listViewItem,。
package com.example.keranbin.myapplication; import java.util.HashMap;
import java.util.Map; 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;
}
}
我们通过自定义一个listItem,即可将所有不同类型,不同数量的数据先组装成统一类型listItem即可,然后用arrayList.add(listitem)即可。
/**
* 这里我们用三种不同的样式进行测试
**/
private ArrayList<lIstViewItem> getDatas() { viewItemsArraylists = new ArrayList<lIstViewItem>();
viewItemsArraylists.add(new lIstViewItem(2, getHashMapThreeType("汪星人", "汪星人喜欢吃骨头", "2015-10-18")));
viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("喵星人", "喵星喜欢吃鱼")));
viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("猴子")));
viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("老虎")));
viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("老母鸡", "老母鸡喜欢吃虫子")));
return viewItemsArraylists;
} //第一种样式,只传输一个数据
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> getHashMapThreeType(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;
}
剩下的就是listViewAdapter的事情啦,和显示一种样式的listViewAdapter不同的一点是我们重写实现父类baseAdapter的两个方法。
//返回当前布局的样式type
@Override
public int getItemViewType(int position) {
return listDatas.get(position).type;
} //返回你有多少个不同的布局
@Override
public int getViewTypeCount() {
return 3;
}
然后在getView中根据需要进行判断决定显示那种样式即可
package com.example.keranbin.myapplication; 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 keranbin on 2015/10/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;
ViewHolderThreeType viewHolderThreeType = 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); //如果要复用,不同于一种样式的listView ,这样的写法是错误的,具体原因看下面第一个小哥评论的说法
convertView.setTag(R.id.viewHolderfirstType, viewHolderfirstType);
break;
case 1:
viewHoldersecondType = new ViewHoldersecondType();
convertView = mLayoutInflater.inflate(R.layout.activity_second_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); //如果要复用,不同于一种样式的listView ,这样的写法是错误的,具体原因看下面第一个小哥评论的说法
convertView.setTag(R.id.viewHoldersecondType, viewHoldersecondType);
break;
case 2: viewHolderThreeType = new ViewHolderThreeType();
convertView = mLayoutInflater.inflate(R.layout.activity_three_type_item, null);
viewHolderThreeType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
viewHolderThreeType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
viewHolderThreeType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
viewHolderThreeType.et_three_theme.setText(listItem.map.get("Theme").toString());
viewHolderThreeType.tv_three_content.setText(listItem.map.get("Content").toString());
viewHolderThreeType.tv_three_time.setText(listItem.map.get("Date").toString());
// convertView.setTag(viewHolderThreeType);//如果要复用,不同于一种样式的listView ,这样的写法是错误的,具体原因看下面第一个小哥评论的说法
convertView.setTag(R.id.viewHolderthreeType, viewHolderThreeType);
break;
}
} else {
switch (Type) {
case 0:
viewHolderfirstType = (ViewHolderfirstType) convertView.getTag(R.id.viewHolderfirstType);
viewHolderfirstType.tv_first_theme.setText(listItem.map.get("Theme").toString());
break;
case 1:
viewHoldersecondType = (ViewHoldersecondType) convertView.getTag(R.id.viewHoldersecondType);
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:
viewHolderThreeType = (ViewHolderThreeType) convertView.getTag(R.id.viewHolderthreeType);
viewHolderThreeType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
viewHolderThreeType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
viewHolderThreeType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
viewHolderThreeType.et_three_theme.setText(listItem.map.get("Theme").toString());
viewHolderThreeType.tv_three_content.setText(listItem.map.get("Content").toString());
viewHolderThreeType.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 ViewHolderThreeType {
EditText et_three_theme;
TextView tv_three_content;
TextView tv_three_time;
}
}
第一种样式页面组件主要是一个TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#454346"> <TextView
android:id="@+id/tv_first_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30dp"
android:textStyle="bold" /> </RelativeLayout>
</RelativeLayout>
第二种样式页面组件主要是一个TextView和一个button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#834385"> <Button
android:id="@+id/btn_second_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp" /> <TextView
android:id="@+id/tv_second_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="30dp" />
</RelativeLayout>
</RelativeLayout>
第三种样式页面组件主要是两个TextView和一个EditText.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#6434ff"> <EditText
android:id="@+id/et_three_theme"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textSize="30dp" /> <TextView
android:id="@+id/tv_three_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:textSize="30dp" /> <TextView
android:id="@+id/tv_three_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/et_three_theme"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/et_three_theme"
android:textSize="20dp" /> </RelativeLayout> </RelativeLayout>
activity_main.xml文件非常简单,就一个listView。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"> <ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
</RelativeLayout>
下面是MainActivity的代码
package com.example.keranbin.myapplication; import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView; import java.util.ArrayList;
import java.util.HashMap; public class MainActivity extends Activity {
private ListView listView; //页面listview
private ListViewAdapter listViewAdapter; //listview适配器
private ArrayList<lIstViewItem> viewItemsArraylists; //Arraylist主要装载的是传给适配器的数据集合 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化页面组件及一些数据
initView();
//为listview设置适配器
ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas());
listView.setAdapter(listViewAdapter);
} //初始化页面组件及一些数据
private void initView() {
listView = (ListView) this.findViewById(R.id.listView);
listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas());
} /**
* 这里我们用三种不同的样式进行测试
**/
private ArrayList<lIstViewItem> getDatas() { viewItemsArraylists = new ArrayList<lIstViewItem>();
viewItemsArraylists.add(new lIstViewItem(2, getHashMapThreeType("汪星人", "汪星人喜欢吃骨头", "2015-10-18")));
viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("喵星人", "喵星喜欢吃鱼")));
viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("猴子")));
viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("老虎")));
viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("老母鸡", "老母鸡喜欢吃虫子")));
return viewItemsArraylists;
} //第一种样式,只传输一个数据
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> getHashMapThreeType(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;
} }
Android ListView显示不同样式的item的更多相关文章
- Android ListView 之 SimpleAdapter 二 (包含 item 中按钮监听)
1 MainActivity.java package com.myadapter; import java.util.ArrayList; import java.util.HashMap; ...
- Android ListView显示底部的分割线
有些时候,我们会提出这样的需求,希望ListView显示底部(顶部)的分割线,这样做,会使得UI效果更加精致,如下图所示: 如果搜索资料,大家会搜到一堆相关的方法,最多的莫过于设置listview的f ...
- android listview实现点击某个item后使其显示在屏幕顶端
在该listview的点击事件中加入一下代码即可 listView.setSelectionFromTop(position, 0);
- ListView显示多种类型的item
ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似 这是MainActivity:,MainActivity的布局就是一个ListView public class Mai ...
- 【转】Android ListView加载不同的item布局
原创教程,转载请保留出处:http://www.eoeandroid.com/thread-72369-1-1.html 最近有需求需要在listView中载入不同的listItem布局,开始 ...
- Android listview 制作表格样式+由下往上动画弹出效果实现
效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...
- Android ListView显示访问WebServices返回的JSON结果
1.WebServices的返回结果 2.ListView内容布局代码 <?xml version="1.0" encoding="utf-8"?> ...
- Android ListView 显示多种数据类型
ListView往往可能会有不同的数据类型,单类型的数据可能运用会比较少些,这也是最近项目中的一个需求{在发送消息的时候,需要选择联系人,而联系人列表由英文字母索引+联系人组成},上一篇文章只是一个基 ...
- Android ListView之选中(撤销选中)Item
在ContactListActivity中,点击未选中的item将其选中,再点击已选中的item撤销其选中 public void onItemClick(AdapterView<?> p ...
随机推荐
- 201871010118-唐敬博《面向对象程序设计(java)》第八周学习总结
博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...
- shell脚本特殊符号——单引号、双引号、反引号、反斜杠
1. 单引号 ( '' ) 被单引号用括住的内容,将被视为单一字串. 2. 双引号 ( " " ) 被双引号用括住的内容,将被视为单一字串.双引号只要求忽略大多数,具体说,括在双 ...
- django之表设计、路由层等
图书管理系统表的设计 from django.db import models # Create your models here. class Book(models.Model): title = ...
- File.createNewFile和 File.createTempFile比较和区别
原文地址:http://wzhiju.iteye.com/blog/1119037 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File.createNe ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- mysql 类型自动化转换问题
mysql 类型自动化转换问题 背景 有个业务需求,使用到find_in_set函数,简单贴下,如下: SELECT FIND_IN_SET('b','a,b,c,d'); //返回值为2,即第2个 ...
- asp.net mvc移除X-AspNet-Version、X-AspNetMvc-Version、Server
asp.net mvc程序部署到IIS,,返回的HTTP头中包含Server, X-Powered-By, 和 X-AspNet-Version.X-AspNet-Version信息. 这些信息有时给 ...
- Oracle函数sys_connect_by_path用法
sys_connect_by_path函数是为了配合递归查询的函数,递归查询可以参考我之前的博客:https://blog.csdn.net/u014427391/article/details/84 ...
- POJ 1306 暴力求组合数
Combinations Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11049 Accepted: 5013 Des ...
- Peewee之playhouse中的数据库连接池
参见 http://note.youdao.com/noteshare?id=104255d91b5a00d716a713ae36e911fd 目前在学习Python库的源码,这是第二篇,比较简单的功 ...