-1

This is my layout which is using the nav bar and scroll view.

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".AccountActivity">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp" />

        </android.support.design.widget.AppBarLayout>

        <!--content of account activity-->

        <ScrollView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            >

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_setup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".AccountActivity">

                <ImageView
                    android:layout_width="match_parent"
                    android:background="@drawable/listgrad"
                    android:id="@+id/imageView"
                    android:layout_height="100dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:text="Name"
                    android:layout_below="@+id/profilepic"
                    android:id="@+id/accountnamefield"
                    android:textSize="18sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Recently Match"
                    android:id="@+id/recentMatchTitle"
                    android:textSize="18sp"
                    android:layout_below="@+id/accountnamefield"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <android.support.v7.widget.RecyclerView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/recentMatchProductList"
                    android:layout_below="@+id/recentMatchTitle"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true">
                </android.support.v7.widget.RecyclerView>

                <ImageButton
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    app:srcCompat="@mipmap/ic_account_circle_white_36dp"
                    android:id="@+id/profilepic"
                    android:background="@drawable/round_button"
                    android:layout_marginTop="21dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

            </RelativeLayout>

        </ScrollView>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/drawer" />

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

I am using scroll view with the nav drawer. Why do I open and close the nav drawer then the view will scroll to bottom position? Can I move it to top or remain the same position? Please give me some helps.

ng2b30
  • 351
  • 6
  • 18

2 Answers2

1

Remove android:inputType="textPersonName" part first for TextView.Then add android:scrollbars="vertical" to your ScrollView .It worked fine for me.

Here I will post the code I used for testing, ScrollView kept same position while using NavigationDrawer

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp" />

        </android.support.design.widget.AppBarLayout>

        <!--content of account activity-->

        <ScrollView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:scrollbars="vertical"
            >

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_setup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".MainActivity">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/bla"
                    android:id="@+id/accountnamefield"
                    android:textSize="18sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Recently Match"
                    android:id="@+id/recentMatchTitle"
                    android:textSize="18sp"
                    android:layout_below="@+id/accountnamefield"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />



            </LinearLayout>

        </ScrollView>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

kasvith
  • 377
  • 5
  • 19
  • Thank you for your answer. However, I cannot solve my problem. I think scroll view is affected by the recycler view so it go to the bottom. – ng2b30 Nov 22 '16 at 16:53
0

Your layout has bunch of problems:

android:layout_alignParentStart="true" - the same thing as left (or right for RTL) android:layout_alignParentEnd="true" - the same thing as right (or left for RTL)

It's wrong to declare start and left in the same time

Using a recycler view inside ScrollView - bad practice and will never work. You can use nested scroll view (RecyclerView inside ScrollView is not working), but the better solution is put what you want inside scroll view (as headers and footers).

I think there is no way to measure your layout. Try to figure out how android is drawing views (https://medium.com/@britt.barak/measure-layout-draw-483c6a4d2fab#.y7nd8gyql).

P.S. You have much more problems with your layout, it's easier to write a new one, than pointing all problems.

Community
  • 1
  • 1
Alexander Mikhaylov
  • 1,790
  • 1
  • 13
  • 23
  • android:layout_alignParentStart="true" and android:layout_alignParentEnd="true" was generated automatically when I use xml to drag the view. I would have a look for that website and I am still not very good to handle so many views in one activity. Thank you. – ng2b30 Nov 22 '16 at 16:56