Search code examples
androidlistviewscrollviewandroid-nestedscrollview

Not working properly with using ListView in NestedScrollView


In Android, Image view should disapper before scroll is started, but works reversely. When I'm holding my finger on the screen if I scroll up and down before Imageview is disapper, scroll view is starting to scroll down, then Imageview starts to close. Here is my code.

<android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/user_menu_bg"
            android:orientation="vertical">

            <ListView
                android:id="@+id/menu_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@drawable/menu_list_divider"
                android:listSelector="@android:color/transparent"
                android:nestedScrollingEnabled="true" />
        </LinearLayout>

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

Solution

  • It is better to use NonScrollableListView instead of listview. Or use RecyclerView.

    Try

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