[英]ListView is not scrolling inside NestedScrollView in android
我從使用列表視圖的視圖尋呼機中膨脹了一個片段。 並且列表視圖不支持預棒棒糖設備中的 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>
任何人都可以建議我任何解決方案。 提前致謝。
嘗試使用
setNestedScrollingEnabled(true);
使用 ViewCompat.setNestedScrollingEnabled() ,您的問題將得到解決。
yourtListViewName.setNestedScrollingEnabled(true);
如果 ListView 會占據其父級的整個高度/寬度,我不確定為什么要在 ScrollView 中使用它。
我建議只在片段的根布局中使用 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。
您必須攔截觸摸事件才能平滑滾動
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;
}
});
它的工作,如果添加此代碼 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"
/>
在 XML 文件中,在定義 ListView 時,請寫下,
android:nestedScrollingEnabled="true"
它將解決問題
創建一個自定義的 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();
}
}
自定義列表視圖
<?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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.