2017-04-02 73 views
0

我已經使用以下鏈接實現了可展開列表視圖。 http://www.androidhive.info/2013/07/android-expandable-list-view-tutorialAndroid:實現可擴展列表視圖以及Linearlayout具有圖像和細節

當使用可擴展列表視圖時,在展開和摺疊列表時,列表將隱藏在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> 
+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); 
      } 
     });