如何在RecyclerView滚动时上下滚动底部栏 [英] How to scroll up/down of bottom bar on scrolling of RecyclerView

查看:72
本文介绍了如何在RecyclerView滚动时上下滚动底部栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google最近发布的设计支持库中,引入了一些很酷的新视图.使用某些新组件(例如CoordinatorLayout)可以(!)使您实现滚动行为.

With Google’s recent release of the Design Support Library several cool new views have been introduced. Using some of the new components (e.g. CoordinatorLayout ) might (!) enable you to achieve the scrolling behavior.

我尝试了一些内置的滚动行为,但没有任何效果对我有用,

I was tryed with some built in scrolling behavior but nothing is working for me,

我的布局中有一个bottombar(LinearLayout)代替了FloatingActionButton

I have a bottombar(LinearLayout) in my layout in place of FloatingActionButton

这就是我想要的.

  1. 该屏幕底部栏的OnLaunch应该出现.
  2. 在"recyclerview"滚动条上,底部滚动条应向下滚动.
  3. 在recyclerview底部滚动时,应向上滚动

有没有内置的机制来实现这一目标?还是我们需要编写Java代码?

Is there any builtin mechanism to achieve this ? or We need write java code?

这是我的代码:

main_activty.xml

main_activty.xml

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">

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

        <include layout="@layout/toolbar_srp" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/white"
            app:tabMode="fixed" />

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

    <!-- All Scrollable Views -->

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

    <!-- Bottom bar-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#a0000000"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/padding_small"
        android:paddingTop="@dimen/padding_small">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="AC"
            android:textColor="@color/white" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="Sleeper"
            android:textColor="@color/white" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="Premium"
            android:textColor="@color/white" />

    </LinearLayout>


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

fragment.xml(在这里放入了recyclerview代码)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/padding_small"
        android:paddingTop="@dimen/padding_small"
        android:weightSum="1">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:background="@drawable/bg_srp_sorter"
            android:clickable="true"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="Departure" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:background="@drawable/bg_srp_sorter"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="Duration" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:gravity="center"
            android:paddingBottom="@dimen/padding_small"
            android:paddingTop="@dimen/padding_small"
            android:text="Price" />

    </LinearLayout>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/bus_list_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
</LinearLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    android:minHeight="?attr/actionBarSize">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.10">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha" />


        </LinearLayout>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.78"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="1">

                <TextView
                    android:id="@+id/toolbar_title_source"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.47"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="Thiruvananthapuram "
                    android:textColor="@color/white"
                    android:textSize="@dimen/label_text_size_large" />

                <TextView
                    android:id="@+id/toolbar_title_arrow"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.08"
                    android:text="@string/char_right"
                    android:textColor="@color/white"
                    android:textSize="@dimen/label_text_size_large"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/toolbar_title_destination"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.47"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text=" Cochin"
                    android:textColor="@color/white"
                    android:textSize="@dimen/label_text_size_large" />


            </LinearLayout>


            <TextView
                android:id="@+id/toolbar_sub_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 Seat(s)"
                android:textColor="@color/lite_gray"
                android:textSize="@dimen/label_text_size_normal" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.13">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:scaleType="fitCenter"
                android:src="@drawable/filter" />
        </LinearLayout>
    </LinearLayout>

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

推荐答案

由于使用的是CoordinatorLayout,因此您可以创建一个自定义的Behaviour,该实现可以实现您所要求的以下示例: create一个extends CoordinatorLayout.Behavior<View>遵循以下示例的类:

since you are using CoordinatorLayout you could create a custom Behaviour that can achieve what you requested follow example below: create a class that extends CoordinatorLayout.Behavior<View> follow example below:

public class QuickReturnFloaterBehavior extends CoordinatorLayout.Behavior<View> {

    private int distance;

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

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        if (dy > 0 && distance < 0 || dy < 0 && distance > 0) {
            child.animate().cancel();
            distance = 0;
        }
        distance += dy;
        final int height = child.getHeight() > 0 ? (child.getHeight()) : 600/*update this accordingly*/;
        if (distance > height && child.isShown()) {
            hide(child);
        } else if (distance < 0 && !child.isShown()) {
            show(child);
        }
    }

    private void hide(View view) {
        view.setVisibility(View.GONE);// use animate.translateY(height); instead
    }

    private void show(View view) {
        view.setVisibility(View.VISIBLE);// use animate.translateY(-height); instead
    }

}

现在要应用此behaviour将此添加到您的布局

now to apply this behaviour add this to your layout

app:layout_behavior="com.example.QuickReturnFloaterBehavior"

这篇关于如何在RecyclerView滚动时上下滚动底部栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