Android:实现可扩展的列表视图以及具有图像和细节的线性布局

Android: Implementing expandable Listview along with Linearlayout Having Images and details

我已经使用以下链接实现了可扩展的列表视图。 http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial

使用可展开的 Listview 时,展开和折叠列表时,列表隐藏在 Textview 下。

这是我的布局

fragment_main.xml

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">



<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/profile_detail_image"
                android:scaleType="fitXY"/>

                <include layout="@layout/fragment_detail" />

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>
     </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

fragment_detail.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="5dp"
        app:contentPadding="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:stretchColumns="*">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

         <LinearLayout android:orientation="vertical">

                <TextView                                                      
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="24sp" />

              <TextView                                                       
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp" />

            <TextView                               
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textSize="18sp" />
        </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Description"
        android:layout_below="@+id/cardview"
        style="@style/Base.TextAppearance.AppCompat.Title" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:textSize="18sp"
        android:padding="10dp"
        android:fontFamily="sans-serif-light"
        android:layout_below="@+id/description_text"/>

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutDirection="rtl"/>

    <Button
        style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_alignParentBottom="true"
        android:text="Order" />
</RelativeLayout>

任何正在寻找答案的人。我找到了解决方案。

先创建一个class

public class Utility{
    public static void getExpandableListViewSize(ExpandableListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size,null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

直接调用activity中的方法点赞

expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    Utility.getExpandableListViewSize(expListView);
                }
            });

降低高度

expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Utility.getExpandableListViewSize(expListView);
            }
        });