Android ExpandableListView动态高度

时间:2011-05-25 16:18:45

标签: android expandablelistview

好的,这是交易。在下面的代码中,我有一个expandablelistview,下面有两个按钮。我需要能够更改屏幕的大小,并使expandablelistview根据屏幕大小更改其高度。如果我将listview的高度设置为包装内容,则它太高而您无法再看到按钮。如果我手动设置高度单位,较小的屏幕也会使按钮不可见。我该怎么办?代码在这里:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal">
  <TextView android:layout_alignParentTop="true"
  android:layout_height="wrap_content" android:id="@+id/textView1"
  android:text="Edit Time" android:layout_marginTop="10px"
  android:layout_width="match_parent"
  android:gravity="center_horizontal"></TextView>
  <TextView android:layout_height="wrap_content"
  android:text="07/04/1776 - 07/04/1776"
  android:layout_below="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_alignLeft="@+id/textView1"
  android:layout_alignRight="@+id/textView1"
  android:gravity="center_horizontal"
  android:layout_marginTop="7px" android:id="@+id/lblWeek">
  </TextView>
  <ExpandableListView android:layout_width="match_parent"
  android:layout_marginTop="7px"
  android:layout_below="@+id/lblWeek"
  android:layout_alignLeft="@+id/lblWeek"
  android:layout_alignRight="@+id/lblWeek"
  android:id="@+id/lstTimeRecords" android:layout_height="270px">
  </ExpandableListView>
  <Button android:layout_height="wrap_content"
  android:id="@+id/button2" android:layout_width="wrap_content"
  android:text="Button" android:layout_below="@+id/lstTimeRecords"
  android:layout_alignLeft="@+id/lstTimeRecords"
  android:layout_alignRight="@+id/lstTimeRecords"></Button>
  <Button android:layout_height="wrap_content"
  android:layout_below="@+id/button2" android:id="@+id/button1"
  android:layout_width="wrap_content" android:text="Button"
  android:layout_alignLeft="@+id/button2"
  android:layout_alignRight="@+id/button2"></Button>
  <Button android:layout_below="@+id/textView1"
  android:layout_width="wrap_content" android:text="&gt;&gt;"
  android:layout_alignRight="@+id/textView1"
  android:layout_height="35px" android:id="@+id/btnNext"></Button>
  <Button android:layout_below="@+id/textView1"
  android:layout_width="wrap_content" android:text="&lt;&lt;"
  android:layout_alignLeft="@+id/textView1"
  android:layout_height="35px" android:id="@+id/btnPrevious">
  </Button>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我将为可扩展的Listview实现CustomAdapter。然后,您可以根据Listview及其子组件中的文本设置自定义大小。实际上Android会自动处理。

请查看下面的代码,了解可扩展列表视图:

public class LineupFragment extends Fragment {

    View rootView;
    ExpandableListView lv;
    private String[] groups;
    private String[][] children;


    public LineupFragment() {

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groups = new String[] { "Test Header One", "Test Header Two", "Test Header Three", "Test Header Four" };

        children = new String [][] {
                { "s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
                { "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32." },
                { "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." },
                { "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc." }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_lineup, container, false);  

    return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv = (ExpandableListView) view.findViewById(R.id.expListView);
        lv.setAdapter(new ExpandableListAdapter(groups, children));
        lv.setGroupIndicator(null);
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final LayoutInflater inf;
        private String[] groups;
        private String[][] children;

        public ExpandableListAdapter(String[] groups, String[][] children) {
            this.groups = groups;
            this.children = children;

            inf = LayoutInflater.from(getActivity());
        }

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

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

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

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

        @Override
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
             ViewHolder holder;
                if (convertView == null) {
                    convertView = inf.inflate(R.layout.list_item, parent, false);
                    holder = new ViewHolder();

                    holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.text.setText(getChild(groupPosition, childPosition).toString());

                return convertView;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_group, parent, false);

                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getGroup(groupPosition).toString());

            return convertView;
        }

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

        private class ViewHolder {
            TextView text;
        }
    }
}

父组件和子组件的xml布局类似于以下内容:

<强> list_group.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
        android:background="#000000">

        <ImageView
            android:id="@+id/photo"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true" />

        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="6dip"
            android:paddingTop="6dip"
            android:textColor="@color/white"
            android:textSize="20dp"
            android:textStyle="bold"/>

</LinearLayout>

<强> list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
            android:background="#000000" >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="5dp"
        android:textColor="@color/white"
        android:textSize="13sp" />

</LinearLayout>

希望这会有所帮助:)