ListFragment 适配器 onClick 仅在第一次点击时执行部分代码,在连续点击时正确执行
ListFragment adapter onClick only executes part of code on first click, executes properly on successive clicks
我可能遗漏了一些明显的东西,但在过去的几个小时里,这件事一直让我抓狂。
我有一个包含多个项目的 ListFragment。在将片段放入 Activity 后单击项目时,附加适配器中 onClick 中的所有代码似乎都会执行,但 mCallback 除外。第二次单击同一项目或选择另一个项目后,mCallback 会正确触发。
我四处寻找类似的东西,但在那些情况下,onClick 似乎 "eaten" 是由于焦点问题而导致的,但由于部分代码执行,这似乎不是这里的问题.
我在这里错过了什么?
TypeAdapter.java
public class TypeAdapter extends ArrayAdapter<Type> {
private Context mContext;
private List<Type> mList;
public OnTypeSelectedListener mCallback;
public int coloredItem = -1;
public TypeAdapter(Context context, List<Type> list) {
super(context, R.layout.typelayout, list);
mContext = context;
mList = list;
mCallback = new OnTypeSelectedListener() {
@Override
public void onTypeSelected(int position, long lotId, long typeId) {
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString()
+ " must implement OnTypeSelectedListener");
}
}
};
}
public interface OnTypeSelectedListener {
void onTypeSelected(int position, long lotId, long typeId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
final int pos = position;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.typelayout, null);
} else {
view = convertView;
}
TextView typeView = (TextView) view.findViewById(R.id.typeView);
typeView.setText(mList.get(position).getBrand() + " Size: " + mList.get(position).getSize());
//edits
typeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View typeView) {
mCallback.onTypeSelected(pos, mList.get(pos).getLotId(), mList.get(pos).getTypeId());
typeView.setBackgroundColor(Color.rgb(255, 228, 12));
setColoredItem(pos);
notifyDataSetChanged();
Log.d("onClickListener", pos + " - " + mList.get(pos).getLotId() + " - " + mList.get(pos).getTypeId());
}
});
if (getColoredItem() == position) {
typeView.setBackgroundColor(Color.rgb(255, 228, 12));
} else {
typeView.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
public int getColoredItem() {
return this.coloredItem;
}
public void setColoredItem(int position) {
this.coloredItem = position;
}
}
问题:
您在第一次点击时更改了 mCallback 的值,没有其他任何反应。
第一次点击时执行以下代码:
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString() + " must implement OnTypeSelectedListener");
}
在下一次点击时,如果 Context 实现了 OnTypeSelectedListener
,您的 mContext 的 onTypeSelected() 将被调用。我想这就是你想要的。
答案:
您可以更改:
mCallback = new OnTypeSelectedListener() {
@Override
public void onTypeSelected(int position, long lotId, long typeId) {
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString()
+ " must implement OnTypeSelectedListener");
}
}
};
至:
if(mContext instanceof OnTypeSelectedListener){
mCallback = (OnTypeSelectedListener) mContext;
}else{
throw new ClassCastException(mContext.toString() + " must implement OnTypeSelectedListener");
}
我可能遗漏了一些明显的东西,但在过去的几个小时里,这件事一直让我抓狂。
我有一个包含多个项目的 ListFragment。在将片段放入 Activity 后单击项目时,附加适配器中 onClick 中的所有代码似乎都会执行,但 mCallback 除外。第二次单击同一项目或选择另一个项目后,mCallback 会正确触发。
我四处寻找类似的东西,但在那些情况下,onClick 似乎 "eaten" 是由于焦点问题而导致的,但由于部分代码执行,这似乎不是这里的问题.
我在这里错过了什么?
TypeAdapter.java
public class TypeAdapter extends ArrayAdapter<Type> {
private Context mContext;
private List<Type> mList;
public OnTypeSelectedListener mCallback;
public int coloredItem = -1;
public TypeAdapter(Context context, List<Type> list) {
super(context, R.layout.typelayout, list);
mContext = context;
mList = list;
mCallback = new OnTypeSelectedListener() {
@Override
public void onTypeSelected(int position, long lotId, long typeId) {
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString()
+ " must implement OnTypeSelectedListener");
}
}
};
}
public interface OnTypeSelectedListener {
void onTypeSelected(int position, long lotId, long typeId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
final int pos = position;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.typelayout, null);
} else {
view = convertView;
}
TextView typeView = (TextView) view.findViewById(R.id.typeView);
typeView.setText(mList.get(position).getBrand() + " Size: " + mList.get(position).getSize());
//edits
typeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View typeView) {
mCallback.onTypeSelected(pos, mList.get(pos).getLotId(), mList.get(pos).getTypeId());
typeView.setBackgroundColor(Color.rgb(255, 228, 12));
setColoredItem(pos);
notifyDataSetChanged();
Log.d("onClickListener", pos + " - " + mList.get(pos).getLotId() + " - " + mList.get(pos).getTypeId());
}
});
if (getColoredItem() == position) {
typeView.setBackgroundColor(Color.rgb(255, 228, 12));
} else {
typeView.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
public int getColoredItem() {
return this.coloredItem;
}
public void setColoredItem(int position) {
this.coloredItem = position;
}
}
问题:
您在第一次点击时更改了 mCallback 的值,没有其他任何反应。 第一次点击时执行以下代码:
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString() + " must implement OnTypeSelectedListener");
}
在下一次点击时,如果 Context 实现了 OnTypeSelectedListener
,您的 mContext 的 onTypeSelected() 将被调用。我想这就是你想要的。
答案:
您可以更改:
mCallback = new OnTypeSelectedListener() {
@Override
public void onTypeSelected(int position, long lotId, long typeId) {
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString()
+ " must implement OnTypeSelectedListener");
}
}
};
至:
if(mContext instanceof OnTypeSelectedListener){
mCallback = (OnTypeSelectedListener) mContext;
}else{
throw new ClassCastException(mContext.toString() + " must implement OnTypeSelectedListener");
}