如何在 RecyclerView 上滑动删除?

Posted

技术标签:

【中文标题】如何在 RecyclerView 上滑动删除?【英文标题】:How to swipe to delete on RecyclerView? 【发布时间】:2017-08-05 04:50:26 【问题描述】:

我在这里有一个历史页面,显示用户已接受或发布的请求。我能够成功地为用户放置一个按钮,以删除用户拥有的所有历史记录。现在我希望用户能够通过滑动一次删除一个历史记录。

HistoryFragment.java

RecyclerView rView;
Button clear;
DatabaseReference dRef;
LinearLayoutManager llm;
String id;

View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    myView = inflater.inflate(R.layout.history, container, false);

    FirebaseUser fu = FirebaseAuth.getInstance().getCurrentUser();
    id = fu.getUid();
    dRef = FirebaseDatabase.getInstance().getReference().child("History"+id);
    clear = (Button) myView.findViewById(R.id.clear);
    rView = (RecyclerView) myView.findViewById(R.id.history_list);
    rView.setHasFixedSize(true);
    llm = new LinearLayoutManager(getActivity());
    llm.setReverseLayout(true);
    llm.setStackFromEnd(true);
    rView.setLayoutManager(llm);

    clear.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            dRef.removeValue();
        
    );

    return myView;

@Override
public void onStart() 
    super.onStart();
    FirebaseRecyclerAdapter<HistoryData, HistoryFragment.RequestViewHolder> fra = new FirebaseRecyclerAdapter<HistoryData, HistoryFragment.RequestViewHolder>(
            HistoryData.class,
            R.layout.historylistview,
            HistoryFragment.RequestViewHolder.class,
            dRef
    ) 
        @Override
        protected void populateViewHolder(final HistoryFragment.RequestViewHolder viewHolder, HistoryData model, final int position) 

            viewHolder.setTrans(model.getTrans());
            viewHolder.setRequestDeadline(model.getRequestDeadline());
            viewHolder.setRequestDescription(model.getRequestDescription());
            viewHolder.setDateTimeAccepted(model.getDateTimeAccepted());
            viewHolder.setRequestLocation(model.getRequestLocation());
            viewHolder.setProfile(getApplicationContext() ,model.getProfile());

        

    ;
    rView.setAdapter(fra);

public static class RequestViewHolder extends RecyclerView.ViewHolder
    View myView;

    public RequestViewHolder(View itemView) 
        super(itemView);

        myView = itemView;
    
    public void setTrans(String profile)
        TextView trans = (TextView) myView.findViewById(R.id.transaction);
        trans.setText(profile);
    
    public void setRequestDeadline(String dead)
        TextView rdead = (TextView) myView.findViewById(R.id.rDead);
        rdead.setText(dead);
    
    public void setRequestDescription(String desc)
        TextView rdesc = (TextView) myView.findViewById(R.id.rDesc);
        rdesc.setText(desc);
    
    public void setDateTimeAccepted(String date)
        TextView dta = (TextView) myView.findViewById(R.id.rAccept);
        dta.setText(date);
    
    public void setRequestLocation(String loc)
        TextView rloc = (TextView) myView.findViewById(R.id.rLoc);
        rloc.setText(loc);
    
    public void setProfile(Context c, String profile)
        ImageView rprofile = (ImageView) myView.findViewById(R.id.HPicture);
        Picasso.with(c).load(profile).into(rprofile);
    


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
    super.onCreateOptionsMenu(menu, inflater);

我还没有真正尝试过任何事情,因为到目前为止我在这里看到的其他问题似乎与我的代码不相似。

【问题讨论】:

谢谢@Rotwang 可以使用 Swipeable-RecyclerView ***.com/a/61703815/10612618 【参考方案1】:

除非您想从头开始创建功能,否则您将不得不使用现有的库/小部件。

例如:

https://android-arsenal.com/details/1/4897

那里有完整的说明,但基本上:

mRecyclerView = new CdsRecyclerView(this);
mRecyclerView = (CdsRecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// Adapter:

mRecyclerViewAdapter = new RecyclerViewAdapter(this, getData());
mRecyclerView.setAdapter(mRecyclerViewAdapter);

// Enable swipe: 

mRecyclerView.enableItemSwipe();

// Listener:

mRecyclerView.setItemSwipeCompleteListener(new CdsItemTouchCallback.ItemSwipeCompleteListener() 
      @Override
      public void onItemSwipeComplete(int position) 
          Toast.makeText(MainActivity.this, "Item was swiped:" + position,
                  Toast.LENGTH_SHORT).show();
      
  );

【讨论】:

以上是关于如何在 RecyclerView 上滑动删除?的主要内容,如果未能解决你的问题,请参考以下文章

如何禁止recyclerview的滚动时间

RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载

RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载

滑动删除Item,拖拽切换Item,你想了解的都在这儿

如何在具有用于滑动的 ItemTouchHelper 的 RecyclerView 中使 TextView 可滚动?

android recyclerview 是不是能上滑动