Android,在 RecyclerView 的每个项目下显示阴影(无边距填充)
Android, display a shadow under each items of a RecyclerView (without margin-padding)
我试图在 RecyclerView 的项目下显示 shadow,但项目之间 没有 space。
我正在尝试重现类似的东西:
我已经实现了一个 RecyclerView 和一个 FlexboxLayout,所以项目占据了整个屏幕的位置。
这是回收站视图声明:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/category_item"
android:clipToPadding="false"
android:paddingEnd="20dp"
android:paddingBottom="20dp"/>
这是项目布局:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:justifyContent="center"
android:elevation="8dp"
>
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:gravity="center"
android:textSize="16sp" />
</com.google.android.flexbox.FlexboxLayout>
以及来自适配器的 onBindViewHolder
方法:
@Override
public void onBindViewHolder(final CategoryViewHolder holder, int position) {
if (categories != null) {
Category category = categories.get(position);
holder.itemTitle.setText("Lorem ipsum");
ViewGroup.LayoutParams lp = holder.itemContainer.getLayoutParams();
holder.itemContainer.setBackgroundResource(category.getColor());
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp =
(FlexboxLayoutManager.LayoutParams) holder.itemContainer.getLayoutParams();
flexboxLp.setFlexGrow(1.0f);
}
}
}
这是我得到的:
如您所见,我放置了一个内边距以便我们可以看到后面的阴影,但是由于项目之间没有 space,因此不会显示阴影。
我希望 shadow 显示在其他项目的 top 上。
我不知道如何在项目之间进行漂亮的分隔,否则我已经尝试过 DividerItemDecoration
但它没有按预期工作。
您可以尝试使用适配器的自定义布局。进入底部的自定义布局,你可以给你的边框或阴影。它会为你工作。
我在这里找到了解决方案:
我创建了一个渐变资源:
<shape xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="@color/c1"
android:centerY="90%"
android:endColor="@color/c1_darker"
android:startColor="@color/c1"
android:type="linear" />
</shape>
我把它设置为我的项目的背景,这样我的项目的顶部更暗,看起来就像是项目上面的阴影。
我试图在 RecyclerView 的项目下显示 shadow,但项目之间 没有 space。
我正在尝试重现类似的东西:
我已经实现了一个 RecyclerView 和一个 FlexboxLayout,所以项目占据了整个屏幕的位置。
这是回收站视图声明:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/category_item"
android:clipToPadding="false"
android:paddingEnd="20dp"
android:paddingBottom="20dp"/>
这是项目布局:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:justifyContent="center"
android:elevation="8dp"
>
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:gravity="center"
android:textSize="16sp" />
</com.google.android.flexbox.FlexboxLayout>
以及来自适配器的 onBindViewHolder
方法:
@Override
public void onBindViewHolder(final CategoryViewHolder holder, int position) {
if (categories != null) {
Category category = categories.get(position);
holder.itemTitle.setText("Lorem ipsum");
ViewGroup.LayoutParams lp = holder.itemContainer.getLayoutParams();
holder.itemContainer.setBackgroundResource(category.getColor());
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp =
(FlexboxLayoutManager.LayoutParams) holder.itemContainer.getLayoutParams();
flexboxLp.setFlexGrow(1.0f);
}
}
}
这是我得到的:
如您所见,我放置了一个内边距以便我们可以看到后面的阴影,但是由于项目之间没有 space,因此不会显示阴影。 我希望 shadow 显示在其他项目的 top 上。
我不知道如何在项目之间进行漂亮的分隔,否则我已经尝试过 DividerItemDecoration
但它没有按预期工作。
您可以尝试使用适配器的自定义布局。进入底部的自定义布局,你可以给你的边框或阴影。它会为你工作。
我在这里找到了解决方案:
我创建了一个渐变资源:
<shape xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="@color/c1"
android:centerY="90%"
android:endColor="@color/c1_darker"
android:startColor="@color/c1"
android:type="linear" />
</shape>
我把它设置为我的项目的背景,这样我的项目的顶部更暗,看起来就像是项目上面的阴影。