Swipe ListView item From right to left show delete button

后端 未结 11 808
轻奢々
轻奢々 2020-12-07 09:15

I have a custom ListView showing the list of words selecting from database. When I swipe this listview item i want to show Delete button like image below. And when I press t

11条回答
  •  情话喂你
    2020-12-07 09:20

    I have gone through tons of third party libraries to try to achieve this. But none of them exhibits smoothness and usability experience which i wanted. Then i decided to write it myself. And the result was , well, i loved it. I will share the code here. Maybe i will write it as a library which can be embedded in any recycler view in future. But for now here is the code.

    Note: i have uses recycler view and ViewHolder. Some values are hardcoded so change them according to your requirement.

    • row_layout.xml

      
          

    • ChatAdaptor.java

      public class ChatAdaptor extends RecyclerView.Adapter {

      List sessions;
      Context context;
      ChatAdaptorInterface listener;
      
      public interface ChatAdaptorInterface{
          void cellClicked(MXGroupChatSession session);
          void utilityButton1Clicked(MXGroupChatSession session);
          void utilityButton2Clicked(MXGroupChatSession session);
      }
      
      public ChatAdaptor(List sessions, ChatAdaptorInterface listener, Context context){
          this.sessions=sessions;
          this.context=context;
          this.listener=listener;
      }
      
      @Override
      public ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view=(View)LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_row,null);
          ChatViewHolder chatViewHolder=new ChatViewHolder(view);
          return chatViewHolder;
      }
      
      @Override
      public void onBindViewHolder(ChatViewHolder holder, final int position) {
          MXGroupChatSession session=this.sessions.get(position);
          holder.selectedSession=session;
          holder.titleView.setText(session.getTopic());
          holder.subtitleView.setText(session.getLastFeedContent());
          Picasso.with(context).load(new File(session.getCoverImagePath())).transform(new CircleTransformPicasso()).into(holder.imageView);
      }
      
      @Override
      public int getItemCount() {
          return sessions.size();
      }
      
      public class ChatViewHolder extends RecyclerView.ViewHolder{
          ImageView imageView;
          TextView titleView;
          TextView subtitleView;
          ViewGroup cell;
          ViewGroup cellContainer;
          Button button1;
          Button button2;
      
          MXGroupChatSession selectedSession;
          private GestureDetectorCompat gestureDetector;
      
          float totalx;
          float buttonTotalWidth;
      
          Boolean open=false;
          Boolean isScrolling=false;
      
      
          public ChatViewHolder(View itemView) {
              super(itemView);
              cell=(ViewGroup) itemView.findViewById(R.id.chat_row_cell);
              cellContainer=(ViewGroup) itemView.findViewById(R.id.chat_row_container);
              button1=(Button) itemView.findViewById(R.id.slide_button_1);
              button2=(Button) itemView.findViewById(R.id.slide_button_2);
      
              button1.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      listener.utilityButton1Clicked(selectedSession);
                  }
              });
      
              button2.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      listener.utilityButton2Clicked(selectedSession);
                  }
              });
      
              ViewTreeObserver vto = cellContainer.getViewTreeObserver();
              vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {
                      buttonTotalWidth = button1.getWidth()+button2.getWidth();
                  }
              });
      
              this.titleView=(TextView)itemView.findViewById(R.id.chat_title);
              subtitleView=(TextView)itemView.findViewById(R.id.chat_subtitle);
              imageView=(ImageView)itemView.findViewById(R.id.chat_image);
              gestureDetector=new GestureDetectorCompat(context,new ChatRowGesture());
      
              cell.setOnTouchListener(new View.OnTouchListener() {
                  @Override
                  public boolean onTouch(View v, MotionEvent event) {
                      if(gestureDetector.onTouchEvent(event)){
                          return true;
                      }
      
                      if(event.getAction() == MotionEvent.ACTION_UP) {
                          if(isScrolling ) {
                              isScrolling  = false;
                              handleScrollFinished();
                          };
                      }
                      else if(event.getAction() == MotionEvent.ACTION_CANCEL){
                          if(isScrolling ) {
                              isScrolling  = false;
                              handleScrollFinished();
                          };
                      }
                      return false;
                  }
              });
          }
      
      
          public class ChatRowGesture extends GestureDetector.SimpleOnGestureListener {
      
              @Override
              public boolean onSingleTapUp(MotionEvent e) {
                  if (!open){
                      listener.cellClicked(selectedSession);
                  }
                  return true;
              }
      
              @Override
              public boolean onDown(MotionEvent e) {
                  return true;
              }
      
              @Override
              public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                      isScrolling=true;
                      totalx=totalx+distanceX;
                      freescroll(totalx);
      
                  return true;
              }
          }
      
          void handleScrollFinished(){
              if (open){
                  if (totalx>2*buttonTotalWidth/3){
                      slideLeft();
                      totalx=buttonTotalWidth;
                  }else{
                      slideRight();
                      totalx=0;
                  }
              }else{
                  if (totalx>buttonTotalWidth/3){
                      slideLeft();
                      totalx=buttonTotalWidth;
                  }else{
                      slideRight();
                      totalx=0;
                  }
              }
      
          }
      
          void slideRight(){
              TransitionManager.beginDelayedTransition(cellContainer);
              ViewGroup.MarginLayoutParams params;
              params=(ViewGroup.MarginLayoutParams) cell.getLayoutParams();
              params.setMargins(0,0,0,0);
              cell.setLayoutParams(params);
              open=false;
          }
      
          void slideLeft(){
              TransitionManager.beginDelayedTransition(cellContainer);
              ViewGroup.MarginLayoutParams params;
              params=(ViewGroup.MarginLayoutParams) cell.getLayoutParams();
              params.setMargins(((int)buttonTotalWidth*-1),0,(int)buttonTotalWidth,0);
              cell.setLayoutParams(params);
              open=true;
          }
          void freescroll(float x){
              if (x0){
                  int xint=(int)x;
                  ViewGroup.MarginLayoutParams params;
                  params=(ViewGroup.MarginLayoutParams) cell.getLayoutParams();
                  params.setMargins(params.leftMargin,0,xint,0);
                  cell.setLayoutParams(params);
              }
          }
      }
      

    Hope this helps someone!!

提交回复
热议问题