0

我想从recyclerview中滑动项目,并且在滑动项目时底部项目应向上移动。如何在使用内容提供者时在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中滑动物品,当物品被滑动时,底部物品应该向上移动。

回答

1

1)您需要覆盖方法onSwiped(),如下所示。

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN | ItemTouchHelper.UP) { 

      @Override 
      public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
       //do something 
       return false; 
      } 

      @Override 
      public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {  
       int position = viewHolder.getAdapterPosition(); 
       arrayList.remove(position);//remove swiped item 
       adapter.notifyDataSetChanged();//notify the recyclerview changes in the dataset 

      } 
    }; 

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); 
itemTouchHelper.attachToRecyclerView(rv);//attach to your recyclerview 

2)不要忘记导入RecyclerView库的v22.2。+。 3)对于botom项目向上移动(更新),您需要在“adapter.notifyDataSetChanged()”首次更新数据库之后执行刷新,然后从数据库中提取新数据(包含更改)。这将更新数据库以及recyclerview。

4)要撤消,您需要保留一堆被删除的元素(LIFO顺序),并在您单击撤消时将其插回。

+0

请检查我的问题,我已经覆盖了该方法,我不能使用arraylist,因为我使用内容提供商的sqlite – Cycle

相关问题