sta*_*owN 5 android android-layout android-recyclerview
我正在尝试使用滚动背景创建一个RecyclerView,如下图所示。这个想法是,当我向上/向下滚动查看器时,背景(浅绿色)图像也应该同步向上/向下移动。关于如何实现这一目标的任何线索?

这是我的基本RecyclerView配置
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/item_margin"
android:clipToPadding="false"
android:background="@drawable/ic_light_green_image"/>
Run Code Online (Sandbox Code Playgroud)
我有相同的用例 - 沿Z轴滚动位于应用栏上方的卡列表,就像Google Play音乐一样。这其实很简单,但对于文档RecyclerView#computeVerticalScrollOffset()被完全误导。它不计算滚动条拇指的偏移量,而是计算其RecyclerView自身滚动了多少(这正是我们在这里所需要的)。
mPostList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int scrollY = mPostList.computeVerticalScrollOffset();
// mAppBarBg corresponds to your light green background view
mAppBarBg.setTranslationY(-scrollY);
// I also have a drop shadow on the Toolbar, this removes the
// shadow when the list is scrolled to the top
mToolbarCard.setCardElevation(scrollY <= 0 ? 0 : toolbarElevation);
}
});
Run Code Online (Sandbox Code Playgroud)
我的布局看起来像这样,如果有帮助的话:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- this corresponds to your light green background -->
<FrameLayout
android:id="@+id/app_bar_bg"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_container_height"
android:background="@color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- CardView adds a drop shadow to the Toolbar -->
<android.support.v7.widget.CardView
android:id="@+id/toolbar_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
我可能不会将 RecyclerView 的背景用于实际移动的物体。也许不要在 RecyclerView 上放置背景,而是在实际移动的 RecyclerView 后面放置一个不同的视图。然后,您可以重写 RecyclerView 上的 onDraw 或 onLayout 并将背景位置更新到您希望其相对于 RecyclerView 滚动百分比的位置。
像这样的东西......XML:
<RelativeLayout ...>
<SomeBackgroundView id="backgroundView" ...>
<MyRecyclerView ...>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
代码:
class MyRecyclerView extends RecyclerView {
protected int mLastScroll = Integer.MAX_VALUE;
protected ScrollChangedListener mScrollChangedListener;
// ...
@Override
void onDraw(Canvas c) {
int scrollY = getScrollY();
if (scrollY != mLastScroll) {
mLastScroll = scrollY;
if (mScrollChangedListener!= null)
mScrollChangedListener.onScrollChanged(scrollY);
}
super.onDraw(c);
}
public void setScrollChangedListener(ScrollChangedListener listener) {
mScrollChangedListener = listener;
}
public interface ScrollChangedListener {
void onScrollChanged(int newScroll);
}
}
class SomeActivity extends Activity implements ScrollChangedListener {
// ...
@Override
void onScrollChanged(int newScroll) {
// Set the background view's position based on newScroll
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3047 次 |
| 最近记录: |