滑动RecyclerView [英] Sliding RecyclerView

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

问题描述

我在 LinearLayout 中有一个 RecyclerView .如何使LinearLayout向上滑动",直到碰到应用程序栏,然后 RecyclerView 然后应照常开始滚动项目.同样,当到达列表中的第一项时,向下滚动列表将开始向下滑动整个容器,直到容器返回其起始位置.

 < LinearLayoutxmlns:android ="http://schemas.android.com/apk/res/android"android:id ="@ + id/container"android:layout_width ="match_parent"android:layout_height ="match_parent">< android.support.v7.widget.RecyclerViewandroid:id ="@ + id/nearby_stops"android:layout_width ="match_parent"android:layout_height ="match_parent"android:clipToPadding ="false"android:paddingBottom ="@ dimen/half_padding"android:paddingTop ="@ dimen/half_padding"android:scrollbars ="none"/></LinearLayout> 

我查看了

(请注意,GIF上的帧动画效果不是很好)

要在地图上的滑动列表后面添加阴影,只需将 app:layout_collapseMode 设置为 parallax ,然后在 MapView <之前添加另一个视图/code>位于 CollapsingToolbarLayout 中,它可以是您的蒙版,它可以是一个简单的视图,然后您可以在向上滚动列表时调整其alpha值.

最好使用本地android视图供您使用,我注意到 AndroidSlidingPanelLayout 大约有43个问题.

I have a RecyclerView in a LinearLayout. How can I have LinearLayout "slide" upwards until it hits the App Bar, whereby the RecyclerView should then begin scrolling the items as usual. Likewise, scrolling the list down will begin to "slide" the entire container down when the the first item is reached in the list until the container returns to its starting position.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/nearby_stops"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/half_padding"
            android:paddingTop="@dimen/half_padding"
            android:scrollbars="none" />

</LinearLayout>

I looked at https://github.com/umano/AndroidSlidingUpPanel, however it does not support RecyclerView and the moment.

解决方案

Put the RecyclerView inside a Fragment, so make an XML like so for the Fragment:

This is your XML for the RecyclerView

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/nearby_stops"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/half_padding"
        android:paddingTop="@dimen/half_padding"
        android:scrollbars="none" />

</LinearLayout>

Then for the Activity hosting the Fragment just add a FrameLayout for the Fragment:

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

    <!-- Put other views here -->

    <FrameLayout
        android:id="@+id/slidingFragmentContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 </LinearLayout>

Then inside your Activity when you instantiate the Fragment do the following:

    SampleFragment listFragment = new SampleFragment();

    getSupportFragmentManager().beginTransaction()
            .setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom,
                    R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom)
            .addToBackStack(null)
            .add(R.id.slidingFragmentContent, listFragment)
            .commit();

The animations R.anim.abc_slide_in_bottom and R.anim.abc_slide_out_bottom are available in the Android API.

I also noticed you're not setting any orientation for your LinearLayouts. Set an orientation like so android:orientation="..."

You can assign a Button to show the Fragment, like so:

mButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        showFragment();
    }
});

--------------------------------------------------------------------

To get the RecyclerView to scroll up as you scroll it just use the following:

Create an XML layout like this for your Activity:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/map_view_height"
        android:orientation="vertical"
        android:fitsSystemWindows="true">>

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <YourCustomViewContainingTheMap
                app:layout_collapseMode="parallax"/>

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

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

Inflate your Fragment inside the FrameLayout and then as you scroll up it should do a parallax animation on the Map. If you don't want a Parallax effect just set app:layout_CollapseMode="pin"

Here is a sample app I created, you can see as I scroll up on the RecyclerView it slides up:

(Please note the Frame Animations on GIFs is not that great)

To get a shadow behind the sliding list on your map, just set app:layout_collapseMode to parallax and then add another view in front of your MapView inside the CollapsingToolbarLayout which can be your mask, it can be be a simple view, and then you can adjust it's alpha value as you scroll up the list.

It's better to use the native android views at your disposal, I noticed that AndroidSlidingPanelLayout has around 43 issues.

这篇关于滑动RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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