这是适配器!
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryAdapterObjectHolder> {
private static String LOG_TAG = "CategoryAdapter";
private List<Category> mCategory;
private static MyClickListener myClickListener;
public static class CategoryAdapterObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView mTextTitle;
public CategoryAdapterObjectHolder(View itemView) {
super(itemView);
mTextTitle = (TextView) itemView.findViewById(R.id.text_menu);
DebugTool.logD(LOG_TAG + "Adding Listener");
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public CategoryAdapter(List<Category> myHistoryData) {
mCategory = myHistoryData;
}
@Override
public CategoryAdapter.CategoryAdapterObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_menu, parent, false);
CategoryAdapterObjectHolder dataObjectHolder = new CategoryAdapterObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(CategoryAdapter.CategoryAdapterObjectHolder holder, int position) {
holder.mTextTitle.setText(mCategory.get(position).getName());
}
public void addItem(Category dataObj, int index) {
mCategory.add(dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mCategory.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mCategory.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
这是OnItemClick
@Override
public void onResume() {
super.onResume();
((CategoryAdapter) adapter).setOnItemClickListener(
new CategoryAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
TextView mTextItem = (TextView) v.findViewById(R.id.text_menu);
// mTextItem.setTextColor(getResources().getColor(R.color.color_text_white));
if (position == 0) {
Utils.showToast(getContext(), "POSITION 0");
} else if (position == 1) {
Utils.showToast(getContext(), "POSITION 1");
} else if (position == 2) {
Utils.showToast(getContext(), "POSITION 2");
} else if (position == 3) {
Utils.showToast(getContext(), "POSITION 3");
}
}
});
}
我的项目有白色。
我想要的时候:
我点击位置== 0。
文字颜色项目位置== 0变为黑暗。和位置不同有白色。
我点击位置== 1:
位置== 1的文字颜色项目变为深色。并将不同的背面颜色定位为白色。
我点击位置== 2:
位置的文字颜色== 2更改为深色并将不同的背面颜色设置为白色。
我点击位置== 3:
位置的文字颜色== 3更改为深色并将不同的背面颜色设置为白色。
请。救救我!
答案 0 :(得分:0)
我不确定我是否完全了解您要实现的目标,但如果您尝试更改TextField
文字的颜色并假设您正在调用OnItemClickListener
:
创建一个包含所有TextView
的字段。
private TextView[] textViews;
使用onCreateView
方法。
textViews = new TextView[4];
textViews[0] = (TextView) view.findViewById(R.id.textViewOne);
textViews[1] = (TextView) view.findViewById(R.id.textViewTwo);
textViews[2] = (TextView) view.findViewById(R.id.textViewThree);
textViews[3] = (TextView) view.findViewById(R.id.textViewFour);
现在在OnItemClickListener
@Override
public void onResume() {
super.onResume();
((CategoryAdapter) adapter).setOnItemClickListener(
new CategoryAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Color white = getResources().getColor( R.color.color_text_white );
Color dark = getResources().getColor( R.color.color_text_dark );
for ( int i = 0; i < textViews.length; i++ ) {
TextView textView = textViews[i];
if ( i == position )
textView.setTextColor( dark );
else
textView.setTextColor( white );
}
}
});
}
答案 1 :(得分:0)
1-在适配器中创建变量 currentPosition 默认= -1
private int currentPosition =-1;
2- onBindViewHolder 设置默认文字颜色黑色,当用户点击查看时:
@Override
public void onBindViewHolder(final DataObjectHolder holder, final int position) {
ViewGroup.LayoutParams layoutParams = holder.view.getLayoutParams();
holder.view.setLayoutParams(layoutParams);
final String text = mDataSet.get(position);
holder.textView.setTextColor(Color.BLACK);
holder.textView.setText(text);
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentPosition>=0) {
notifyItemChanged(currentPosition);
}
holder.textView.setTextColor(Color.WHITE);
currentPosition=position;
}
});
}
答案 2 :(得分:0)
class ColorAdapter extends RecyclerView.Adapter<ColorSizeViewHolder> {
Context context;
ArrayList<ColorContents> colorContents;
int current_position=-1;
int previous_position=-1;
public ColorAdapter(Context context, ArrayList<ColorContents> colorContents) {
this.context = context;
this.colorContents = colorContents;
}
@NonNull
@Override
public ColorSizeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(context).inflate(R.layout.color_size_model,parent,false);
return new ColorSizeViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ColorSizeViewHolder holder, final int position) {
holder.color_size.setText(colorContents.get(position).getColor());
holder.color_size.setTextColor(context.getResources().getColor(R.color.colorAccent));
holder.color_size.setTag(position);
holder.color_size.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (int)v.getTag();
holder.color_size.setTextColor(context.getResources().getColor(R.color.colorPrimary));
current_position=pos;
if (previous_position!=-1 && current_position!=previous_position)
{
notifyItemChanged(previous_position);
}
previous_position=pos;
}
});
holder.setItemClickListner(new ItemClickListner() {
@Override
public void onItemClick(int pos) {
}
});
}
@Override
public int getItemCount() {
return colorContents.size();
}
}