ListView is not scrolling inside NestedScrollView in android
Asked Answered
T

8

8

I've inflated a fragment from view pager which uses the listview. And list view does not support setNestedScrollingEnabled in pre lollipop devices. So I've added the listview inside a NestedScrollView but when scrolling the list it does not scrolling.

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

Can anyone suggest me any solution. Thanks in advance.

Thou answered 12/10, 2016 at 10:32 Comment(3)
Try using RecyclerView instead of ListViewMoist
This is the last solution I know. But there are a large no. of list view in my project. So changing all of them is time consuming. So it will be great if anyone can help me with the list view.Thou
You don't need scrollView over listView, because listview has own scrollingChairborne
A
6

Try using

setNestedScrollingEnabled(true);
Asset answered 12/10, 2016 at 10:34 Comment(2)
setNestedScrollingEnabled(true); is only working with the api level 21 or higher. I'm talking about the api level below 21.Thou
In XML also <android.support.v4.widget.NestedScrollView android:fillViewport="true" /> and <View android:nestedScrollingEnabled = "true" />Sequacious
C
5
yourtListViewName.setNestedScrollingEnabled(true);
Chesser answered 7/8, 2019 at 14:27 Comment(0)
B
3

Use ViewCompat.setNestedScrollingEnabled() and your problem will be solved.

Brnaby answered 13/12, 2017 at 0:7 Comment(0)
S
1

I am not sure why you would want to use a ListView inside a ScrollView if it will take up the entire height/width of it's parent.

I would suggest just using the ListView in the root layout of your fragment and eliminate the 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>

Also, I would suggest using RecyclerView instead of ListView as it implements NestedScrollingChild.

But if you are set on having nested scroll views in your layout you need to understand that NestedScrollView is intended to work as parent or a child scroll view while list. Here is a good example of a scrollview containing 2 other scrollviews:

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

Also, I noticed that you are maybe using CoordinatorLayout because you are setting the layout behavior of your child views. If so, you only need to specify the 'app:layout_behavior="@string/appbar_scrolling_view_behavior"' for your main and only ScrollView: A ListView, RecyclerView or NestedScrollView.

Spradling answered 12/10, 2016 at 10:54 Comment(0)
J
0

You have to intercept the touch event for smooth scrolling

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;
            }
        });
Janinejanis answered 12/10, 2016 at 11:10 Comment(0)
M
0

its work if add this code 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"
    />
Marijuana answered 9/7, 2019 at 9:9 Comment(0)
P
0

Inside the XML file, while defining the ListView, kindly write,

android:nestedScrollingEnabled="true"

It will solve the issue

Pretty answered 20/6, 2020 at 15:50 Comment(0)
M
0

Create a Custom 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>

In Java File

Create a object of your customListview instead of ListView like : NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_nonscroll_list);

Mattson answered 23/4, 2021 at 5:46 Comment(2)
Can you please explain how your code behaves ?Aspiration
We creating custom nested scroll view and using in XML. try the code and tell me if works correctly or not!!Mattson

© 2022 - 2024 — McMap. All rights reserved.