在三级ExpandableListView中滚动后,展开的子项会折叠

时间:2014-12-09 12:44:50

标签: android expandablelistview

我在this示例的帮助下正在执行3级ExpandableListview程序。它正确显示项目。

但是当我展开子组(第2级组)并滚动时,它会自动更改为折叠状态。我怎样才能解决这个问题?

This is my code. activity_main.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" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:focusable="false"
       />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/checkBox1"
        android:paddingLeft="40dp" />

</RelativeLayout>


Object.java

    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;
    }
}

MainActivity.java

    public class MainActivity extends Activity {
    public static CustomExpandableListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int noObjectsLevel1= 5;
        int noObjectsLevel2= 4;
        int noObjectsLevel3= 7;

        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) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }  
}

Adapter.java

    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());
 // name.setBackgroundColor(R.color.blue);
//level
  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;
 }

 @SuppressLint("ResourceAsColor")
@Override
 public View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {

  Object object= (Object) getChild(0, 0);
  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());
  name.setBackgroundColor(R.color.green);
//level3
  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;
 }

 @SuppressLint("ResourceAsColor")
@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());
  //name.setBackgroundColor(R.color.yellow);
  //level 2
  return convertView;
 }

 @Override
 public boolean hasStableIds() {
  return true;
 }

 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }
}

0 个答案:

没有答案