How can I make my Android SwipeableCardViews more like the IOS 7 mail app (swipe to show buttons)

后端 未结 3 1822
悲&欢浪女
悲&欢浪女 2021-02-05 18:37

How can I make my SwipeableCardViews more like the IOS 7 mail app (swipe to show buttons)

So far I have created an android application which allows the user

3条回答
  •  猫巷女王i
    2021-02-05 19:11

    I've just done this. This is how it looks like:

    I don't have any CardView but you can have any layout (so you can add the support lib CardView or whatever you like).

    This is how I did it. I used this library. Since the library has minSdkVersion 15 and we have 14, I copy-pasted the following classes directly from GitHub: OnItemClickListener, RecyclerViewAdapter, SwipeableItemClickListener, SwipeToDismissTouchListener and ViewAdapter (this are the only ones you need if you use a RecyclerView).

    Because the lib has not been updated yet, you'll need to add the following method into SwipeableItemClickListener:

    @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
    

    Your list item layout should have the following structure:

    
    
            
        
    
        
            
                
                
            
    
    

    Then simply add this code to your Activity/Fragment after creating the RecyclerView and setting it's adapter:

    final SwipeToDismissTouchListener touchListener =
            new SwipeToDismissTouchListener<>(
                    new RecyclerViewAdapter(mRecyclerView),
                    new SwipeToDismissTouchListener.DismissCallbacks() {
                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }
                        @Override
                        public void onDismiss(RecyclerViewAdapter recyclerView, int position) {
                            // remove item at position and notify adapter
                        }
                    }
            );
    mRecyclerView.setOnTouchListener(touchListener);
    mRecyclerView.addOnScrollListener((RecyclerView.OnScrollListener) touchListener.makeScrollListener());
    mRecyclerView.addOnItemTouchListener(new SwipeableItemClickListener(
            getActivity(),
            new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (view.getId() == R.id.swipe_delete) {
                        touchListener.processPendingDismisses();
                    } else {
                        touchListener.undoPendingDismiss();
                    }
                }
            }
    ));
    

提交回复
热议问题