Prithniraj Nicyone
Prithniraj Nicyone

Reputation: 5121

ListView is not scrolling inside NestedScrollView in android

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.

Upvotes: 9

Views: 11052

Answers (8)

Mounish Kumar
Mounish Kumar

Reputation: 1

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

Upvotes: 0

Ashwin Balani
Ashwin Balani

Reputation: 781

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

android:nestedScrollingEnabled="true"

It will solve the issue

Upvotes: 1

Abhign01
Abhign01

Reputation: 321

yourtListViewName.setNestedScrollingEnabled(true);

Upvotes: 6

Rahman Rezaee
Rahman Rezaee

Reputation: 2165

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

Upvotes: 0

Ashwin
Ashwin

Reputation: 118

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

Upvotes: 3

Srishti Roy
Srishti Roy

Reputation: 576

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

Upvotes: 0

Andrei Balan
Andrei Balan

Reputation: 320

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.

Upvotes: 1

user6934307
user6934307

Reputation:

Try using

setNestedScrollingEnabled(true);

Upvotes: 7

Related Questions