我想实现类似于海盗湾使用的部分标题
基本上是这样的Listview:
标题 名称列表 项目列表 项目列表 项目列表 项目标题 名称列表 项目列表 项目列表 项目
实现这一目标的最佳方法是什么?我已经阅读了节标题和合并适配器,但它们不能满足我的需要......我将如何实现节标题
我想实现类似于海盗湾使用的部分标题
基本上是这样的Listview:
标题 名称列表 项目列表 项目列表 项目列表 项目标题 名称列表 项目列表 项目列表 项目
实现这一目标的最佳方法是什么?我已经阅读了节标题和合并适配器,但它们不能满足我的需要......我将如何实现节标题
这是我解决它的方法:
我教我的自定义 ListAdapter 返回标题行和详细信息行,这涉及覆盖 ListAdapter 中的 getViewTypeCount() 和 getItemViewType() 方法,而且我得到了 getView() 以了解行类型之间的区别。下面的代码应该给你一个提示.. ListViewAdapter 的代码(注释大部分)
public class ListViewAdapter extends ArrayAdapter<ListViewItemModel> {
public ListViewAdapter(Context context) {
super(context, 0);
}
public void addHeader(int title) { //expects The Title for the header as an Arugment to it
add(new ListViewItemModel(title, -1, true));//add the object to the Bottom of the array
}
public void addItem(int title, int icon) {
add(new ListViewItemModel(title, icon, false));
}
public void addItem(ListViewItemModel itemModel) {
add(itemModel);
}
@Override
public int getViewTypeCount() { //Returns the number of types of Views that will be created by getView(int, View, ViewGroup).
return 2; //we will create 2 types of views
}
@Override
public int getItemViewType(int position) { //framework calls getItemViewType for row n, the row it is about to display.
//Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
return getItem(position).isHeader ? 0 : 1; // get position passes (n) and accertain is its a header or not
}
@Override
public boolean isEnabled(int position) {
return !getItem(position).isHeader;
}
public static class ViewHolder {
public final TextView textHolder;
public final ImageView imageHolder;
public ViewHolder(TextView text1, ImageView image1) {
this.textHolder = text1;
this.imageHolder = image1;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
//Abstract View --> Get a View that displays the data at the specified position in the data set.
ListViewItemModel item = getItem(position);
ViewHolder holder = null;
View view = convertView;
if (view == null) {
int layout = R.layout.list_view_row;
if (item.isHeader)
layout = R.layout.list_view_row_header;
view = LayoutInflater.from(getContext()).inflate(layout, null);
TextView text1 = (TextView) view.findViewById(R.id.menurow_title);
ImageView image1 = (ImageView) view.findViewById(R.id.menurow_icon);
view.setTag(new ViewHolder(text1, image1));
}
if (holder == null && view != null) {
Object tag = view.getTag();
if (tag instanceof ViewHolder) {
holder = (ViewHolder) tag;
}
}
if (item != null && holder != null) {
if (holder.textHolder != null)
holder.textHolder.setText(item.title);
if (holder.imageHolder != null) {
if (item.iconRes > 0) {
holder.imageHolder.setVisibility(View.VISIBLE);
holder.imageHolder.setImageResource(item.iconRes);
} else {
holder.imageHolder.setVisibility(View.GONE);
}
}
}
return view;
}
}
以及供 ArrayAdapter 使用的模型
public class ListViewItemModel {
public int title;
public int iconRes;
public boolean isHeader;
public ListViewItemModel(int title, int iconRes, boolean header) {
this.title = title;
this.iconRes = iconRes;
this.isHeader = header;
}
public ListViewItemModel(int title, int iconRes) {
this(title, iconRes, false);
}
}
现在在我将在导航中使用此自定义列表视图的课程中
ListViewAdapter mAdapter = new ListViewAdapter(this);// Add First Header
mAdapter.addHeader(R.string.menu_data);
menu_One = getResources().getStringArray(R.array.menu_one); // Load list view data array strings
String[] menuOneIcons = getResources().getStringArray(R.array.menu_two_one);// Load list view image array strings
int oneIcons = 0;
for (String item : menuItemsData) { //enhanced For loop, iterate on elements from the collection named menuItemsData
int id_menu_one = getResources().getIdentifier(item, "string",this.getPackageName());
int id_menu_one_icons = getResources().getIdentifier(menuOneIcons[oneIcons], "drawable",this.getPackageName());
ListViewItemModel mItem = new ListViewItemModel(id_data_title,id_data_icon);
mAdapter.addItem(mItem);
oneIcons++;
}
// Add second header
mAdapter.addHeader(R.string.menu_two); //second Header here
menu_Two = getResources().getStringArray(R.array.menu_two);
String[] menuTwoIcons = getResources().getStringArray(R.array._menu_two_icons);
int twoIcons = 0;
for (String item : menu_Two) {
int id_menu_two = getResources().getIdentifier(item, "string",this.getPackageName());
int id_menu_two_icons = getResources().getIdentifier(enuTwoIcons[twoIcons], "drawable",this.getPackageName());
// creating drawer menu model
ListViewItemModel mItem = new ListViewItemModel(id_menu_two,id_menu_two_icons);
mAdapter.addItem(mItem);
twoIcons++;
}
只需使用每个标题一组的 ExpandableListView。