我可能错过了一些明显的东西,但这件事让我在过去的几个小时里疯狂了。
我有一个包含多个项目的ListFragment。在片段放入Activity后单击一个项目时,附加适配器中的onClick中的所有代码似乎都被执行,除了mCallback。第二次单击同一项目或选择其他项目后,mCallback会正常激活。
我已经环顾四周寻找类似的东西了,但在那些情况下,onClick似乎已经被“吃掉了”#34;由焦点问题,这似乎不是因为部分代码执行的问题。
我在这里缺少什么?
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;
}
}
答案 0 :(得分:0)
<强>问题:强>
您正在更改第一次单击时mCallback的值,而不会发生任何其他情况。 在第一次单击时执行以下代码:
try {
mCallback = (OnTypeSelectedListener) mContext;
} catch (ClassCastException e) {
throw new ClassCastException(mContext.toString() + " must implement OnTypeSelectedListener");
}
在下一次单击时,如果上下文实现了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");
}