我发现很多关于三级可扩展的问题。我试过我无法扩展它,它没有显示。
我需要在我的应用中保留三级可扩展列表视图。这些值来自db。我能够实现正常的可扩展本机组件。我试过三级可扩展它不起作用。
MyExpandableListAdapter mAdapter;
private void createExpandableListViewDialog() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewList = MainActivity.this.getLayoutInflater().inflate(
R.layout.custom_dialog, null);
setContentView(viewList);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
elvForDialog = (ExpandableListView) viewList
.findViewById(R.id.elvForDialog);
mAdapter = new MyExpandableListAdapter(MainActivity.this, listchapt,
null);
elvForDialog.setIndicatorBounds(width - 20, width);// 130
elvForDialog.setAdapter(mAdapter);
elvForDialog.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int arg2, long arg3) throws RuntimeException {
try{
//String pos = elvForDialog.getItemAtPosition(arg2).toString();
String pos = (String)mAdapter.getGroup(arg2);
System.out.println("position=" + pos);
//List<String> children = new ArrayList<String>();
children = new ArrayList<String>();
if (mAdapter.getChildrenCount(arg2) == 0) {
dbHelper.openDataBase();
children = dbHelper.GetSubchapt(pos);
dbHelper.close();
mAdapter.addChildren(arg2, children);
}
System.out.println("explist" + listSubchapt);
try {
Log.v("LH", "ssetOnGroupClickListener");
Log.v("LH", "" + viewListLastSelected.toString());
Log.v("LH",
"" + ((TextView) viewListLastSelected).getText());
} catch (Exception e) {
Log.v("LH", "ERROR@onCreate: " + e.toString());
}
}
catch(Exception e) {
System.out.println("e=" +e);
}
return false;
}
});
elvForDialog.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) throws RuntimeException {
//currentChapt = listchapt.get(arg2).toString();
//currentSubchapt = listNames.get(arg2).get(arg3).toString();
String currentChapt = (String)mAdapter.getGroup(arg2);
String currentSubchapt = (String)mAdapter.getChild(arg2, arg3);
System.out.println("Yes it shows child " + currentChapt
+ currentSubchapt);
// createExpandableListViewDialog(1);
Intent in = new Intent(MainActivity.this, WebContent.class);
in.putExtra("chapterid", currentChapt);
in.putExtra("subchaptid", currentSubchapt);
startActivity(in);
elvForDialog.clearChildFocus(viewListLastSelected);
try {
viewListLastSelected.setBackgroundDrawable(null);
((TextView) viewListLastSelected).setTextColor(Color.GRAY);
} catch (Exception e) {
}
((TextView) arg1).setTextColor(Color.WHITE);
viewListLastSelected = arg1;
return false;
}
});
elvForDialog.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = mAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
elvForDialog.collapseGroup(i);
}
}
}
});
MyExpandableListAdapter类
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
Activity activity;
List<String> listGroup = new ArrayList<String>();
// List<List<String>> listChild = new ArrayList<List<String>>();
SparseArray<List<String>> listChild = new SparseArray<List<String>>();
private int _posGroup = 0;
private int _posChild = 0;
public MyExpandableListAdapter(Activity a, List<String> group,
List<List<String>> children) {
super();
this.activity = a;
this.listGroup = group;
// this.listChild = children;
}
public void addChildren(int groupPosition, List<String> children) {
this.listChild.put(groupPosition, children);
}
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
// Log.v("LH", "ChildID: " + childPosition);
_posGroup = groupPosition;
_posChild = childPosition;
return childPosition;
}
public int getChildrenCount(int groupPosition) {
if (listChild.get(groupPosition) == null) {
Log.e("mAdapter.getChildrenCount",
"Tried to access children which have not yet been added");
return 0;
} else
return listChild.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//String string = listChild.get(groupPosition).get(childPosition);
//View view = getGenericChildView(string);
/*TextView text = (TextView) view;
if (this._posGroup == groupPosition && this._posChild == childPosition) {
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
}*/
//view.setBackgroundResource(R.drawable.menu_submenubg);
CustExpListview SecondLevelexplv = new CustExpListview(activity);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
//return view;
}
// group method stub
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
public int getGroupCount() {
return listGroup.size();
}
public long getGroupId(int groupPosition) {
// Log.v("LH", "GroupID: " + groupPosition);
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String string = listGroup.get(groupPosition);
View result = getGenericGroupView(string);
result.setBackgroundResource(R.drawable.menu_hoverbg);
return result;
}
public TextView getGenericChildView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height = 73; // 73,50
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(50, 0, 0, 0);
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
text.setTextSize(15);
text.setText(s);
return text;
}
public TextView getGenericGroupView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height = 60;
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(20, 0, 0, 0);
text.setTextSize(20);
text.setTextColor(Color.WHITE);
text.setText(s);
return text;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class CustExpListview extends ExpandableListView {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,
MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private int _posGroup = 0;
private int _posChild = 0;
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
// Log.v("LH", "ChildID: " + childPosition);
_posGroup = groupPosition;
_posChild = childPosition;
return childPosition;
}
public int getChildrenCount(int groupPosition) {
if (listChild.get(groupPosition) == null) {
Log.e("mAdapter.getChildrenCount",
"Tried to access children which have not yet been added");
return 0;
} else
return listChild.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String string = listChild.get(groupPosition).get(childPosition);
View view = getGenericChildView(string);
TextView text = (TextView) view;
if (this._posGroup == groupPosition
&& this._posChild == childPosition) {
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
}
view.setBackgroundResource(R.drawable.menu_submenubg);
// return SecondLevelexplv;
return view;
}
// group method stub
public Object getGroup(int groupPosition) {
return listChild.get(groupPosition);
}
public int getGroupCount() {
return listChild.size();
}
public long getGroupId(int groupPosition) {
// Log.v("LH", "GroupID: " + groupPosition);
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String string = listGroup.get(groupPosition);
View result = getGenericGroupView(string);
result.setBackgroundResource(R.drawable.menu_hoverbg);
return result;
}
public TextView getGenericChildView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height = 73; // 73,50
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(50, 0, 0, 0);
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
text.setTextSize(15);
text.setText(s);
return text;
}
public TextView getGenericGroupView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height = 60;
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(20, 0, 0, 0);
text.setTextSize(20);
text.setTextColor(Color.WHITE);
text.setText(s);
return text;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
这会扩展父级别项目,但是,它会生成父级的重复项目。我该怎么做呢?这是正确的方法,但我的适配器坏了,因此生成重复的项目?
我只是找不到我在这里缺少的东西......先谢谢。
答案 0 :(得分:0)
您需要做的是在显示之前使用CustomExpandableListView
衡量其children
和group
。
尝试使用以下代码。
public class CustomExpandableListView extends ExpandableListView {
boolean expanded = true;
public CustomExpandableListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomExpandableListView(Context context) {
super(context);
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setIsExpanded(boolean expanded) {
this.expanded = expanded;
}
}