问题
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.
回答1:
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 LinearLayout
s. 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.
来源:https://stackoverflow.com/questions/32162249/sliding-recyclerview