Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded

℡╲_俬逩灬. 提交于 2019-12-28 02:55:07

问题


I have an ExpandableListView inside a NestedScrollView (yes I know, it is not good to have a scrolling view inside another scrolling view but I don't know what else to do, please do tell me if anybody knows a better approach).

The size of the content in NestedScrollView is still within the screen so it won't scroll, but when ExpandableListView is expanded, the content will leak outside the screen but the NestedScrollView still won't scroll.. Why is this so?

Here's my NestedScrollView layout :

<NestedScrollView>
    <LinearLayout>
        <LinearLayout></LinearLayout>
        ... // About 3 of the LinearLayouts
        <ExpandableListView/>
    </LinearLayout>
</NestedScrollView>

回答1:


You can use NonScrollExpandableListView you can achieve non-scroll property of any Lisview or GridView or ExpandableListView by overriding following method.

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

So for using NonScrollExpandableListView you need to make one custom class.

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

And use it like.

<com.example.extraclasses.NonScrollExpandableListView 

    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> 

Happy coding.




回答2:


Use this method this will calculate the ExpendableListSize at run time.

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

            }
        }
    }

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

}

An call this method in your setOnGroupClickListener.like below

mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            setListViewHeight(parent, groupPosition);
            return false;
        }
    });



回答3:


The answer from V-rund Puro-hit is what worked for me. But it took some modifications to work with Kotlin supporting API >19. So for the purpose of saving someone time, here it is:

Create a new class file NonScrollExpandableListView:

import android.content.Context
import android.util.AttributeSet
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView


class NonScrollExpandableListView : ExpandableListView {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun setAdapter(adapter: ExpandableListAdapter?) {
        super.setAdapter(adapter)
    }

    override fun setOnChildClickListener(onChildClickListener: OnChildClickListener) {
        super.setOnChildClickListener(onChildClickListener)
    }

    override fun expandGroup(groupPos: Int) : Boolean {
        return super.expandGroup(groupPos)
    }

    override fun expandGroup(groupPos: Int, animate: Boolean) : Boolean {
        return super.expandGroup(groupPos, animate)
    }

    override fun isGroupExpanded(groupPosition: Int): Boolean {
        return super.isGroupExpanded(groupPosition)
    }

    public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom)
        val params = layoutParams
        params.height = measuredHeight
    }
}

...and I use it like so:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <com.example.you.kotlinlistview.NonScrollExpandableListView
                android:id="@+id/expandableCategories"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <Button
                android:id="@+id/add_new_feed"
                android:drawableStart="@drawable/ic_add"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Add new feed"
                />

            <Button
                android:id="@+id/settings_button"
                android:drawableStart="@drawable/ic_settings"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Settings"
                />

            <Button
                android:id="@+id/logout_button"
                android:drawableStart="@drawable/ic_exit"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Log Out"
                />

        </LinearLayout>
    </ScrollView>

As a result, I can comfortably add buttons and other elements to NavigationView side drawer) and it all works nicely in one common ScrollView. Main usage here is when you need to combine multiple ListViews and ExpandableListViews inside the one common ScrollView which would take care of scrolling.




回答4:


Add android:nestedScrollingEnabled="true" to your ExpandalbleListView layout.




回答5:


We can't use listview, gridview or expandable listview inside scrollview. If you wan't to use expandable listview inside scrollview then you have to give some fixed height to your expandable listview.



来源:https://stackoverflow.com/questions/37605545/android-nestedscrollview-which-contains-expandablelistview-doesnt-scroll-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!