简体   繁体   English

Android 在滚动视图中滚动到顶部下一个替换片段

[英]Android scroll to top next replacing fragment in scrollview

this is my layout:这是我的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_scrollview"
    android:layout_width="match_parent"
    android:scrollbars="vertical"
    android:fillViewport="true"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space.small"
        android:orientation="vertical"
        android:padding="@dimen/space.medium">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="@dimen/space.medium">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bla_bla_bla"/>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/my_relative_layout">
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

it is a fragment inside my tab menu, when this tab will be create the relative layout will be replaced by a variable number of elements:它是我的选项卡菜单中的一个片段,当创建此选项卡时,相对布局将被可变数量的元素替换:

android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.my_relative_layout, myListFragment).commit();
mHolder.scrollView.scrollTo(0,0); //not work

But my fragment doesn't scroll to top, it works only with a delay like:但是我的片段不会滚动到顶部,它只能在延迟时起作用,例如:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
     @Override
     public void run() {
         mHolder.scrollView.scrollTo(0,0);
     }
}, 100);

How can I do that in a right way?我怎样才能以正确的方式做到这一点? I won't use a delay我不会使用延迟

Add添加

android:descendantFocusability="blocksDescendants"

to

"RelativeLayout"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM