为代码转储道歉 - 如果您更喜欢 github 版本,这是我正在使用的代码: https ://github.com/Barbelos/ThreeLevelExpandableListVIew
该列表工作正常,除了一件事-我可以想出在 onMeasure 中设置高度的唯一方法是测量列表扩展为的 RelativeLayout 的大小-因此,如果列表大于屏幕,则可以滚动它。
出于某种原因(我不太了解代码),第二级扩展得很好——你可以在那里放尽可能多的项目,它只会扩展以适应。问题发生在第三层 - 项目在我看来就像屏幕高度一样被截断。它们还在那里——如果你按住屏幕的其他部分,你可以滚动第三级,但这不是很直观。任何人都可以找到一种方法来完成这项工作吗?我尝试更改 xml 但无处可去,而 MeasureSpec.UNSPECIFIED 似乎与我正在尝试做的完全相反......
MainActivity.java:
package com.example.threeleveltest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
public static CustomExpandableListView list;
static int ht;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus == true) {
RelativeLayout parent= (RelativeLayout) findViewById(R.id.parent);
ht = parent.getHeight();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int noObjectsLevel1= 15;
int noObjectsLevel2= 24;
int noObjectsLevel3= 57;
List<Object> objectsLvl1= new ArrayList<Object>();
for (int i=0; i<noObjectsLevel1; i++) {
List<Object> objectsLvl2= new ArrayList<Object>();
for (int j=0; j<noObjectsLevel2; j++) {
List<Object> objectsLvl3= new ArrayList<Object>();
for (int k=0; k<noObjectsLevel3; k++) {
objectsLvl3.add(new Object("lvl3_"+String.valueOf(k), null));
}
objectsLvl2.add(new Object("lvl2_"+String.valueOf(j), objectsLvl3));
}
objectsLvl1.add(new Object("lvl1_"+String.valueOf(i), objectsLvl2));
}
RelativeLayout parent= (RelativeLayout) findViewById(R.id.parent);
list= new CustomExpandableListView(this);
Adapter adapter= new Adapter(this, objectsLvl1);
list.setAdapter(adapter);
parent.addView(list);
}
}
class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/*
* Adjust height
*/
int h=MainActivity.ht;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
适配器.java:
package com.example.threeleveltest;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class Adapter extends BaseExpandableListAdapter {
private List<Object> objects;
private Activity activity;
private LayoutInflater inflater;
public Adapter(Activity activity, List<Object> objects) {
this.objects= objects;
this.activity= activity;
this.inflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return objects.get(groupPosition).getObjects().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) {
Object object= (Object) getChild(groupPosition, childPosition);
CustomExpandableListView subObjects= (CustomExpandableListView) convertView;;
if (convertView==null) {
subObjects= new CustomExpandableListView(activity);
}
Adapter2 adapter= new Adapter2(activity, object);
subObjects.setAdapter(adapter);
return subObjects;
}
@Override
public int getChildrenCount(int groupPosition) {
return objects.get(groupPosition).getObjects().size();
}
@Override
public Object getGroup(int groupPosition) {
return objects.get(groupPosition);
}
@Override
public int getGroupCount() {
return objects.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Object object= (Object) getGroup(groupPosition);
if (convertView==null) {
convertView= inflater.inflate(R.layout.listview_element, null);
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
class Adapter2 extends BaseExpandableListAdapter {
private Object object;
private LayoutInflater inflater;
private Activity activity;
public Adapter2(Activity activity, Object object) {
this.activity= activity;
this.object= object;
this.inflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return object.getObjects().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) {
Object object= (Object) getChild(0, childPosition);
if (convertView==null) {
convertView= inflater.inflate(R.layout.listview_element, null);
Resources r = activity.getResources();
float px40 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px40,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return object.getObjects().size();
}
@Override
public Object getGroup(int groupPosition) {
return object;
}
@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) {
convertView= inflater.inflate(R.layout.listview_element, null);
Resources r = activity.getResources();
float px20 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px20,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
对象.java:
package com.example.threeleveltest;
import java.util.List;
public class Object {
String name;
List<Object> objects;
public Object(String name, List<Object> objects) {
this.name= name;
this.objects= objects;
}
public String getName() {
return this.name;
}
public List<Object> getObjects() {
return this.objects;
}
}
活动主.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
listview_element.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" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="40dp"
android:paddingLeft="40dp" />
</RelativeLayout>