猿问

如何获取数据并更改所选适配器项的背景颜色?

我正在尝试更改项目bgcolor的recyclerview值以及获取该位置的值。我能够获取这些值并更改它们bgcolor,但我无法正确执行它们。我面临以下问题:1.第一个位置项目的背景总是改变:当我点击它时我想改变它。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);

    }

}


慕姐4208626
浏览 124回答 1
1回答

白板的微信

整数默认值为 0,因此当列表显示时,您的行值将更改第一行的背景颜色。您可以使用private int row = -1 ;代替private int row ;您还需要在用户单击行时更改行值。myAd.layout.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;row = myAd.adapterPosition();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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"/>&nbsp;&nbsp;<item android:drawable="@color/Azure"/></selector>
随时随地看视频慕课网APP

相关分类

Java
我要回答