android 多条目筛选,Android ListView专题之七  在ListView中实现多种视条目

在imdb中有这样的界面:

a4c26d1e5885305701be709a3d33442f.png

画红框的部分其实都是ListView中的条目,不过它们是不同的View。实现了一个类似的界面效果:

a4c26d1e5885305701be709a3d33442f.png

其中日期标题部分视图布局:

encoding=”utf-8″?>

android:orientation=”vertical”

android:layout_width=”fill_parent”

android:layout_height=”10dip”

android:background=”@drawable/section_background”>

android:layout_width=”fill_parent”

android:layout_height=”match_parent” />

带图片的条目布局部分:

encoding=”utf-8″?>

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:orientation=”horizontal” >

android:src=”@drawable/p”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content” />

android:layout_width=”wrap_content”

android:layout_height=”wrap_content” />

问题在于,如何在ListView中既有标题条目又有内容条目。

这里用到了设计模式中的Iterator模式。在java代码中示例有Iterator,可以迭代ArrayList,HashSet等不同的数据结构对象。

这里设计了一个类族:

a4c26d1e5885305701be709a3d33442f.png

ListElement是接口:

package com.easymorse.listview.customer;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

public interface ListElement {

public int getLayoutId();

public boolean isClickable();

public View getViewForListElement(LayoutInflater

layoutInflater,

Context context, View view);

}

其中:

getLayoutId()返回布局的值;

isClickable()返回是否可点击;

getViewForListElement()返回视图对象。

这个接口有两个实现:

SectionListElement,用于实现标题条目;

ContentListElement,用于实现内容条目。

见SectionListElement代码:

package com.easymorse.listview.customer;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.TextView;

public class SectionListElement implements

ListElement {

private String text;

public void setText(String text) {

this.text = text;

}

@Override

public int getLayoutId() {

return R.layout.section;

}

@Override

public boolean isClickable() {

return false;

}

@Override

public View getViewForListElement(LayoutInflater

layoutInflater,

Context context, View view) {

LinearLayout layout = (LinearLayout)

layoutInflater.inflate(getLayoutId(), null);

TextView textView=(TextView)

layout.findViewById(R.id.section_title);

textView.setText(text);

return layout;

}

}

见ContentListElement代码:

package com.easymorse.listview.customer;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ContentListElement implements

ListElement {

private String title;

public void setTitle(String title) {

this.title = title;

}

@Override

public int getLayoutId() {

return R.layout.item;

}

@Override

public View getViewForListElement(LayoutInflater

layoutInflater,

Context context, View view) {

LinearLayout layout = (LinearLayout) layoutInflater.inflate(

getLayoutId(), null);

TextView textView = (TextView)

layout.findViewById(R.id.title);

textView.setText(title);

return layout;

}

@Override

public boolean isClickable() {

return true;

}

}

ListView需要ListAdapter的实现。在这里是直接集成BaseAdapter来实现的。用于交给ListView生成出列表。代码:

package com.easymorse.listview.customer;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

public class CustomerListAdapter extends

BaseAdapter {

private Context context;

protected

ArrayList

resultList;

private LayoutInflater layoutInflater;

public CustomerListAdapter(Context context) {

super();

this.context = context;

this.layoutInflater = (LayoutInflater) context

.getSystemService(“layout_inflater”);

this.resultList = new

ArrayList();

}

@Override

public int getCount() {

return this.resultList.size();

}

@Override

public Object getItem(int position) {

return this.resultList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View view, ViewGroup parent)

{

return this.resultList.get(position).getViewForListElement(

layoutInflater, context, view);

}

public void

addList(List elements)

{

this.resultList.addAll(elements);

}

@Override

public boolean isEnabled(int position) {

return this.resultList.get(position).isClickable();

}

public void addSectionHeaderItem(String text)

{

SectionListElement element = new SectionListElement();

element.setText(text);

this.resultList.add(element);

}

}

在Activity中创建CustomerListAdapter以及设置它的代码部分:

CustomerListAdapter adapter = new

CustomerListAdapter(this);

adapter.addSectionHeaderItem(“2002-3-1″);

ArrayList elements = new

ArrayList();

for (int i = 0; i < 5; i++) {

ContentListElement element = new ContentListElement();

element.setTitle(“哈利波特第” + (i+1) + “集”);

elements.add(element);

}

adapter.addList(elements);

adapter.addSectionHeaderItem(“2002-2-2″);

elements = new

ArrayList();

for (int i = 0; i < 3; i++) {

ContentListElement element = new ContentListElement();

element.setTitle(“指环王第” + (i+1) + “集”);

elements.add(element);

}

adapter.addList(elements);

this.setListAdapter(adapter);

这里ListActivity,还需要注意两件事情,Activity要继承ListActivity。另外,在layout中:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值