在 CoordinatorLayout 下,ViewPager 内容布局的固定视图

4
我的应用有一个带有TabLayout和3个Fragments的Activity。在Activity的布局中,我有一个带有ViewPager的CoordinatorLayout。我还需要对工具栏进行动画处理。 现在,在Fragments的布局中,我需要在底部放置一个固定的TextView。 以下是activity和fragment的XML。
“我遇到的问题是,Fragment布局中的这个固定TextView会进入底部导航栏下面,并且也会滚动,这不是我想要的。 如何解决这个问题?
activity.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:id="@+id/clMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="#FFFFFF"
                android:textSize="22sp"
                android:textStyle="bold" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

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

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/rlParent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1"
        android:fitsSystemWindows="true"
        android:orientation="vertical">


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

    <TextView
        android:id="@+id/tvConfirmOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorDarkGreen"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/confirm_order"
        android:textColor="@color/colorWhite"
        android:textSize="18sp"
        android:textStyle="bold"></TextView>
</LinearLayout> 
2个回答

19

如果您希望在每个片段中固定此底部的TextView,可以通过将您的TextView转移到 activity.xml 中并为其添加自定义行为来解决。

因此,首先,创建一个代表自定义行为的类:

public class FixedBottomViewBehavior extends CoordinatorLayout.Behavior<View> {

    public FixedBottomViewBehavior() {
    }

    public FixedBottomViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        //making our bottom view depends on ViewPager (or whatever that have appbar_scrolling_view_behavior behavior)
        return dependency instanceof ViewPager;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if (ViewCompat.isLaidOut(parent)) {
            //attach our bottom view to the bottom of CoordinatorLayout
            child.setY(parent.getBottom() - child.getHeight());

            //set bottom padding to the dependency view to prevent bottom view from covering it
            dependency.setPadding(dependency.getPaddingLeft(), dependency.getPaddingTop(),
                    dependency.getPaddingRight(), child.getHeight());
        }
        return false;
    }
} 

这里没有什么魔法,我们只是使底部视图依赖于一些带有appbar_scrolling_view_behavior的视图(在我们的情况下是ViewPager),然后将其附加到CoordinatorLayout的底部并设置一些依赖项的填充。

第二,通过在activity.xml中添加TextView来进行更改:

<?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"
    android:id="@+id/clMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="#FFFFFF"
                android:textSize="22sp"
                android:textStyle="bold" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

    <TextView
        android:id="@+id/tvConfirmOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorDarkGreen"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/confirm_order"
        android:textColor="@color/colorWhite"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_behavior="your.package.name.FixedBottomViewBehavior" />

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

确保你已经从fragment.xml中移除了TextView。同时别忘了将your.package.name.FixedBottomViewBehavior改成你的包名。

大功告成! 这样做应该可以很好地实现工具栏动画并在底部固定视图。

此外:如果你不知道如何将OnClick事件传递给你的片段,你可以按照以下方法在你的Activity中进行:

    public interface OnConfirmOrderClickListener {
        void onConfirmOrderClick(View v);
    }

    public Fragment getActiveFragment() {
        String name = makeFragmentName(pager.getId(), pager.getCurrentItem());
        return getSupportFragmentManager().findFragmentByTag(name);
    }

    public String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    } 

    tvConfirmOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment current = getActiveFragment();
            if (current instanceof OnConfirmOrderClickListener)
                ((OnConfirmOrderClickListener) current).onConfirmOrderClick(v);
        }
    });

不要忘记让你的片段实现OnConfirmOrderClickListener接口。希望这有所帮助!

输入图片描述


如果固定视图只能在viewpager的一个片段中呢? - Vincent NOCK
@VincentNOCK 根据当前屏幕上的片段,使用 onPageChangeListener 来隐藏/显示视图,使用 ViewPager - romtsn
我使用onPageChangeListener使按钮滚动。我会添加一个答案来解释如何做到这一点。 - Vincent NOCK
这个解决方案是最优雅的。谢谢@rom4ek - Ishaan
@rom4ek 如果我使用 EditText 而不是 TextView tvConfirmOrder,上面的解决方案是否有效?(EditText 的高度会根据输入的文本而改变。) - Deena

1

对于那些希望按钮只出现在一个片段中的人,可以使用ViewPageronPageChangeListener来使按钮随着ViewPager滚动。

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        updateButtonPosition(position, positionOffsetPixels);
    }
});


//Move the button according to the tab you want it to be fixed. 
//Here is the 3rd tab of a 3 tab viewPager, for instance
private void updateButtonPosition(int position, int positionOffsetPixels) {
    int fixedRewardXPos;
    if (position == 0) {
        fixedRewardXPos = viewPager.getWidth() * 2 - positionOffsetPixels;
    } else if (position == 1) {
        fixedRewardXPos = viewPager.getWidth() - positionOffsetPixels;
    } else {
        fixedRewardXPos = -positionOffsetPixels;
    }

    //Add getLeft() to keep the button to its former position in it's fragment
    aButton.setX(fixedRewardXPos + aButton.getLeft());
}

你能否在不同位置值上执行的操作上添加一些解释? - Shubham AgaRwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接