Search code examples
androidandroid-listviewpull-to-refreshswipe-gesture

Android ListView Pull to refresh and Swipe List item to reveal buttons


I am working on Android ListView. I implemented pull to refresh through XListView, But now I also want to implement Swipe Left to right to show buttons List Item on this ListView. How can I do it? Or How to add 2 libs as same on ListView.

My ListView in XML is.

<com.orderlyexpo.www.listview.refresh.XListView
        android:id="@+id/lvOrders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/gray_text"
        android:dividerHeight="@dimen/dp1x" /> 

Solution

  • Don't use any lib for swipe, make your own view and you can use Pull to refresh same lib.

    Just do it this way.

    Add a class name.

    SwipeDetector.java

    public class SwipeDetector implements View.OnTouchListener {
    
    public static enum Action {
        LR, // Left to Right
        RL, // Right to Left
        TB, // Top to bottom
        BT, // Bottom to Top
        None // when no action was detected
    }
    
    private static final String logTag = "SwipeDetector";
    private static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    private Action mSwipeDetected = Action.None;
    
    public boolean swipeDetected() {
        return mSwipeDetected != Action.None;
    }
    
    public Action getAction() {
        return mSwipeDetected;
    }
    
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.None;
            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getX();
            upY = event.getY();
    
            float deltaX = downX - upX;
            float deltaY = downY - upY;
    
            // horizontal swipe detection
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
               //     Logger.show(Log.INFO,logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return true;
                }
                if (deltaX > 0) {
              //      Logger.show(Log.INFO,logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return true;
                }
            } else 
    
                // vertical swipe detection
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
               //         Logger.show(Log.INFO,logTag, "Swipe Top to Bottom");
                        mSwipeDetected = Action.TB;
                        return false;
                    }
                    if (deltaY > 0) {
               //         Logger.show(Log.INFO,logTag, "Swipe Bottom to Top");
                        mSwipeDetected = Action.BT;
                        return false;
                    }
                } 
            return true;
        }
        }
        return false;
    }
    }
    

    And then call it from your ListView or item onClickListner mathod. I called it from ItemClick from baseadapter.

    convertView.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(swipeDetector.swipeDetected()) {
                if(swipeDetector.getAction() == Action.LR) {
                    viewHolder.chatButton.setVisibility(View.VISIBLE);
                    //outterLayout
                    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    relativeParams.addRule(RelativeLayout.RIGHT_OF, viewHolder.chatButton.getId());
                    relativeParams.setMargins(20, 0, 0, 0);
                    viewHolder.outterLayout.setLayoutParams(relativeParams);
                    viewHolder.tvDeliver.setVisibility(View.GONE);
                    return;
                } else {
                    viewHolder.chatButton.setVisibility(View.GONE);
                    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    relativeParams.setMargins(0, 0, 0, 0);
                    viewHolder.outterLayout.setLayoutParams(relativeParams);
                    return;
                }
            } 
                Toast.makeText(context, "Click", 2000).show();          
            }
        });