RecyclerView 小部件行上的斑马条纹颜色效果
Zebra-striping color-effect on RecycledView widget rows
我想要做的是为 RecycledView 中的每一行分配不同的颜色。我创建了一个自定义 RecycledView.Adapter,在 onCreateViewHolder 方法中我有这个:
// Create a row_folder view
View view = inflater.inflate(R.layout.row_folder, parent, false);
if(position % 2 == 0) {
view.setBackgroundColor(Color.BLACK);
}
// Use MyViewHolder to bind the view
MyViewHolder holder = new MyViewHolder(view);
return holder;
颜色未分配。我错过了什么?谢谢
已编辑:
row_folderxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="@color/my_white"
android:orientation="horizontal">
<ImageView
android:id="@+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="@drawable/folder_icon" />
<TextView
android:id="@+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="@drawable/folder_content_icon" />
<TextView
android:id="@+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
在你的回收器视图适配器中做这样的事情:
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
EDIT:
像这样更改您的 xml 文件:row_folder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="@color/my_white"
android:orientation="horizontal">
<ImageView
android:id="@+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="@drawable/folder_icon" />
<TextView
android:id="@+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="@drawable/folder_content_icon" />
<TextView
android:id="@+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
在您的 ViewHolder class 中再创建一个变量,如下所示:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
我希望它能奏效。
我把它放在这里只是为了那些试图在基本上包含 CardView 的 RecyclerView 的行上应用类似斑马条纹的颜色效果的人参考。
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--Content goes here-->
</android.support.v7.widget.CardView>
现在,在适配器端,首先初始化 CardView(我使用的是 butterknife 绑定)。
public class YooViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.cv)
CardView cardView;
public YooViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
最后在 onBindViewHolder
处放置这一行代码。
holder.cardView.setCardBackgroundColor(
ContextCompat.getColor(mContext,
position % 2 == 0 ? R.color.white : R.color.white_grayish));
请务必在 CardView 上使用 setCardBackgroundColor
(而不是 setBackgroundColor
)。
我想要做的是为 RecycledView 中的每一行分配不同的颜色。我创建了一个自定义 RecycledView.Adapter,在 onCreateViewHolder 方法中我有这个:
// Create a row_folder view
View view = inflater.inflate(R.layout.row_folder, parent, false);
if(position % 2 == 0) {
view.setBackgroundColor(Color.BLACK);
}
// Use MyViewHolder to bind the view
MyViewHolder holder = new MyViewHolder(view);
return holder;
颜色未分配。我错过了什么?谢谢
已编辑: row_folderxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="@color/my_white"
android:orientation="horizontal">
<ImageView
android:id="@+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="@drawable/folder_icon" />
<TextView
android:id="@+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="@drawable/folder_content_icon" />
<TextView
android:id="@+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
在你的回收器视图适配器中做这样的事情:
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
EDIT:
像这样更改您的 xml 文件:row_folder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="@color/my_white"
android:orientation="horizontal">
<ImageView
android:id="@+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="@drawable/folder_icon" />
<TextView
android:id="@+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="@drawable/folder_content_icon" />
<TextView
android:id="@+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="@color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
在您的 ViewHolder class 中再创建一个变量,如下所示:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
我希望它能奏效。
我把它放在这里只是为了那些试图在基本上包含 CardView 的 RecyclerView 的行上应用类似斑马条纹的颜色效果的人参考。
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--Content goes here-->
</android.support.v7.widget.CardView>
现在,在适配器端,首先初始化 CardView(我使用的是 butterknife 绑定)。
public class YooViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.cv)
CardView cardView;
public YooViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
最后在 onBindViewHolder
处放置这一行代码。
holder.cardView.setCardBackgroundColor(
ContextCompat.getColor(mContext,
position % 2 == 0 ? R.color.white : R.color.white_grayish));
请务必在 CardView 上使用 setCardBackgroundColor
(而不是 setBackgroundColor
)。