Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swipe to delete on RecyclerView?

What I have here a History page that displays the requests the user have accepted or posted. I was able to successfully put a button for the user to delete all of the history the user has. Now I want the user to be able to delete a history one at a time by swiping it.

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

I haven't really tried anything because the other questions I've seen here so far doesn't seem to be similar to my codes.

like image 868
exc3m Avatar asked Mar 14 '17 17:03

exc3m


1 Answers

You will have to use an existing library / widget unless you want to create the functionality from scratch.

For instance:

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

You have full instructions there but basically:

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();
      }
  });
like image 182
SuppressWarnings Avatar answered Sep 27 '22 19:09

SuppressWarnings