gpt4 book ai didi

android - 如何在滚动 RecyclerView 时向上/向下滚动底部栏

转载 作者:可可西里 更新时间:2023-11-01 19:09:02 27 4
gpt4 key购买 nike

随着 Google 最近发布的设计支持库,引入了几个很酷的新 View 。使用一些新组件(例如 CoordinatorLayout )可能(!)使您能够实现滚动行为。

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

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

这是我想要的。

  1. 应显示此屏幕底部栏的 OnLaunch。
  2. 在 recyclerview 底部栏向上滚动时应该向下滚动。
  3. 在 recyclerview 底栏向下滚动时应该向上滚动

是否有任何内置机制可以实现此目的?或者我们需要编写 java 代码?

这是我的代码:

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>

最佳答案

我是这样做的:

mRecylerView.addOnScrollListener(new HideShowScrollListener() {
@Override
public void onHide() {
//fab.animate().setInterpolator(new AccelerateDecelerateInterpolator()).scaleX(0).scaleY(0);
// do your hiding animation here

}

@Override
public void onShow() {
// fab.animate().setInterpolator(new AccelerateDecelerateInterpolator()).scaleX(1).scaleY(1);
// do your showing animation here
}
});

您将需要 HideShowScrollListener.class

/**
* This class is a ScrollListener for RecyclerView that allows to show/hide
* views when list is scrolled.
* */
public abstract class HideShowScrollListener extends RecyclerView.OnScrollListener {
private static final int HIDE_THRESHOLD = 20;
private int scrolledDistance = 0;
private boolean controlsVisible = true;

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
onHide();
controlsVisible = false;
scrolledDistance = 0;
} else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
onShow();
controlsVisible = true;
scrolledDistance = 0;
}

if((controlsVisible && dy>0) || (!controlsVisible && dy<0)) {
scrolledDistance += dy;
}
}

public abstract void onHide();
public abstract void onShow();

}

关于android - 如何在滚动 RecyclerView 时向上/向下滚动底部栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715732/

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