Listview Swipe inside viewflipper

后端 未结 3 507
旧巷少年郎
旧巷少年郎 2021-01-03 09:39

Im trying to swipe left and right on a listview and get the viewflipper to swtich. Just like the remeberthemilk app and the default news and weather app on the nexus one (Sw

相关标签:
3条回答
  • 2021-01-03 10:20

    Here is the way i use. it is prety simple.

    When you assign onTouchListener to ViewFlipper, create reference for that listener first.

    public class ViewFlipperTouchListener implements OnTouchListener {
       ....
       ......
    }
    
    
    ViewFlipperTouchListener listener = new ViewFlipperTouchListener ();
    
    mViewFlipper.setOnTouchListener(listener);
    
    
    
    //now you have reference to listener. use it to send on touch event.
    
    
    mListView.setOnTouchListener(new OnTouchListener(){
    
      @Override
      public boolean onTouch(View v, MotionEvent event){
         listener.onTouch(v, event);
     return false;
      }
    });
    

    i simply delegated MotionEvent to viewflipper's onTouchListener by using reference "listener". And return false to imply that event is not consumed on ListView. This will help you to scroll down/up on listview and delegates everything (scroll down/up and swipe left/right ...etc to listener.)..

    Believe it. it works nice :D

    0 讨论(0)
  • 2021-01-03 10:33

    Well one way I found is instead of relying on, onItemClick, I implemented

    @Override
     public boolean onSingleTapConfirmed(MotionEvent e) {
    
         // TODO Auto-generated method stub
         Log.e("Item Click", "Item Click");
         return super.onSingleTapConfirmed(e);
    
     }
    

    Inside SimpleOnGestureListener and it seems as if it works

    0 讨论(0)
  • 2021-01-03 10:35

    I think you cant do alot about it, other than playing with your constants in your code, so that its better decidable if the user clicked or wants to swipe. For example I did it the following way in my code (i used touch event but the principle should be similar):

    @Override
    public boolean onTouch(View view, MotionEvent event) {
    
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            {
                downXValue = event.getX();
                downTime = event.getEventTime();
                break;
            }
    
        case MotionEvent.ACTION_UP:
            {
                float currentX = event.getX();
                long currentTime = event.getEventTime();
                float difference = Math.abs(downXValue - currentX);
                long time = currentTime - downTime;
    
                Log.i("Touch Event:", "Distance: " + difference + "px Time: " + time + "ms");
    
                if ((downXValue < currentX) && (time > 50) && (difference > 50)) {
                    showPrevious();
                }
    
                if ((downXValue > currentX) && (time > 50) && (difference > 50)) {
                    showNext();
                }
                break;
            }
        }
    }
    

    As you can see i put a Log output there. So i just tested the app and every time i tried to flip or click, the Log told me how long i pressed and how big the difference was. So i adjusted my values in the if condition until it worked in almost all cases

    0 讨论(0)
提交回复
热议问题