リサイクルビューからアイテムをスワイプしたい場合、アイテムをスワイプしたときにボトムアイテムが上に移動する必要があります。アダプタでcontentprovidersを使用しているときにrecyclerviewのアイテムをスワイプする方法
私の断片のonCreateViewでnew ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//Toast.makeText(getActivity(),"ByBy",Toast.LENGTH_LONG).show();
myListCursorAdapter.onItemRemove(viewHolder, recyclerView);
}
}).attachToRecyclerView(recyclerView);
public class TaskCursorAdapter extends RecyclerCursorAdapter<TaskCursorAdapter.ViewHolder> {
Context context;
Cursor cursor;
public TaskCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.cursor = cursor;
this.context = context;
setHasStableIds(true);
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView taskname;
public TextView dateDetails;
Context context;
Cursor cursor;
public ViewHolder(View view, Context context, Cursor cursor) {
super(view);
this.context = context;
this.cursor = cursor;
taskname = (TextView) view.findViewById(R.id.listitemtaskname);
dateDetails = (TextView) view.findViewById(R.id.listitemdatedetails);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Form the content URI that represents the specific pet that was clicked on,
// by appending the "id" (passed as input to this method) onto the
// {@link PetEntry#CONTENT_URI}.
// For example, the URI would be "content://com.example.android.pets/pets/2"
// if the pet with ID 2 was clicked on.
Intent intent = new Intent(context, EditorActivity.class);
Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, this.getAdapterPosition() + 1);
intent.setData(currentPetUri);
context.startActivity(intent);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view, context, cursor);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
int taskColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.NAME);
int dateColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.DUEDATE);
int timeColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.DUETIME);
String task = cursor.getString(taskColumnIndex);
String dateString = cursor.getString(dateColumnIndex);
String timeString = cursor.getString(timeColumnIndex);
viewHolder.cursor = cursor;
viewHolder.taskname.setText(task);
if (timeString == null && dateString == null) {
viewHolder.dateDetails.setVisibility(View.GONE);
} else {
viewHolder.dateDetails.setText(dateString + "\t\t" + timeString);
}
}
public void onItemRemove(final RecyclerView.ViewHolder viewHolder, final RecyclerView recyclerView) {
int adapterPosition = viewHolder.getAdapterPosition()+1;
Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, viewHolder.getAdapterPosition());
Snackbar snackbar = Snackbar
.make(recyclerView, "PHOTO REMOVED", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
int mAdapterPosition = viewHolder.getAdapterPosition() + 1;
Toast.makeText(context,"Hi",Toast.LENGTH_LONG).show();
recyclerView.scrollToPosition(mAdapterPosition);
}
});
snackbar.show();
context.getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI_FINISHED, values);
// Toast.makeText(context,"Hi2",Toast.LENGTH_LONG).show();
//notifyItemChanged(adapterPosition);
recyclerView.scrollToPosition(adapterPosition);
}
}
私はrecyclerviewとするときスナックバーのアンドゥが、私はその項目に戻りたい押されてからアイテムをスワイプするために、その難しいが、すべてのものを試してみました。私はrecyclerviewからアイテムをスワイプしたい、アイテムがスワイプされたときにボトムアイテムが上に移動するはずです。
私の質問を確認してください、私は既にメソッドをオーバーライドして、私はコンテンツプロバイダでsqliteを使用しているので、私は使用できませんarraylist – Cycle