更改回收站视图中所选项目的颜色 (Java)

Change color of selected item in recyclerview (Java)

我正在为学校项目开发此应用程序,这是我第一次使用 java 开发应用程序。

我创建了一个 recyclerview 以显示列表中的多个订单。我想让它做的是,当它被点击时,将“nameText”的颜色更改为绿色。

每次我 select 其中一项时,我的代码目前都会使我的应用程序崩溃。

recylerView的主视图:

public class Menu extends AppCompatActivity implements menuAdpter.OnNoteListener {

    RecyclerView recView;
    ArrayList<MenuItem> items = new ArrayList<MenuItem>();
    menuAdpter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        recView = findViewById(R.id.recycleView);
        getMenuItem();
        myAdapter = new menuAdpter(items, this);
        recView.setAdapter(myAdapter);

        recView.setLayoutManager(new LinearLayoutManager(this));


    }

// Getting information from the server:
    public void getMenuItem() {


        try {

            Request getMenuRequest = new Request(FISConstants.GET_MENU);

            String resultFromServerGetMenu = SimpleClient.makeRequest(FISConstants.HOST, getMenuRequest);
//            System.out.println(resultFromServerGetMenu);

            String[] returnedListOfWords = resultFromServerGetMenu.split("-|\,");
            System.out.println(Arrays.toString(returnedListOfWords));

            for (int i = 0; i < returnedListOfWords.length - 3; i = i + 4) {
                MenuItem temp = new MenuItem(returnedListOfWords[i], returnedListOfWords[i + 1], returnedListOfWords[i + 2], returnedListOfWords[i + 3]);
                items.add(temp);
            }
            System.out.println(items);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error with server connection");
        }
    }

    @Override
    public void onNoteClick(int position) {
        System.out.println("Clicked!" + items.get(position).getName());
        menuAdpter.changeText();
    }
}

来自 viewAdpter 的代码:

public class menuAdpter extends RecyclerView.Adapter<menuViewHolder>{

    public static ArrayList<MenuItem> mData;
    private OnNoteListener mOnNoteListener;
    public static menuViewHolder holder;

    public menuAdpter(ArrayList<MenuItem> data, OnNoteListener onNoteListener) {
        mData = data;
        mOnNoteListener = onNoteListener;
    }

    public static ArrayList<MenuItem> getmData() {
        return mData;
    }

    @NonNull
    @Override
    public menuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items_menu, parent, false);
        holder = new menuViewHolder(myView, mOnNoteListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull menuViewHolder holder, int position) {
        // setting each value from the list to each field:
        holder.nameText.setText(mData.get(position).getName());
        holder.priceText.setText("$" + mData.get(position).getPrice());
        holder.descriptionText.setText(mData.get(position).getDescription());
        holder.typeText.setText(mData.get(position).getType());
        holder.amountText.setText("10");
    }

    public static void changeText() {
        System.out.println(holder);
        holder.nameText.setTextColor(0xff00ff00);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public interface OnNoteListener {
        void onNoteClick(int position);
    }
}

来自 viewHolder 的代码:

public class menuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView nameText;
    protected TextView priceText;
    protected TextView descriptionText;
    protected TextView amountText;
    protected TextView typeText;

    public static ArrayList<MenuItem> mData;

    menuAdpter.OnNoteListener onNoteListener;

    public menuViewHolder(@NonNull View itemView, menuAdpter.OnNoteListener onNoteListener) {
        super(itemView);

        nameText = itemView.findViewById(R.id.nameTextView);
        priceText = itemView.findViewById(R.id.priceTextView);
        descriptionText = itemView.findViewById(R.id.descriptionTextView);
        amountText = itemView.findViewById(R.id.amountTextView);
        typeText = itemView.findViewById(R.id.typeTextView);

        this.onNoteListener = onNoteListener;

        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        onNoteListener.onNoteClick(getAdapterPosition());
    }
}

我希望代码做的是 onNoteClick 调用函数 menuAdpter.changeText();并更改 selected 项目的颜色。

如果有人能告诉我如何更改 selected 项目的颜色,我将不胜感激。

谢谢!

您应该在 onclick() 中更新 textView 颜色,试试这个:

  @Override
    public void onClick(View view) {
         textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
    }