如何将ExpandableListView扩展为垂直填充屏幕

时间:2013-11-17 05:30:48

标签: android

我理解ExpandableListView不会扩展以完全填满屏幕。我创建了自己的自定义ExpandableListView。如何完全填满屏幕?

以下是我的代码:

这是我的ExpandableListView:

public class GeoAreaAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<GeoArea> myGeoAreas; 

    public GeoAreaAdapter(Context context, List<GeoArea> myGeoAreas) {
        this._context = context;
        this.myGeoAreas = myGeoAreas;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.myGeoAreas.get(groupPosition).neighborhoods.get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

        Neighborhood curNBH = (Neighborhood) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
        txtListChild.setText(curNBH.name);


        CheckBox ckb_NBHSelected = (CheckBox) convertView
                .findViewById(R.id.ckb_NBHSelected);
        ckb_NBHSelected.setEnabled(false);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.myGeoAreas.get(groupPosition).neighborhoods.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.myGeoAreas.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.myGeoAreas.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GeoArea curGeoArea = (GeoArea)getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(curGeoArea.name);

           ImageView img_GeoAreaExpanded = (ImageView)convertView.findViewById(R.id.img_GeoAreaExpanded);
        if(isExpanded){
               img_GeoAreaExpanded.setVisibility(View.VISIBLE);
        }else{
               img_GeoAreaExpanded.setVisibility(View.GONE);
        }
        return convertView;
    }

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

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


}

这是小组布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" 
    android:background="#000000">

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgv_GeoAreaSelector"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imgv_GeoAreaSelector"
        android:layout_toLeftOf="@+id/imgv_GeoAreaSelector"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#f9f93d"
        android:textSize="17dp" />

    <ImageView
        android:id="@+id/imgv_GeoAreaSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@android:drawable/ic_menu_send"
        android:focusable="false"
        />

    <ImageView
        android:id="@+id/img_GeoAreaExpanded"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/arrow_down_float" 
        android:focusable="false"
        android:visibility="gone"/>

</RelativeLayout>

这是我的孩子布局:

<?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="wrap_content" 
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/ckb_NBHSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:focusable="false"
        />

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="#000000"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />


</LinearLayout>

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

为每个组视图设置填充。这迫使每个小组占用更多空间。在我的车里,我测量了屏幕,然后在java中动态设置了填充。