如何在ScrollView中拥有锚定视图?

时间:2016-08-04 04:18:20

标签: android xml android-viewpager scrollview android-coordinatorlayout

大家。我正在做一个Android应用程序而且我试图复制一些facebook messenger行为。我的活动有一个coordinatorlayout,在tableyout和viewpager中。它运行正常,但在片段中我在nestedscrollerview中有一个recyclerview。特别是一些视图,一个textview然后是recyclelerview,当它到达reyclerview上方的文本时向下滚动时,这个文本保持在顶部并且它永远不会被嵌套的scrollview隐藏。就像facebook messenger应用程序一样,如下所示: facebook1 facebook2

我想过在nestedscrollview之外有一个隐藏的textview,当scrollview到达另一个textview时会变得可见,所以它看起来很稳定,但我想知道是否有更好的方法。谢谢。

这是代码的简化版本: 活动布局:

<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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        layout="@layout/tool_bar"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

片段布局:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="30dp"
    android:background="@color/background">

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

        <TextView ..../>

        <TextView ..../>

        <!-- I need this textview to be anchored when it's reached by the scrollview -->
        <TextView
            android:id="@+id/anchored_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="0.2dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

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

0 个答案:

没有答案