【问题标题】:How to fix a Fragment's xml inner layout position to the bottom of screen in viewpager如何将片段的 xml 内部布局位置修复到 viewpager 中的屏幕底部
【发布时间】:2018-04-03 11:17:57
【问题描述】:

我在 Activity(activity_tabs.xml) 中有一个 Tablayout、viewpager。 Tablayout 包含 5 个选项卡和 5 个片段。第一个片段是fragment_feed.xml,它转到viewpager 并且该viewpager 是可滚动的。我有一个Linearlayout(带有FAB按钮),在那个fragment_feed.xml中占据了底部的一小部分。这里的问题是,在那个片段(第一个选项卡)中,Linearlayout 被放置在下面一点,当我向上滚动时它会到达原始位置,当向下滚动时,它会转到下面的位置。

这是我的片段 Xml(FeedFragment.xml)

        <FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/totalFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_grey"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.authent.authentication.FeedFragment">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.constraint.ConstraintLayout
        android:id="@+id/firstRelative"
        android:layout_width="match_parent"
        android:layout_height="88dp"
        android:layout_marginBottom="6dp"
        android:background="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/askCircleImageView"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:src="@drawable/default_photo"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/feed_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="24dp"
            android:text="Username"
            android:textSize="18dp"
            android:textStyle="bold"
            app:layout_constraintStart_toEndOf="@+id/askCircleImageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/askQuestion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:text="Ask your Question..."
            android:textSize="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/askCircleImageView" />

    </android.support.constraint.ConstraintLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/feed_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/firstRelative"
        android:layout_marginBottom="0dp"
        android:background="@color/white"
        android:nestedScrollingEnabled="false"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

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

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

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="196dp"
            android:layout_gravity="bottom"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="16dp"
            android:visibility="gone"
            app:cardCornerRadius="20dp">

            <LinearLayout
                android:id="@+id/linearReveal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <Button
                    android:id="@+id/shareBtn"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:text="@string/share_info"
                    android:textAllCaps="false"
                    android:textColor="#00ccff"
                    android:textSize="24sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray" />

                <Button
                    android:id="@+id/askBtn"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:text="Ask a Query"
                    android:textAllCaps="false"
                    android:textColor="#ff3300"
                    android:textSize="24dp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray" />

                <Button
                    android:id="@+id/challengeBtn"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:text="Post a Challenge"
                    android:textAllCaps="false"
                    android:textColor="#cc9900"
                    android:textSize="24dp" />

            </LinearLayout>

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/floatBtn"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_gravity="end"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/edit_nick_name"
            app:backgroundTint="@color/colorPrimary" />

    </LinearLayout>
  </FrameLayout>

这是我的活动 xml(activity_tabs.xml)

     <?xml version="1.0" encoding="utf-8"?>
<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="com.authent.authentication.TabsActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/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/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimary"
        app:titleTextAppearance="@style/ActionBar.nameText"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/customPopupStyle">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:layout_marginStart="18dp"
            android:layout_marginLeft="18dp"/>

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@drawable/tablayout_border"
        app:tabIndicatorColor="@color/red" />

  </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.design.widget.CoordinatorLayout>

布局截图。这是布局膨胀的时候。

这是我滚动的时候。回到原来的位置。

那个 FAB 按钮正好下降到工具栏的长度。当我向上滚动工具栏隐藏并且 FAB 按钮(整个线性布局)到达原始位置时。

如何解决此问题。我想要那个线性布局(包含 FAB 按钮)在一个固定的位置。

【问题讨论】:

    标签: android android-viewpager android-toolbar android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    在实例化 FragmentPagerAdapter 时使用 Fragment.getChildFragmentManager() 很重要。另请注意,您不能对子 Fragment 使用 Fragment.setRetainInstance() ,否则会出现异常。

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多