gpt4 book ai didi

android - 修复了 ViewPager 内容布局中的 View ,在 CoordinatorLayout 下

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:00 24 4
gpt4 key购买 nike

我的应用程序有一个带有 TabLayoutActivity 和 3 个 Fragments。在 Activity 的 布局中,我有 CoordinatorLayoutViewPager。我还需要为 toolbar 制作动画。现在在 Fragments 布局中,我需要在底部放置一个固定的 TextView。下面给出的是 Activity 和 fragment 的 XML

I am facing the problem that this fixed TextView in Fragment's layout is going under the bottom navigation bar and is scrolling also, that I don't want.

如何实现这一点?

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>

最佳答案

由于您希望每个 fragment 的底部固定 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;
}
}

这里没有魔法,我们只是让我们的底部 View 依赖于 appbar_scrolling_view_behavior 的某个 View (在我们的例子中是 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 准确更改为您的包名称。

瞧!如果工具栏有适当的动画和固定的底部 View ,这应该会很有魅力。

此外:如果您不知道如何将 OnClick 事件传递给您的 fragment ,您可以在您的 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);
}
});

不要忘记让您的 fragment 实现 OnConfirmOrderClickListener。希望对您有所帮助!

enter image description here

关于android - 修复了 ViewPager 内容布局中的 View ,在 CoordinatorLayout 下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com