我正在开发一个android应用程序,我正在使用RecyclerView。我需要在RecyclerView中添加一个分隔符。
我试着加上-
recyclerView.addItemDecoration(new
DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
下面是我的XML代码-
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
/>
在res/drawable文件夹中创建一个单独的xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="@android:color/black" />
</shape>
在主活动中连接那个xml文件(your_file),像这样:
DividerItemDecoration divider = new DividerItemDecoration(
recyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.your_file));
recyclerView.addItemDecoration(divider);
在res/drawable文件夹中创建一个单独的xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="@android:color/black" />
</shape>
在主活动中连接那个xml文件(your_file),像这样:
DividerItemDecoration divider = new DividerItemDecoration(
recyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.your_file));
recyclerView.addItemDecoration(divider);
好了,如果不需要改变你的分割线颜色,只需对分割线装饰应用alpha。
GridLayoutManager透明的示例:
DividerItemDecoration horizontalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.HORIZONTAL);
horizontalDividerItemDecoration.getDrawable().setAlpha(50);
DividerItemDecoration verticalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.VERTICAL);
verticalDividerItemDecoration.getDrawable().setAlpha(50);
my_recycler.addItemDecoration(horizontalDividerItemDecoration);
my_recycler.addItemDecoration(verticalDividerItemDecoration);
你仍然可以通过设置颜色来改变分割线的颜色
过滤到它。
GridLayoutManager设置tint的例子:
DividerItemDecoration horizontalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.HORIZONTAL);
horizontalDividerItemDecoration.getDrawable().setTint(getResources().getColor(R.color.colorAccent));
DividerItemDecoration verticalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.VERTICAL);
verticalDividerItemDecoration.getDrawable().setAlpha(50);
my_recycler.addItemDecoration(horizontalDividerItemDecoration);
my_recycler.addItemDecoration(verticalDividerItemDecoration);
此外,您还可以尝试设置颜色滤镜,
horizontalDividerItemDecoration.getDrawable().setColorFilter(colorFilter);