0
我發現了很多關於三級可擴展的問題。我試過了,我不能展開它,它沒有顯示。三級可擴展列表android
我需要在我的應用程序中保持三個級別的可擴展列表視圖..這些值取自數據庫。我能夠實現正常的可擴展本地組件。我嘗試過三級可擴展它不工作。
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", "[email protected]: " + 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;
}
}
}
這將展開父級別項目,但它產生的家長重複的項目。我如何正確地做到這一點?這是正確的方式,但我的適配器已損壞,因此生成重複的項目?
我只是找不到我在這裏失蹤...在此先感謝。
你有最新的Google Play圖書館服務? – Piyush
是的,我有@PiYusHGuPtA –
@Chintan Rathod它不是我的問題。對於無法正確分配列表來分組視圖和二級適配器中的子視圖。數值取自數據庫 –