3级可扩展列表视图第3级截断

时间:2013-05-26 04:26:30

标签: java android xml expandablelistview

代码转储道歉 - 如果您更喜欢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);
    }  
}

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

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

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" >

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

2 个答案:

答案 0 :(得分:1)

这不是一个答案,更多的是一种解决方法,真的,但我在这里发布以防万一有人在未来遇到同样的问题 - 我不够聪明,无法解决原来的问题,所以我最终只使用了标准的两级可扩展列表,当您点击第二级时,它会弹出一个对话框,其中包含第三级项目的列表视图。看起来不错 - 实际上在小屏幕上效果更好,您可以添加任意数量的项目。

答案 1 :(得分:0)

我来不及但我希望这可以帮助其他人解决同样的问题。我修复它的方法是将CustomExpandableView中makeMeasureSpec()的大小增加到一个非常大的数字。这真的取决于你想要在第3级显示多少项目。

.swift