neo
neo

Reputation: 1404

Why ScrollView automatically scrolled to bottom of screen?

I want to use PreferenceFragmentCompat inside another fragment(ProfileFragment). Here is my fragment_profile layout:

 <?xml version="1.0" encoding="utf-8"?>
<layout 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">




<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/ToolbarStyle"
            android:theme="@style/ToolbarStyle"
            app:popupTheme="@style/ToolbarStyle"
            app:title="@string/profile">


        </android.support.v7.widget.Toolbar>

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/cash"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/ktpay_cash_top_margin"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/balance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cash"
                tools:text="23423" />

            <TextView
                android:id="@+id/tgSymbol"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/bottom_margin_tenge_symbol"
                android:layout_marginLeft="@dimen/margin_left_tenge_symbol"
                android:gravity="bottom"
                android:text="@string/tenge_symbol"

                app:layout_constraintBottom_toBottomOf="@+id/balance"
                app:layout_constraintLeft_toRightOf="@+id/balance" />

            <LinearLayout
                android:id="@+id/wallet_recharge_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:clickable="true"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:orientation="horizontal"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ktpay_balance">


                <ImageView
                    android:id="@+id/wallet_icon"
                    android:layout_width="@dimen/wallet_icon_size"
                    android:layout_height="@dimen/wallet_icon_size"
                    android:src="@drawable/add_money" />


                <TextView
                    android:id="@+id/recharge_wallet"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="@dimen/margin_left_from_wallet_icon"
                    android:text="@string/recharge"
                    android:textColor="@color/recharge_color" />


            </LinearLayout>


            <android.support.constraint.ConstraintLayout
                android:id="@+id/bonus_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:background="@drawable/bonus_layout"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/wallet_recharge_layout">


                <TextView
                    android:id="@+id/bonusText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:text="@string/bonuses_kt"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toTopOf="@+id/amount_bonus"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/amount_bonus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/bonus_amount_text_size"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/bonusText"
                    tools:text="123213 тг" />

            </android.support.constraint.ConstraintLayout>


            <FrameLayout
                android:id="@+id/appSettings"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/bonus_layout" />


        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</LinearLayout>

I add to my FrameLayout with id app_settings my SettingsFragment that extends from PreferenceFragmentCompat. Everything works great, except when fragment_profile is loaded, its content already scrolled to the bottom of the screen. I don't want it to happen. When I remove ScrollView from fragment_profile, it works as expected. But I need ScrollView. The one of the possible causes I think, that PreferenceFragmentCompat uses RecyclerView. It somehow affects to my UI, because now fragment_profile contains ScrollView that contains RecyclerView. I thought that I need to call setNestedScrollingEnabled in RecyclerView inside PreferenceFragmentCompat, but don't know how to get reference to that RecyclerView. So, the base problem is why ScrollView inside fragment_profile is scrolled to the bottom when it is shown? What can be possible causes to this problem?

Upvotes: 1

Views: 1247

Answers (2)

Yash Pandya
Yash Pandya

Reputation: 1

It can manage by handling view programmatically by taking view on that activity

ScrollView scroll_view=(Scrollview) findviewbyid(R.id.scrollview);
scroll_view.fullScroll(ScrollView.FOCUS_UP);

Upvotes: 0

Saikrishna Rajaraman
Saikrishna Rajaraman

Reputation: 3273

Scrollview gains focus on Fragment open. So in order to make sure it doesn't scroll on fragment open add following attributes to the parent LinearLayout.

android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants" 

Upvotes: 3

Related Questions