に基づいて終了する項目を移動RecyclerViewおそらくこれは簡単な質問ですが、私はまだ解決策を見つけることができませんでした。チェックボックス
私はRecyclerView
にCardView
を要素として持ち、各CardViewにはCheckBox
ビューがあります。
私はTodo'sのリストを作成し、チェックボックスをチェックすると、 はチェックされた項目がリストの最後に移動します。
現在、私のアダプタは次のとおりです。
private class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
protected class ViewHolder extends RecyclerView.ViewHolder
{
TextView todoTextView;
TextView addedOnTextView;
TextView dueToDateTextView;
TextView dueToHourTextView;
CheckBox checkBox;
protected ViewHolder(CardView cv)
{
super(cv);
this.todoTextView = (TextView) cv.findViewById(R.id.todo_text);
this.addedOnTextView = (TextView) cv.findViewById(R.id.added_on);
this.dueToDateTextView = (TextView) cv.findViewById(R.id.due_to_date);
this.dueToHourTextView = (TextView) cv.findViewById(R.id.due_to_hour);
this.checkBox = (CheckBox)cv.findViewById(R.id.checkbox);
}
}
private final String[] COLORS = {"#64B5F6", "#F44336"};
private ArrayList<Todo> mTodos;
public Adapter()
{
this.mTodos = new ArrayList<>();
}
public void addItem(Todo message)
{
this.mTodos.add(message);
notifyItemInserted(getItemCount());
}
@Override
public int getItemCount() {
return mTodos.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cView = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.todolist_item,
parent, false);
cView.setCardBackgroundColor(Color.parseColor(COLORS[viewType]));
ViewHolder hView = new ViewHolder(cView);
return hView;
}
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Todo currentTodo = mTodos.get(position);
holder.todoTextView.setText(currentTodo.getmTodo());
holder.dueToDateTextView.setText(currentTodo.getDueToDate());
holder.dueToHourTextView.setText(currentTodo.getDueToHour());
holder.addedOnTextView.setText(currentTodo.getAddedStr());
holder.checkBox.setChecked(currentTodo.isChecked());
final int myPos = holder.getAdapterPosition();
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
currentTodo.setChecked();
buttonView.setEnabled(false);
mTodos.add(mTodos.remove(myPos));
notifyDataSetChanged();
}
});
}
私は、これがこれを行うための正しい方法ではありません知っている、そして現在私は 次の例外取得しています:
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
を私はOnCheckedStateChanged
を知っていますメソッドはOnCreateViewHolder
メソッドで定義する必要がありますが、現在の アイテムの位置がリストの最後に移動する位置がありません。
誰でも私にその仕事を手伝ってもらえますか?
例外が関係する機能は何ですか? – Cochi
OnBindViewHolderでonCheckedStateChanged、notifyDataSetChangeための具体的コール() – dor132
確認[本](http://stackoverflow.com/questions/27070220/recycleview-notifydatasetchanged-illegalstateexception)アウト。 – xiaoyuan