Android - 包含ExpandableListView的NestedScrollView在展开时不会滚动

Kev*_*vie 16 android expandablelistview android-nestedscrollview

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

内容的大小NestedScrollView仍然在屏幕内,因此它不会滚动,但是当ExpandableListView展开时,内容将泄漏到屏幕外但NestedScrollView仍然不会滚动..为什么会这样?

这是我的NestedScrollView布局:

<NestedScrollView>
    <LinearLayout>
        <LinearLayout></LinearLayout>
        ... // About 3 of the LinearLayouts
        <ExpandableListView/>
    </LinearLayout>
</NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

V-r*_*hit 61

你可以使用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();
} 
Run Code Online (Sandbox Code Playgroud)

因此,对于使用,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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用它.

<com.example.extraclasses.NonScrollExpandableListView 

    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> 
Run Code Online (Sandbox Code Playgroud)

快乐的编码.


小智 7

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


dsa*_*laj 6

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
    }
}
Run Code Online (Sandbox Code Playgroud)

...我这样使用它:

    <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>
Run Code Online (Sandbox Code Playgroud)

因此,我可以轻松地将按钮和其他元素添加到NavigationView侧抽屉中),并且这一切都可以在一个 common 中很好地工作ScrollView。这里的主要用途是当您需要在一个公共内部组合多个ListViews 和s来处理滚动。ExpandableListViewScrollView