我想在RecyclerView中单击按钮时更改颜色。
当我单击与位置1、2、3、4的位置4对应的按钮时,我想更改4按钮的文本颜色。
public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(int position);
void onLongItemClick(int position);
}
Context context;
List<SingleItem> items;
public static OnItemClickListener onItemClickListener;
public SingleAdapter(Context context, List<SingleItem> items) {
this.context = context;
this.items = items;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
{
this.onItemClickListener = onItemClickListener;
}
@Override
public int getItemCount() {
return items.size();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.single_item, parent, false);
final ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
SingleItem item = items.get(position);
viewHolder.btnItemButton.setText(item.getmSubText());
}
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
Button btnItemButton;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
btnItemButton = itemView.findViewById(R.id.itemButton);
btnItemButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(onItemClickListener != null)
{
onItemClickListener.onItemClick(getAdapterPosition());
}
}
@Override
public boolean onLongClick(View v) {
return false;
}
}
}
public class SingleItem {
String mSubText;
public SingleItem(String mSubText)
{
this.mSubText = mSubText;
}
public String getmSubText() {
return mSubText;
}
public void setmSubText(String mSubText)
{
this.mSubText = mSubText;
}
}
static String mAddListDepthUnits[] = { "m", "ft", "fm", "HR", "pb" };
private void setDepthRecycler()
{
singleItems = new ArrayList<>();
for( int i = 0; i < mAddListDepthUnits.length; i++)
{
singleItems.add(new SingleItem(mAddListDepthUnits[i]));
}
mSingleAdapter = new fec.fishfinderapp.menu.SingleAdapter(mSubMenu_Units.getContext(), singleItems);
midDepthRecycler.setAdapter(mSingleAdapter);
mSingleAdapter.setOnItemClickListener(new SingleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
switch(position)
{
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
break;
}
}
@Override
public void onLongItemClick(int position) {
}
});
}
当前代码处于此状态。 如何在此处更改特定按钮的文本颜色? 我几乎找不到答案。 请帮助我
答案 0 :(得分:0)
据我了解,您需要基于按钮单击来更改文本颜色?
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
SingleItem item = items.get(position);
viewHolder.btnItemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position == 4) {
viewHolder.btnItemButton.setTextColor(Color.RED);
}else{
viewHolder.btnItemButton.setTextColor(Color.GREEN);
}
}
});
viewHolder.btnItemButton.setText(item.getmSubText());
}