我正在尝试更改bgcolor
项目的recyclerview
并获取该位置的值。我能够获取这些值并更改bgcolor
,但我没有能够正确地做到这一点。我面临着以下问题:
1.first位置项目的背景总是改变的:我想在单击它时更改它。
2.如果我能够相应地更改bgcolor
,则不会获取值。
这是我的代码: recyclerview的适配器类
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAd> {
private static Clickitem clickitem;
private ArrayList<String> names = new ArrayList<>();
private Context context;
private int row ;
String defaulter;
List<Item> items;
boolean isSeleceted = false;
private int selectedPosition = -1;
private String onlygps, onlysid, onlygpsnew, onlygpsnewer;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
@NonNull
@Override
public MyAd onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.list_recycler, viewGroup, false);
return new MyAd(view);
}
MyAdapter(ArrayList<String> names, Context context) {
this.names = names;
this.context = context;
clickitem = (Clickitem) context;
}
public static Clickitem getClickitem() {
return clickitem;
}
public static void setClickitem(Clickitem clickitem) {
MyAdapter.clickitem = clickitem;
}
@Override
public void onBindViewHolder(@NonNull final MyAd myAd, final int i) {
String full = names.get(i);
onlygps = full.substring(0, full.indexOf("/"));
onlysid = full.substring(full.lastIndexOf("/") + 1);
myAd.GPSname.setText(onlygps);
myAd.logs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String full = names.get(i);
onlygpsnew = full.substring(0, full.indexOf("/"));
Intent intent = new Intent(context, LogsHistory.class);
intent.putExtra("GPS", onlygpsnew);
context.startActivity(intent);
}
});
if (row == i) {
myAd.layout.setBackgroundColor(Color.YELLOW);
} else {
myAd.layout.setBackgroundColor(Color.WHITE);
}
myAd.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t = names.get(i);
notifyDataSetChanged();
clickitem.onitemsclicklistener(i, v);
String fuller = names.get(i);
onlygpsnewer = fuller.substring(0, fuller.indexOf("/"));
Toast.makeText(context, onlygpsnewer + " " + "Selected", Toast.LENGTH_SHORT).show();
}
}
);
}
@Override
public int getItemCount() {
return names.size();
}
public class MyAd extends RecyclerView.ViewHolder {
TextView GPSname;
LinearLayout layout, logs;
TextView et;
public MyAd(@NonNull View itemView) {
super(itemView);
GPSname = itemView.findViewById(R.id.gpsname);
layout = itemView.findViewById(R.id.containerlayoiut);
logs = itemView.findViewById(R.id.logs);
et = itemView.findViewById(R.id.check);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickitem.onitemsclicklistener(getAdapterPosition(), view);
}
});
}
}
public void setItemClick(MyAdapter.Clickitem click) {
MyAdapter.clickitem = click;
}//setClick
public interface Clickitem {
void onitemsclicklistener(int position, View view);
}
答案 0 :(得分:0)
整数默认值为0,因此当显示列表时,您的行值将更改第一行的背景颜色。
您可以使用
private int row = -1 ;
代替
private int row ;
此外,您还需要通过单击用户来更改行值。
myAd.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
row = myAd.adapterPosition();
notifyDataSetChanged();
另一种使用选择器的方法。我可以更容易阅读和理解。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/AliceBlue" android:state_focused="true"/>
<item android:drawable="@color/Azure"/>
</selector>