@linux1s1s
2019-02-15T16:36:53.000000Z
字数 5131
阅读 4502
AndroidWidget
2015-07
先看下面这个ListView
该如何实现上面这个需求?
我们知道ListView
数据绑定类BaseAdapter
有个getItemViewType
,这个方法完全满足上面的要求,下面写个简单的Demo实现上面的需求。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view"
android:divider="@color/list_view_divider_color"
android:dividerHeight="1dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/item_list_view_type_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="@dimen/item_list_view_type_title_size"
android:padding="8dp"
tools:ignore="RtlHardcoded,RtlSymmetry" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="@dimen/item_list_view_type_title_size"
android:padding="8dp"
android:drawablePadding="10dp"
android:drawableRight="@mipmap/ic_launcher"
android:id="@+id/item_list_view_type_content"
tools:ignore="RtlHardcoded">
</TextView>
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListViewTypeActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview_type);
initView();
}
private void initView()
{
ListView listView = (ListView) findViewById(R.id.list_view);
List<ItemFeed> data = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
ItemFeed feed = new ItemFeed();
if (i % 5 == 0)
{
feed.setItemType(ItemType.TITLE);
feed.setText("TypeListView_Item: Title" + i);
}
else
{
feed.setItemType(ItemType.CONTENT);
feed.setText("TypeListView_Item: Content" + i);
}
data.add(feed);
}
BaseAdapter listAdapter = new TypedListAdapter(data);
listView.setAdapter(listAdapter);
}
private class TypedListAdapter extends BaseAdapter
{
private List<ItemFeed> data;
public TypedListAdapter(List<ItemFeed> datas)
{
this.data = datas;
}
@Override
public int getViewTypeCount()
{
return 2;
}
@Override
public int getItemViewType(int position)
{
final ItemFeed feed = data.get(position);
if (feed != null)
{
return feed.getItemType().getValue();
}
return super.getItemViewType(position);
}
@Override
public int getCount()
{
return data == null ? 0 : data.size();
}
@Override
public Object getItem(int position)
{
return data == null ? null : data.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ItemFeed feed = data.get(position);
final LayoutInflater inflater = LayoutInflater.from(ListViewTypeActivity.this);
ItemTitleHolder titleHolder;
ItemContentHolder contentHolder;
int type = getItemViewType(position);
switch (type)
{
case 0:
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item_listview_type_title, parent, false);
titleHolder = new ItemTitleHolder(convertView);
convertView.setTag(titleHolder);
}
else
{
titleHolder = (ItemTitleHolder) convertView.getTag();
}
titleHolder.refreshUI(feed);
break;
case 1:
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item_listview_type_content, parent, false);
contentHolder = new ItemContentHolder(convertView);
convertView.setTag(contentHolder);
}
else
{
contentHolder = (ItemContentHolder) convertView.getTag();
}
contentHolder.refreshUI(feed);
break;
}
return convertView;
}
}
private class ItemTitleHolder
{
private TextView title;
public ItemTitleHolder(View convertView)
{
if (convertView != null)
{
title = (TextView) convertView.findViewById(R.id.item_list_view_type_title);
}
}
public void refreshUI(ItemFeed feed)
{
if (title != null && feed != null)
{
title.setText(feed.getText());
}
}
}
private class ItemContentHolder
{
private TextView content;
public ItemContentHolder(final View convertView)
{
if (convertView != null)
{
content = (TextView) convertView.findViewById(R.id.item_list_view_type_content);
}
}
public void refreshUI(ItemFeed feed)
{
if (content != null && feed != null)
{
content.setText(feed.getText());
}
}
}
private class ItemFeed
{
public ItemType getItemType()
{
return itemType;
}
public void setItemType(ItemType itemType)
{
this.itemType = itemType;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
private ItemType itemType;
private String text;
}
private enum ItemType
{
TITLE(0),
CONTENT(1);
public int getValue()
{
return value;
}
private int value;
ItemType(int value)
{
this.value = value;
}
}
}