我正在尝试创建一个嵌套的expandable list
(3层)。但使用以下代码不能看到子类别列表(第3层)。 getChildCount is > 1
但getChildview
从未在SecondLevelAdapter
中调用。
-> menu
-> Category
-> sub category
class Menu{
List<Category> categories;
}
class Category{
List<SubCategory> subcategories;
}
public class ParentLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<NavigationMenu> mListDataHeader;
public ParentLevelAdapter(Context mContext, List<NavigationMenu> mListDataHeader) {
this.mContext = mContext;
this.mListDataHeader = mListDataHeader;
}
@Override
public NavCategory getChild(int groupPosition, int childPosition) {
return mListDataHeader.get(groupPosition).categories.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);
secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, getChild(groupPosition,childPosition)));
return secondLevelExpListView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mListDataHeader.get(groupPosition).categories.size();
}
@Override
public NavigationMenu getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.mListDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
class ViewHolder{
ImageView icon;
TextView name;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_item_view, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(getGroup(groupPosition).name);
holder.icon.setImageResource(getGroup(groupPosition).iconResoluteID);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<NavSubcategory> mListDataHeader;
private final NavCategory navCategory;
public SecondLevelAdapter(Context mContext, NavCategory navCategory) {
this.mContext = mContext;
this.mListDataHeader = navCategory.sub_categories;
this.navCategory = navCategory;
}
@Override
public NavSubcategory getChild(int groupPosition, int childPosition) { //ignore group
return mListDataHeader.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
class ChildViewHolder{
TextView name;
}
class GroupViewHolder{
TextView name;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_second_item, parent, false);
holder = new ChildViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(holder);
}
else{
holder = (ChildViewHolder) convertView.getTag();
}
holder.name.setText(getChild(groupPosition,childPosition).sub_category_name);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public int getChildrenCount(int groupPosition) {
return mListDataHeader.size();
}
@Override
public NavCategory getGroup(int groupPosition) {
return navCategory;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_second_group_item, parent, false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(groupViewHolder);
}
else{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.name.setText(getGroup(groupPosition).category_name);
return convertView;
}
}
答案 0 :(得分:1)
在ParentLevelAdapter的getChildView中替换
final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);
带
final CustomExpandableListView customExpandableListView = new CustomExpandableListView(this.mContext);
其中CustomExpandableListView是:
public class CustomExpandableListView extends ExpandableListView {
Context context;
public CustomExpandableListView(Context _context) {
super(_context);
context = _context;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int screenWidth = (int)(metrics.widthPixels); //or whatever you need here for width of the row
widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}