我试图通过片段生成自定义组件。我跟着如下。现在我得到一个错误因为接口方法应该在activity中实现(传递的上下文与activity,而不是fragment相关)但我想在片段中处理该方法。我该怎么做? Didi做错了什么?
我收到错误
无法将MainActivity强制转换为EditTextComp $ EditTextOnFocusListener
片段
public class CropDetailsFragment extends Fragment implements
EditTextComp.EditTextOnFocusListener {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
ArrayList<EditTextModel> tx = new ArrayList<EditTextModel>();
EditTextModel cdm = new EditTextModel("", "No of Plants", null);
tx.add(cdm);
adapter = new EditTextCompAdapter(this.getContext(), tx);
recyclerView.setAdapter(adapter);
}
@Override
public void onEditTextchange(String x) {
String xx =x;
}
}
适配器
public class EditTextCompAdapter extends
RecyclerView.Adapter<EditTextCompAdapter.ViewHolder> {
Context context;
ArrayList<EditTextModel> objects;
public EditTextCompAdapter(Context ctx, ArrayList<EditTextModel> objects) {
context = ctx;
this.objects = objects;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public String text;
public ViewHolder(EditTextComp itemView) {
super(itemView);
}
public void setText(EditTextModel editTextModel) {
EditTextComp edittextView = (EditTextComp) itemView;
edittextView.setdata(editTextModel);
try {
edittextView.setEditTextOnFocusListener( (EditTextComp.EditTextOnFocusListener) context);
} catch (ClassCastException e) {
e.printStackTrace();
Log.e("TAG", e.toString());
}
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
EditTextComp customView = new EditTextComp(context);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
customView.setLayoutParams(lp);
return new ViewHolder(customView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
EditTextModel edittext = objects.get(position);
holder.setText(edittext);
}
@Override
public int getItemCount() {
return objects.size();
}
}
自定义组件类
public class EditTextComp extends LinearLayout implements
View.OnFocusChangeListener {
EditText editText = null;
TextView textView = null;
public EditTextComp(Context context) {
super(context, null);
createComponent();
}
public interface EditTextOnFocusListener {
void onEditTextchange(String value);
}
private EditTextOnFocusListener editTextonfocuslistener;
public void setEditTextOnFocusListener(EditTextOnFocusListener listener) {
this.editTextonfocuslistener = listener;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText value = (EditText) v.findViewById(v.getId());
String x = value.getText().toString();
if (editTextonfocuslistener != null) {
editTextonfocuslistener.onEditTextchange(x);
}
}
switch (v.getId()) {
case R.id.editText:
break;
default:
break;
}
}
public EditTextComp(Context context, AttributeSet attrs) {
super(context, attrs, 0);
createComponent();
}
public EditTextComp(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
createComponent();
}
private void createComponent() {
inflate(getContext(), R.layout.compound_comp_edit_text, this);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.text);
}
public void setdata(EditTextModel data) {
editText.setText(data.getValue());
editText.setHint(data.getPlaceholder());
textView.setText((data.getTitle()!=null)? data.getTitle():"");
editText.setOnFocusChangeListener(this);
}
}
我是android新手。请帮帮我
答案 0 :(得分:0)
以下代码返回未实现EditTextOnFocusListener
edittextView.setEditTextOnFocusListener( (EditTextComp.EditTextOnFocusListener) context);
你应该通过构造函数传递监听器,如下所示:
public EditTextCompAdapter(Context ctx, ArrayList<EditTextModel> objects, EditTextComp.EditTextOnFocusListener listenter) {
context = ctx;
this.objects = objects;
this.listener = listener;
}
看Simple Android RecyclerView example,你会发现许多有趣的东西)))