2016-07-31 84 views
2

我試圖在Coordinatorlayout中實現ExpandableListView
我可以填寫數據並顯示,但是當我保留 android:layout_height="wrap_content/match_parent/fill_parent"但當我做android:layout_height="500dp"時,ExpandableList不會擴展。協調器佈局中的ExpandableListView

我的子視圖的高度爲100dp,因此,對於列表中的5個項目,500dp可以正常工作。

下面是代碼的一部分。
layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
    tools:context="com..example.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/app_bar_height" 
     android:fitsSystemWindows="true" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 


     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 



    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:paddingBottom="24dp" 
      android:paddingTop="24dp"> 


      <View 
       android:layout_width="fill_parent" 
       android:layout_height="1dp" 
       android:layout_marginTop="10dp" 
       android:background="#cccccc"/> 

       <ExpandableListView 
        android:layout_weight="1" 
        android:id="@+id/lvExp" 
        android:layout_width="match_parent" 
        android:layout_height="500dp" 
        android:cacheColorHint="#00000000"/> 

     </LinearLayout> 

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


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" 
     app:layout_anchor="@id/app_bar" 
     app:layout_anchorGravity="bottom|end" /> 

</android.support.design.widget.CoordinatorLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity implements ViewInterface{ 

    private Intent myIntent; 
    private RequestQueue queue; 
    AdapterView listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<ITEM>> listDataChild; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // get the listview 

      expListView = (ExpandableListView) findViewById(R.id.lvExp); 
      expListView.setFocusable(false); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       expListView.setNestedScrollingEnabled(true); 
      } 

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
          .setAction("Action", null).show(); 
       } 
      }); 


      prepdata(); 

      // Listview Group click listener 
      expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

       @Override 
       public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
       // Toast.makeText(getApplicationContext(), 
       // "Group Clicked " + listDataHeader.get(groupPosition), 
       // Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 

     // Listview Group expanded listener 
     expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 

       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Expanded"+(height+6)*10, 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     // Listview Group collasped listener 
     expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 

       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Collapsed", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     // Listview on child click listener 
     expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       // TODO Auto-generated method stub 
       Toast.makeText(
         getApplicationContext(), 
         listDataHeader.get(groupPosition) 
           + " : " 
           + listDataChild.get(
           listDataHeader.get(groupPosition)).get(
           childPosition), Toast.LENGTH_SHORT) 
         .show(); 
       return false; 
      } 
     }); 
    } 

    @Override 
    public void prepdata(){ 

     //Start Data Prep 
     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<ITEM>>(); 

     // Adding child data 
     listDataHeader.add("Choose your options1"); 
     listDataHeader.add("Choose your options2"); 


     // Adding child data 
     List<ITEM> options = new ArrayList<ITEM>(); 

     ITEM item1 = new ITEM("Burger King1", "Rs 491", "YesOff","Haveit1"); 
     ITEM item2 = new ITEM("Burger King2", "Rs 492", "YesOff","Haveit2"); 
     ITEM item3 = new ITEM("Burger King3", "Rs 493", "YesOff","Haveit3"); 
     ITEM item4 = new ITEM("Burger King4", "Rs 494", "YesOff","Haveit4"); 
     ITEM item5 = new ITEM("Burger King5", "Rs 495", "YesOff","Haveit5"); 

     options.add(item1); 
     options.add(item2); 
     options.add(item3); 
     options.add(item4); 
     options.add(item5); 
     // Header, Child data 
     listDataChild.put(listDataHeader.get(0), options); 
     listDataChild.put(listDataHeader.get(1), options); 
     //End Data Prep 

     listAdapter = new AdapterView(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

    } 
} 
+0

