Android:实现可扩展的Listview以及Linearlayout拥有图像和细节

时间:2017-04-02 07:44:30

标签: android expandablelistview

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

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

enter image description here

这是我的布局

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>

1 个答案:

答案 0 :(得分:0)

任何人都在寻找答案。我找到了一个解决方案。

首先创建一个类

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

只需在活动中调用方法赞

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