6

I've got a layout with ScrollView that has a ListView inside. Everything works fine, but if I open DrawerLayout, ScrollView will be scrolled a little. Just look at these screenshots.

enter image description here

Here I open DrawerLayout

enter image description here

Then I close DrawerLayout. enter image description here

As you can see it is scrolled a little. How to fix this? This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menuLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ProgressBar
            android:id="@+id/progressWheelBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/noDataTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/noData"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/actionBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/innerActionBarLayout"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@color/material_blue_500" >

                <ImageButton
                    android:id="@+id/menuButton"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:background="@null"
                    android:src="@drawable/ic_menu_white_24dp" />

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="16dp"
                    android:layout_toRightOf="@+id/menuButton"
                    android:textColor="@color/white"

                    android:textSize="18sp" />
            </RelativeLayout>
        </LinearLayout>

        <ScrollView
            android:id="@+id/mainScrollView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/actionBarLayout"
            android:fillViewport="true" 
            android:scrollbars="none"
            >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
                 <!-- Here goes the frame layout with a listview inside -->
                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone"
                    tools:context="ru.tenet.ttk.MainActivity" />
            </LinearLayout>
        </ScrollView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/actionBarLayout"
            android:background="@drawable/shadow_down" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@color/white"
        android:orientation="vertical"
        android:clickable="true" >

        <ListView
            android:id="@+id/menuListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/list_selector"
            android:layout_marginTop="15dp"
            android:divider="@null" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
Tony
  • 3,605
  • 14
  • 52
  • 84
  • 4
    Do you really need the `ScrollView`? A `ListView` scrolls on its own. – Mike M. Jun 02 '16 at 23:33
  • You can use `NestedScrollView` – Khairul Alam Licon Jun 06 '16 at 08:09
  • You can put ListView inside a scroll view but u should handle both the scroll, Bec list view has its own scroll feature and scroll view has its scroll feature to , when u try to scroll both scroll will gets conflicted so u need to handle parent and child scroll events. – Naveen Shriyan Jun 08 '16 at 07:53
  • This link may help: http://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view – Naveen Shriyan Jun 08 '16 at 07:53
  • How did you synchronise the scrolling of ListView and ScrollView? Which one will take precedence? I guess is that you override onIntercepterTouchEvent somewhere, is it true? – Tin Tran Jun 08 '16 at 16:27

4 Answers4

0

It is hard to give a definite answer because you haven't posted your code. One reason could be that you have a "windowActionBarOverlay=true" style setting in your activity. Due to that setting, the content of the activity starts right at the beginning of the page and falls behind the ActionBar.

Besides, as commenters above have stated, do you actually need a ListView inside of a ScrollView? ListView itself is a ScrollView.

Best if you post the code of your Drawer activity and the content activity.

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33
0

Simple solution is using headerView - ListView.addHeaderView(); I think you don't need to use ScrollView & just add container as headerView

BlueMist
  • 226
  • 1
  • 7
0

Your best bet would be to use the RecyclerView and define a headerRow to show your title.

MrJM
  • 1,214
  • 1
  • 12
  • 26
0

Assign a DrawerListener to DrawerLayout, and in onDrawerClosed event scroll the scrollView to top.

mDrawer.setDrawerListener(new DrawerListener() {
        @Override
        public void onDrawerSlide(View view, float v) {

        }

        @Override
        public void onDrawerOpened(View view) {

        }

        @Override
        public void onDrawerClosed(View view) {
            mScrollView.fullScroll(ScrollView.FOCUS_UP);
        }

        @Override
        public void onDrawerStateChanged(int i) {

        }
    });
ked
  • 2,426
  • 21
  • 24