Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expandable listview inside scrollview

I have few contents follwed by Expandable listview.I am not able to scroll the entire layout. I have been searching for appropriate answer more than a week..Suggest some answers.

like image 329
Satty Avatar asked Mar 29 '14 10:03

Satty


2 Answers

This solution worked for me, when i used custom layout inside navigation view having scroll view with LinearLayout which has Expandable listview.

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header">
    <ScrollView
            android:fillViewport="true"
            android:layout_marginTop="130dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/homeLayout"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:background="@drawable/layout_click_effect"
                    android:layout_width="match_parent"
                    android:layout_height="45dp">

                    <ImageView
                        android:id="@+id/homeIv"
                        android:layout_width="45dp"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_home_black_24dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Home" />
                </LinearLayout>
                <View
                    android:background="@android:color/darker_gray"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"/>

                <ExpandableListView
                    android:id="@+id/topCatgExpLv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:groupIndicator="@null"
                    android:dividerHeight="1dp" />
        </ScrollView>


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

In your onCreate

mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
    MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
            mGroups);
    mListView.setAdapter(adapter);
    mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);
            return false;
        }
    });

private void setListViewHeight(ExpandableListView listView, int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

        totalHeight += groupItem.getMeasuredHeight();

        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                totalHeight += listItem.getMeasuredHeight();

            }
            //Add Divider Height
            totalHeight += listView.getDividerHeight() * (listAdapter.getChildrenCount(i) - 1);
        }
    }
    //Add Divider Height
    totalHeight += listView.getDividerHeight() * (listAdapter.getGroupCount() - 1);

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();
}
like image 105
Amit Tumkur Avatar answered Sep 19 '22 19:09

Amit Tumkur


For users who are looking for expandable listview into scrollview, use views instead of expandable list follow the below link & code-

enter image description here

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linear_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</ScrollView>

And In Java Class some thing like this-

for (int i = 0; i < pProductArrayList.size(); i++) {

            LayoutInflater inflater = null;
            inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mLinearView = inflater.inflate(R.layout.row_first, null);

            final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
            final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
            final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
            final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);

            if(isFirstViewClick==false){
            mLinearScrollSecond.setVisibility(View.GONE);
            mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
            }
            else{
                mLinearScrollSecond.setVisibility(View.VISIBLE);
                mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
            }

            mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(isFirstViewClick==false){
                        isFirstViewClick=true;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                        mLinearScrollSecond.setVisibility(View.VISIBLE);

                    }else{
                        isFirstViewClick=false;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                        mLinearScrollSecond.setVisibility(View.GONE);   
                    }
                    return false;
                } 
            });
like image 43
Manish Srivastava Avatar answered Sep 19 '22 19:09

Manish Srivastava