如何在RecyclerView上滑动删除?

时间:2017-03-14 17:41:38

标签: android firebase firebase-realtime-database android-recyclerview

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

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);
}

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

2 个答案:

答案 0 :(得分:13)

这很简单,你也可以不使用任何第三方库。

fn.fuzzymatch<-function(dat1,dat2,string1,string2,meth){

  dist.name<-stringdistmatrix(dat1[[string1]],dat2[[string2]],method = meth)

  min.name<-apply(dist.name, 1, min)

  match.s1.s2<-NULL
  for(i in 1:nrow(dist.name))
  {
    s2.i<-match(min.name[i],dist.name[i,])
    s1.i<-i
    match.s1.s2<-rbind(data.frame(s1_row=s1.i,s2_row=s2.i,s1name=dat1[s1.i,][[string1]],s2name=dat2[s2.i,][[string2]], dist=min.name[i]),match.s1.s2)
  }
  output <- (match.s1.s2)[order(match.s1.s2$s1_row),]
  return(output)
}


match_50 <- fn.fuzzymatch(data1,data2,"Address1","Address2","dl")

现在是回调,

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(rv);

您现在可以识别您的哪个项目已被刷过。您可以从适配器中删除该项目,并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) { Toast.makeText(MyActivity.this, "on Move", Toast.LENGTH_SHORT).show(); return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { Toast.makeText(MyActivity.this, "on Swiped ", Toast.LENGTH_SHORT).show(); //Remove swiped item from list and notify the RecyclerView final int position = viewHolder.getAdapterPosition(); adapter.notifyItemRemoved(position); } };

答案 1 :(得分: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();
      }
  });