23

我有一个ExpandableListView内部NestedScrollView(是的,我知道,在另一个滚动视图中拥有一个滚动视图并不好,但我不知道还能做什么,如果有人知道更好的方法,请告诉我)。

里面的内容大小NestedScrollView还在屏幕内所以不会滚动,但是ExpandableListView展开时内容会漏到屏幕外但NestedScrollView还是不会滚动..为什么会这样?

这是我的NestedScrollView布局:

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

5 回答 5

79

您可以使用您可以实现任何或或通过覆盖以下方法NonScrollExpandableListView的非滚动属性。LisviewGridViewExpandableListView

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

因此,为了使用NonScrollExpandableListView,您需要创建一个自定义类。

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

并像使用它一样。

<com.example.extraclasses.NonScrollExpandableListView 

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

快乐编码。

于 2016-06-03T04:23:41.157 回答
7

添加android:nestedScrollingEnabled="true"到您的 ExpandalbleListView 布局。

于 2017-08-22T22:14:36.660 回答
5

V-rund Puro-hit的答案对我有用。但是需要一些修改才能使用支持 API >19 的 Kotlin。因此,为了节省时间,这里是:

创建一个新的类文件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
    }
}

...我像这样使用它:

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

结果,我可以轻松地将按钮和其他元素添加到NavigationView侧抽屉),并且所有这些都可以在一个 common 中很好地工作ScrollView。这里的主要用途是当您需要将多个ListViews 和s 组合在一个可以处理滚动ExpandableListView的 common中时。ScrollView

于 2019-07-06T07:43:11.943 回答
3

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;
        }
    });
于 2017-10-13T09:23:53.547 回答
0

我们不能在滚动视图中使用列表视图、网格视图或可扩展列表视图。如果您不想在滚动视图中使用可扩展列表视图,那么您必须为可扩展列表视图提供一些固定高度。

于 2016-06-03T04:06:07.877 回答