简体   繁体   English

在Android中单击RecylerView的项目时如何更改文本颜色

[英]How to change text color when click item of RecylerView in android

This is Adapter! 这是适配器!

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);
  }
}                               

This is OnItemClick 这是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");
                    } 
                }
            });
}

My item it have colour white. 我的物品有白色。

I want when : 我想要什么时候:

I click at position == 0. 我在位置== 0上单击。

Text Color item of position == 0 change to dark. 位置== 0的“文字颜色”项目变为暗。 and position different have colour white. 和位置不同的颜色为白色。

I click position == 1 : 我点击位置== 1:

Text colour item of position == 1 change to colour dark. 位置== 1的文本颜色项目变为深颜色。 And position different back colour white. 与位置不同的背色为白色。

I click position == 2 : 我点击位置== 2:

Text colour of position == 2 change to Dark and position different back colour white. 位置== 2的文本颜色更改为“暗”,而位置的背面颜色为白色。

I click position == 3 : 我点击位置== 3:

Text colour of position == 3 change to Dark and position different back colour white. 位置== 3的文本颜色变为“暗”,而位置的背面颜色为白色。

Please. 请。 Help me! 帮我!

I'm not sure I understand fully what you are trying to achieve but if you are trying to change the colour of your TextField 's text and assuming your OnItemClickListener is being called: 我不确定我是否完全理解您要实现的目标,但是如果您要更改TextField文本的颜色并假设您的OnItemClickListener被调用,则不会:

Create a field that will hold all of your TextView s. 创建一个将包含所有TextView的字段。

private TextView[] textViews;

In your onCreateView method. 在您的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);

Now in your OnItemClickListener 现在在您的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- Create variable currentPosition default = -1 in adapter 1-在适配器中创建变量currentPosition默认= -1

private int currentPosition =-1;

2- onBindViewHolder set default text color Black and when user click view: 2- onBindViewHolder设置默认文本颜色为黑色 ,当用户单击视图时:

  • notifyItemChanged(currentPosition) - update default view for old view(reload) notifyItemChanged(currentPosition)-更新旧视图的默认视图(重新加载)
  • Set text color White . 设置文本颜色为白色


@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;
            }
        });
}

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();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM