Android 4层可扩展列表视图-第4层在第2层下方打开,不';不要移动它
我一直在尝试创建一个简单的4级可扩展列表视图。它适用于3级,但当我加上4级时,它就搞砸了。第四级列表打开,但仅移动第三级列表,而不是第二级和第一级列表。所以第四级和第三级列表在第二级下消失 这是我的密码Android 4层可扩展列表视图-第4层在第2层下方打开,不';不要移动它,android,android-layout,Android,Android Layout,我一直在尝试创建一个简单的4级可扩展列表视图。它适用于3级,但当我加上4级时,它就搞砸了。第四级列表打开,但仅移动第三级列表,而不是第二级和第一级列表。所以第四级和第三级列表在第二级下消失 这是我的密码 public class MainActivity extends AppCompatActivity { public static final int FIRST_LEVEL_COUNT = 5; public static final int SECOND_LEVEL_C
public class MainActivity extends AppCompatActivity {
public static final int FIRST_LEVEL_COUNT = 5;
public static final int SECOND_LEVEL_COUNT = 5;
public static final int THIRD_LEVEL_COUNT = 5;
public static final int FOURTH_LEVEL_COUNT = 5;
private ExpandableListView expandableListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.mainList);
expandableListView.setAdapter(new ParentLevel(this));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parent(First) Level
public class ParentLevel extends BaseExpandableListAdapter {
private Context context;
public ParentLevel(Context context) {
this.context = context;
}
@Override
public Object getChild(int arg0, int arg1) {
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(MainActivity.this);
secondLevelELV.setAdapter(new SecondLevelAdapter(context));
secondLevelELV.setGroupIndicator(null);
return secondLevelELV;
}
@Override
public int getChildrenCount(int groupPosition) {
return SECOND_LEVEL_COUNT;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return FIRST_LEVEL_COUNT;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("FIRST LEVEL");
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Second Level
public class SecondLevelExpandableListView extends ExpandableListView {
public SecondLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public SecondLevelAdapter(Context context) {
this.context = context;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("SECOND LEVEL");
}
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return 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) {
ThirdLevelExpandableListView thirdLevelELV = new ThirdLevelExpandableListView(MainActivity.this);
thirdLevelELV.setAdapter(new ThirdLevelAdapter(context));
thirdLevelELV.setGroupIndicator(null);
return thirdLevelELV;
}
@Override
public int getChildrenCount(int groupPosition) {
return THIRD_LEVEL_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Third Level
public class ThirdLevelExpandableListView extends ExpandableListView {
public ThirdLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public ThirdLevelAdapter(Context context) {
this.context = context;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("THIRD LEVEL");
}
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return 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) {
FourthLevelExpandableListView fourthLevelELV = new FourthLevelExpandableListView(MainActivity.this);
fourthLevelELV.setAdapter(new FourthLevelAdapter(context));
fourthLevelELV.setGroupIndicator(null);
return fourthLevelELV;
}
@Override
public int getChildrenCount(int groupPosition) {
return FOURTH_LEVEL_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fourth Level
public class FourthLevelExpandableListView extends ExpandableListView {
public FourthLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class FourthLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public FourthLevelAdapter(Context context) {
this.context = context;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_fourth, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("FOURTH LEVEL");
}
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return 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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_fourth, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("FOURTH LEVEL");
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return FOURTH_LEVEL_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ExpandableListView android:id="@+id/mainList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</RelativeLayout>
row_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/eventsListEventRowText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textColor="@android:color/white"
android:textSize="18sp"/>
</LinearLayout>
row_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/eventsListEventRowText"
android:paddingLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="14sp"/>
</LinearLayout>
row_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:background="@android:color/holo_green_dark">
<TextView
android:id="@+id/eventsListEventRowText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="24sp"/>
</LinearLayout>
row_fourth.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:background="#d6cf1e">
<TextView
android:id="@+id/eventsListEventRowText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:textSize="24sp"/>
</LinearLayout>
有人能告诉我这里的问题和解决方案吗。尝试以下方法,即使根据您的要求,您也可以管理多个级别
尝试以下方法,即使根据您的要求,您也可以管理多个级别
看看这个对n级树非常有用的列表。你试过了吗?我不能让这个也工作。你必须定制它,我是说外观和填充。它工作得很好,需要花很多时间来理解和定制。看看这个对n级树有用的列表。你试过这个吗?我不能让这个也工作。你必须定制它,我是说外观和填充。它工作得很好,需要花很多时间来理解和定制。如果你发布URL,请尝试发布一些代码示例来解释URL是关于什么的。否则它将被视为垃圾邮件,并使您的答案不那么可信。欢迎使用堆栈溢出!一个链接到一个潜在的解决方案总是受欢迎的,但请你这样做,你的同事用户将有一些想法,它是什么,为什么会在那里。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。考虑到仅仅是一个指向外部站点的链接是一个可能的原因。如果你发布了URL,试着发布一个小代码示例来解释URL是关于什么的。否则它将被视为垃圾邮件,并使您的答案不那么可信。欢迎使用堆栈溢出!一个链接到一个潜在的解决方案总是受欢迎的,但请你这样做,你的同事用户将有一些想法,它是什么,为什么会在那里。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。考虑到仅仅是一个指向外部站点的链接是一个可能的原因。