当我完成在recyclerview中编辑编辑文本时,我正在努力解决这个问题(为什么?因为只有例如编辑文本等于“test”)才能访问recyclerview中的其他对象。
所以我有一个带有许多视图Holders的适配器,这里是编辑文本的适配器:
public EditTextViewHolder(View itemView, final Activity activity, final Context context, final String param) {
super(itemView);
this.activity = activity;
this.context = context;
this.param = param;
name = (TextView) itemView.findViewById(R.id.tEditTextName);
desc = (TextView) itemView.findViewById(R.id.tEditTextDescription);
details = (TextView) itemView.findViewById(R.id.tEditTextMoreDetails);
editText = (EditText) itemView.findViewById(R.id.eEditTextValue);
image = (ImageView) itemView.findViewById(R.id.iEditTextImage);
lMain = (LinearLayout) itemView.findViewById(R.id.layoutTaskEditText);
lOptional = (LinearLayout) itemView.findViewById(R.id.layoutEditTextOptional);
lRequired = (LinearLayout) itemView.findViewById(R.id.isRequiredTask);
}
public void setLayout(final Content content) {
name.setText(content.getTitle());
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
content.getAnswers().get(0).setValue(s.toString().trim());
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus)
TaskActivity.sAdapter.notifyDataSetChanged();
}
});
}
但我收到错误“当RecyclerView正在计算布局或滚动时无法调用此方法”,即使我尝试在处理程序或ui线程中通知它也无法正常工作。
它适用于我所有其他观看者。你知道编辑文本我做错了吗?
答案 0 :(得分:0)
您致电时可能会发生此异常 notifyItemInserted(position);, notifyItemChanged(position),或 notifyItemRemoved(位置);从后台线程(或从 回调,在后台线程上运行。)
要解决此问题,请在UI线程中使用Handler
:
android.os.Handler mHandler = getActivity().getWindow().getDecorView().getHandler();
mHandler.post(new Runnable() {
public void run(){
//change adapter contents
mRecyclerViewAdapter.notifyItemInserted(position);
}
});
答案 1 :(得分:-1)
尝试在适配器上调用notifyDataSetChanged而不是活动,并在UIThread上调用它。
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
并确保适配器内部具有正确数量的数据
@Override
public int getItemCount() {
}