可扩展ListView仅占用布局的一部分

时间:2014-05-19 12:51:29

标签: listview layout expandablelistview android

我已经按照网上的教程实现了三级可扩展列表。但问题是列表只占用了布局的一部分,并且可以在该部分中滚动。 我是初学者,不知道在哪里更改代码。请帮助

    public class MainActivity extends Activity {
public static CustomExpandableListView list;
private HttpRequestActivity request;
private String response;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list= new CustomExpandableListView(this);
    Interest adapter= new Interest(this, objectsLvl1);
    list.setAdapter(adapter);

    LinearLayout parent= (LinearLayout) findViewById(R.id.parent);
    parent.addView(list);    
    }
    }


    Adapter class for ExpandableListView

     class CustomExpandableListView extends ExpandableListView {    
public CustomExpandableListView(Context context) {
    super(context);     
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

     // Adjust height
     Log.d("ONMEASURE", "OnMeasure is called");
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(500, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}  
   }`


   Xml layout:

  <?xml version="1.0" encoding="UTF-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#90EE90"

   android:orientation="vertical" >

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="98dp" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/mytextview"
        android:textAppearance="?android:attr/textAppearanceMedium" />

  </RelativeLayout>

  <LinearLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="vertical" >
  </LinearLayout>

  </LinearLayout>`




    public class Interest extends BaseExpandableListAdapter { // For top level i..e

private List<Object> objects;
private Activity activity;
private LayoutInflater inflater;

public Interest(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);
    }
    Int adapter = new Int(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());


    // System.out.println("Inside Click 1" + object.getName());
    return convertView;
}

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

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

       class Int extends BaseExpandableListAdapter {
private Object object;
private LayoutInflater inflater;
private Activity activity;
public String message;

public Int(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.getDisplayM
                                                                           etrics()); 
  convertView.setPadding(
  convertView.getPaddingLeft() + (int) px40,
  convertView.getPaddingTop(),
  convertView.getPaddingRight(),
  convertView.getPaddingBottom());
   }

  TextView name= (TextView) convertView.findViewById(R.id.name);
  name.setText(object.getName());




  convertView.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {



Context context = view.getContext();

Intent redirect = new Intent(context,Tabs.class);
redirect.putExtra("message", message);
    context.startActivity(redirect);   
   }
   });

   return convertView;
   }

public void onclickmodified() {

}

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

 public boolean onChildClick(CustomExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    // Log.d("TAG","Success");
    return true;
 }

1 个答案:

答案 0 :(得分:-1)

使用Scrollview.VerticalAlignment =“Stretch”,以及Height =“Auto”,如果它在Grid中,则将行高定义为“*”。

<Grid x:Name="Content">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

自动将仅使用孩子所需的空间,*将使用剩余的可用空间。