2

I am trying to implement ExpandableListView in Coordinatorlayout.
I am able to fill data and show, but ExpandableList is not getting expanded when I keep android:layout_height="wrap_content/match_parent/fill_parent" but when I do android:layout_height="500dp", it gets expanded.

My child view has height of 100dp so, for 5 items in the list, 500dp works fine.

Below is the part of code.
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);

    }
}
Juan Cruz Soler
  • 7,684
  • 5
  • 34
  • 42
impossible
  • 1,872
  • 5
  • 26
  • 54

2 Answers2

0

ExpandableListView are weird with CoordinatorLayout. Moreover, you don't find many docs about their ScrollBehaviour when you wrap them into a CoordinatorLayout.

I had the same issue, but I solved it by placing my ExpandableListView into a SwipeRefreshLayout, telling both of them how they should behave. Here is my working layout :

<?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>

Note that using a SwipeRefreshLayout will allow the action of scrolling to the top to "visualy make the action of refreshing data".

To disable this, in your onCreate() method, inflate your xml and write this :

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

PS : I don't know if you're going to face the same issue I then got, which was my AppBar wrapping a part of my last List group. I had to add this block to fix the issue (everything is in the comments) :

// 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
        }

Hope this will help you.

Maxime Flament
  • 563
  • 5
  • 19
0

I did Something like This.

<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>

Custom ExpandableListView NonExpandableListView for CordinateLayout. src: 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();
    }
}