【发布时间】:2020-03-19 08:03:18
【问题描述】:
我正在尝试为 recyclerview 制作一个适配器以侦听单击事件并更改文本和编辑文本的颜色。 当我只添加
text.settextView.setHighlightColor(Color.parseColor("~~"));
它工作正常但是当我使用if-else 并添加时
text.settextView.setHighlightColor(Color.parseColor("~~"));
不行……一开始我以为是跳过了一些语句,所以加了Log.i检查,我觉得没有跳过的语句。
static class ViewHolder extends RecyclerView.ViewHolder{
public MyCustomEditTextListener myCustomEditTextListener2;
TextView textView;
EditText editText;
public ViewHolder(@NonNull View itemView, MyCustomEditTextListener myCustomEditTextListener2) {
super(itemView);
textView=itemView.findViewById(R.id.textView2);
editText=itemView.findViewById(R.id.editText2);
this.myCustomEditTextListener2=myCustomEditTextListener2;
this.editText.addTextChangedListener(myCustomEditTextListener2);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos=getAdapterPosition();
if(swtich==0) { //swtich is defined in adapter class(static int swtich=0;)
Log.i("info", "click event & swith");
textView.setHighlightColor(Color.parseColor("#e1bee7"));
//editText.setHighlightColor(Color.parseColor("#FFF1FF"));
swtich = 1;
}else if(swtich==1){
textView.setHighlightColor(Color.parseColor("#FFFFFF"));
editText.setHighlightColor(Color.parseColor("#000000"));
swtich=0;}
if(pos!=RecyclerView.NO_POSITION){
if(mListener!=null){
mListener.onItemClick(view,pos);
}
}
}
});
}
【问题讨论】:
-
你在哪里定义了
swtich?它的价值是什么? (你只测试正好 0 和 1,可以是别的吗?) -
我把它加在上面。公共类 webadapter 扩展 RecyclerView.Adapter
{ static int T=0; ArrayList list =new ArrayList(); -
接受的答案指出这是
else if而不是else。
标签: android android-recyclerview onclicklistener