【问题标题】:ListView is not scrolling inside NestedScrollView in androidListView 没有在 android 的 NestedScrollView 内滚动
【发布时间】:2021-04-23 13:46:42
【问题描述】:

我已经从使用列表视图的视图寻呼机中膨胀了一个片段。并且列表视图不支持预棒棒糖设备中的 setNestedScrollingEnabled。所以我在 NestedScrollView 中添加了列表视图,但是在滚动列表时它不会滚动。

 <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/fragment_item_view"
    android:background="@color/white"
    android:isScrollContainer="true">

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_centerInParent="true" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">
        <ListView
            android:id="@+id/list_item_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:clipToPadding="false"
            android:divider="@color/gray_stroke_color"
            android:dividerHeight="0.5dp"
            android:paddingBottom="@dimen/padding_64dp"                >
        </ListView>

    </android.support.v4.widget.NestedScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

谁能建议我任何解决方案。提前致谢。

【问题讨论】:

  • 尝试使用 RecyclerView 代替 ListView
  • 这是我知道的最后一个解决方案。但是有一个很大的没有。我的项目中的列表视图。因此,更改所有这些非常耗时。因此,如果有人可以在列表视图方面帮助我,那就太好了。
  • listView 上不需要 scrollView,因为 listview 有自己的滚动功能

标签: android listview


【解决方案1】:

尝试使用

setNestedScrollingEnabled(true);

【讨论】:

  • setNestedScrollingEnabled(true);仅适用于 api 级别 21 或更高级别。我说的是21以下的api级别。
  • 在 XML 中还有 &lt;android.support.v4.widget.NestedScrollView android:fillViewport="true" /&gt; 和 &lt;View android:nestedScrollingEnabled = "true" /&gt;
【解决方案2】:
yourtListViewName.setNestedScrollingEnabled(true);

【讨论】:

    【解决方案3】:

    使用 ViewCompat.setNestedScrollingEnabled() 你的问题就解决了。

    【讨论】:

      【解决方案4】:

      如果 ListView 会占据其父级的整个高度/宽度,我不确定为什么要在 ScrollView 中使用 ListView。

      我建议只在片段的根布局中使用 ListView 并消除 ScrollView。

      <android.support.v4.widget.SwipeRefreshLayout
      android:id="@+id/swipe_refresh_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
          <ListView
              android:id="@+id/list_item_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scrollbars="vertical"
              android:clipToPadding="false"
              android:divider="@color/gray_stroke_color"
              android:dividerHeight="0.5dp"
              android:paddingBottom="@dimen/padding_64dp"/>
      
          </ListView>
      
      </android.support.v4.widget.SwipeRefreshLayout>
      

      另外,我建议使用 RecyclerView 而不是 ListView,因为它实现了 NestedScrollingChild。

      但是,如果您在布局中设置了嵌套滚动视图,您需要了解 NestedScrollView 旨在作为父滚动视图或子滚动视图同时工作。这是一个包含其他 2 个滚动视图的滚动视图的一个很好的示例:

      <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
      <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
      
          <android.support.v4.widget.NestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="100dp">
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"/>
      
          </android.support.v4.widget.NestedScrollView>
      
          <android.support.v4.widget.NestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="100dp">
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"/>
      
          </android.support.v4.widget.NestedScrollView>
      
      </LinearLayout>
      </ScrollView>
      

      另外,我注意到您可能正在使用 CoordinatorLayout,因为您正在设置子视图的布局行为。如果是这样,您只需为您的主要且唯一的 ScrollView 指定 'app:layout_behavior="@string/appbar_scrolling_view_behavior"':ListView、RecyclerView 或 NestedScrollView。

      【讨论】:

        【解决方案5】:

        你必须拦截触摸事件才能平滑滚动

        outerScrollView.setOnTouchListener(new View.OnTouchListener() {
        
                    public boolean onTouch(View v, MotionEvent event) {
                        findViewById(R.id.inner_scroll).getParent()
                                .requestDisallowInterceptTouchEvent(false);
                        return false;
                    }
                });
        
                innerScrollView.setOnTouchListener(new View.OnTouchListener() {
        
                    public boolean onTouch(View v, MotionEvent event) {
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        return false;
                    }
                });
        

        【讨论】:

          【解决方案6】:

          如果添加此代码,它的工作 android:nestedScrollingEnabled="true"

           <ExpandableListView
              android:id="@+id/lvExp"
              android:nestedScrollingEnabled="true"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_gravity="right"
              />
          

          【讨论】:

            【解决方案7】:

            XML文件里面,定义ListView的时候,请写,

            android:nestedScrollingEnabled="true"
            

            解决问题

            【讨论】:

              【解决方案8】:

              创建一个自定义的 NonScrollListView

              NonScrollListView.java

              public class NonScrollListView extends ListView {
              
              public NonScrollListView(Context context) {
                  super(context);
              }
              public NonScrollListView(Context context, AttributeSet attrs) {
                  super(context, attrs);
              }
              public NonScrollListView(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();    
              }
              

              }

              customListView

              <?xml version="1.0" encoding="utf-8"?>
              
              <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
              android:fadingEdgeLength="0dp"
              android:fillViewport="true"
              android:overScrollMode="never"
              android:scrollbars="none" >
              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" >
              
                  <!-- com.Example Changed with your Package name -->
              
                  <com.Example.NonScrollListView
                      android:id="@+id/lv_nonscroll_list"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content" >
                  </com.Example.NonScrollListView>
              
                  <RelativeLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/lv_nonscroll_list" >
                  </RelativeLayout>
              </RelativeLayout></ScrollView>
              

              在 Java 文件中

              创建一个您的 customListview 对象,而不是 ListView,例如: NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_nonscroll_list);

              【讨论】:

              • 您能解释一下您的代码的行为方式吗?
              • 我们创建自定义嵌套滚动视图并在 XML 中使用。试试代码,告诉我是否工作正常!!
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-05-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-07-21
              相关资源
              最近更新 更多