您好!你看這篇文章嗎? [ExpandableListView在CoordinatorLayout](http://stackoverflow.com/questions/32867779/coordinatorlayout-appbarlayout-expandablelistview-being-rendered-off-screen?rq=1) –

+0

是的,我做了,但它沒有幫助。 – impossible

+0

改進格式 –

回答

0

ExpandableListView怪異與CoordinatorLayout。此外,當您將它們包裝到CoordinatorLayout中時,您找不到有關其ScrollBehaviour的許多文檔。

我有同樣的問題,但我通過將我的ExpandableListView放入一個SwipeRefreshLayout來解決它,告訴他們他們應該如何表現。 這是我的工作佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:orientation="horizontal"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_expandable_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    </android.support.design.widget.AppBarLayout> 


    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeToRefresh" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <ExpandableListView 
       android:id="@+id/expandable_list" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    </android.support.v4.widget.SwipeRefreshLayout> 
</android.support.design.widget.CoordinatorLayout> 

注意使用SwipeRefreshLayout將允許滾動到頂部的行動「visualy使人神清氣爽數據的作用」

要禁用它,在你的onCreate()方法,虛增您的XML和寫:

SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh); 
     swipeRefreshLayout.setEnabled(false); // disables swipe bottom scroll AND refresh dialog 

PS:如果你將要面對同樣的問題,我話,我不知道得到了,這是我的AppBar包裹了我最後一個列表組的一部分。我不得不添加這個塊來解決這個問題(一切都在評論中):

// Enables scroll under appbar, AND makes parent view consider the height of the AppBar to add 
     // to the bottom of the page so the ExpandableScrollView last group isn't wrapped. 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // >= API 21 version 
      listView.setNestedScrollingEnabled(true); 
     }else { // < API 21 version 
      CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mSwipeLayout.getLayoutParams(); 
      AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_expandable_list_view); 

      TypedValue tv = new TypedValue(); 
      getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true); 
      int heightOfAppBarCompat = getResources().getDimensionPixelSize(tv.resourceId); //gets the height of the AppBar 
      params.bottomMargin = heightOfAppBarCompat; 
      mSwipeLayout.setLayoutParams(params); //sets the AppBar height as layout_marginBottom of the ExpandableListView 
     } 

希望這會對你有所幫助。

0

我做了這樣的事情。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/root_coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways"> 


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


       <android.support.v7.widget.Toolbar 
        android:id="@+id/budget_app_bar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:background="@drawable/gradient" 
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
        app:layout_collapseMode="pin" /> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/gradient" 
        android:orientation="horizontal" 
        android:paddingEnd="@dimen/eight_dp" 
        android:paddingLeft="@dimen/sixteen_dp" 
        android:paddingRight="@dimen/eight_dp" 
        android:paddingStart="@dimen/sixteen_dp"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/budget" 
         android:textColor="@color/white" 
         android:textSize="@dimen/twenty_four_sp" /> 

        <TextView 
         android:id="@+id/total_amount_budget_activity" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="end|right" 
         android:text="$ 5000" 
         android:textColor="@color/white" 
         android:textSize="@dimen/twenty_four_sp" /> 
       </FrameLayout> 

       <TextView 
        android:id="@+id/category_count_TV_budget_activity" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/gradient" 
        android:paddingLeft="@dimen/sixteen_dp" 
        android:paddingStart="@dimen/sixteen_dp" 
        android:paddingTop="@dimen/eight_dp" 
        android:text="2 items in 3 Categories" 
        android:textColor="@color/white" 
        android:textSize="@dimen/sixteen_sp" /> 


       <include layout="@layout/add_categegories_layout" /> 
      </LinearLayout> 


     </android.support.design.widget.CollapsingToolbarLayout> 


    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     app:layout_anchor="@id/app_bar_layout" 
     app:layout_anchorGravity="bottom" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <com.tacktile.contassist.helper.classes.NonScrollExpandableListView 
      android:id="@+id/expand_list_budget_activity" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:divider="@null" 
      android:focusable="false" 
      android:groupIndicator="@null" 
     /> 
    </android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 

定製ExpandableListView NonExpandableListView爲CordinateLayout。 源:Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded https://stackoverflow.com/a/37605908/3863332

public class NonScrollExpandableListView extends ExpandableListView { 

    public NonScrollExpandableListView(Context context) { 
     super(context); 
    } 

    public NonScrollExpandableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public NonScrollExpandableListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } 
}