我想实现类似于pirate Bay使用的节标题
基本上是这样的Listview:
标题名称 项目清单 项目清单 项目清单 项目清单 标题名称 项目清单 项目清单 列表项目
实现这一目标的最佳方式是什么?我已阅读部分标题和合并适配器,但它们不能满足我的需求......我将如何实现节标题
答案 0 :(得分:5)
以下是我解决它的方法:
我教我的自定义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++;
}
答案 1 :(得分:0)
只需使用一个ExpandableListView,每个标题包含一个组。