[英]Removing item from RecyclerView and inserting it to another position
[英]Get item position of RecyclerView from another class
我有一个CardView
列表,其中包含约20张使用RecyclerView
和Adapter
制作的Adapter
。
单击Card项目后,我希望它启动包含另一个CardView
列表的新intent
。 我可以这样做,但我也希望它根据单击的卡位置来设置卡颜色。
例如-如果单击卡片项目Red
,它将开始新的意图类别,并将卡片颜色设置为Red
阴影(我可以定义它)。 并与其他色卡项目类似。
这可能吗?
您可以简单地从Adapter
获取CardView
的位置。
这将为您提供帮助。
首先通过捆绑将颜色作为字符串传递给新活动:
Intent mIntent = new Intent(mContext, YourNewActivity.class);
mIntent.putExtra("color", yourColorString);
startActivity(mIntent)
在新的活动中,从捆绑中获取颜色。
String color = "#000";
Bundle bundle = getIntent().getExtras();
if(bundle != null){
color = bundle.getString(color,"#000");
}
将颜色传递给适配器构造函数中的适配器,然后在适配器中找到卡视图并设置背景颜色,如下所示:
cardView.setBackgroundColor(Color.parseColor(color));
祝好运。
以这种方式...在适配器内
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
//..... your rest code
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,SecondActivity.class);
intent.putExtra("CardPosition",position); //for positiion
intent.putExtra("CardColor",
list.get(position).getColor()); //for value
context.startActivity(intent);
}
});
}
在第二个活动中,您可以获取cardview的位置...根据您的要求更改方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.