2013-04-22 52 views
2

我正在考慮創建一個Android應用程序作爲更廣泛的藝術項目的一部分,並具有Android開發沒有經驗需要檢查,如果事情是可能/可行的。左/右和上/下刷卡導航

它需要一個非常簡單的接口,數個(15 - 20)之間導航包含圖像或文本頁面。導航需要左/右和向上/向下滑動。

我已經(從以往關於這一主題的問題通過複製/粘貼代碼)在一個非常簡單的應用程序使用PagerAdapter/OnClickListener實現概念的證據,但這不僅左/右切換。

是否有使用此頁面之間向上/向下導航的方法嗎?任何幫助/建議將不勝感激,

import android.content.Context; 
import android.content.Intent; 
import android.os.Parcelable; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MyPagerAdapter extends PagerAdapter { 


    public int getCount() { 
     return 5; 
    } 


    @Override 
    public Object instantiateItem(View container, int position) { 
     Context context = container.getContext(); 
     LinearLayout layout = new LinearLayout(context); 

     TextView textItem = new TextView(context); 
     Button buttonItem = new Button(context); 
     buttonItem.setText("Aceptar"); 
     buttonItem.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent("com.phone"); 

      } 
     }); 


     switch (position) { 
     case 0: 
      textItem.setText("Photo 1 - begining"); 
      break; 
     case 1: 
      textItem.setText("Photo 2"); 
      break; 
     case 2: 
      textItem.setText("Photo 3"); 
      break; 


case 3: 
     textItem.setText("Photo 4"); 
     break; 
    case 4: 
     textItem.setText("Photo 5 - end"); 
     break; 
    } 
    layout.addView(textItem); 
    ((ViewPager) container).addView(layout, 0); 
    return layout; 
} 
@Override 
public void destroyItem(View arg0, int arg1, Object arg2) { 
    ((ViewPager) arg0).removeView((View) arg2); 
} 
@Override 
public boolean isViewFromObject(View arg0, Object arg1) { 
    return arg0 == ((View) arg1); 
} 
@Override 
public Parcelable saveState() { 
    return null; 
} 
} 

下面是新的代碼給錯誤

的方法onTouch(查看,MotionEvent)是未定義的類型對象

import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 
     super.onTouch(view, motionEvent); 
     return gestureDetector.onTouchEvent(motionEvent); 
    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 

    public void onSwipeRight() { 
    } 

    public void onSwipeLeft() { 
    } 

    public void onSwipeTop() { 
    } 

    public void onSwipeBottom() { 
    } 
} 
+0

請選中該http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures和http://stackoverflow.com/questions/12970249/Android的刷卡左,或右到幻燈片視圖 – 2013-04-22 11:23:45

+0

請選中該http://stackoverflow.com/questions/937313/android-basic-gesture-detection和 http://stackoverflow.com/問題/ 12970249/android-swipe-left-right-to-slide-views – 2013-04-22 11:33:09

+0

@ pragnesh-soni謝謝你,關注http://stackoverflow.com/questions/4139288/android-how-to-handle-right-左手輕掃手勢,我留下了錯誤「方法onTouch(視圖,MotionEvent)未定義的類型對象」,在super.onTouch(視圖,motionEvent)行; ---我無法刪除這個, – dantibb 2013-04-22 16:17:47

回答

4

出於某種原因,我認爲你有一個運行時錯誤,但不是,在你嘗試編譯之前,你很明顯在Eclipse中看到一條錯誤消息。即使編譯了代碼,您也必須將您的手勢檢測器應用於佈局容器,以便它能夠執行任何操作。

您在您的OnSwipeTouchListener類中稱爲super.onTouch(),它是Object的一個子類。 Object沒有onTouch()方法,這就是編譯器所抱怨的。 onTouch()方法需要在您的活動中進行,而不是在您的自定義手勢監聽器中。

我要去猜你使用,無論例如,你得到了所有的剪切和粘貼有點混亂。

這裏的檢測方向甩,演示瞭如何應用自定義手勢聽者一些非常簡單的示例代碼:

http://pcfandroid.wordpress.com/2011/07/17/swipe-with-android-android-tutorial/

這篇文章是死的容易執行,很清晰,很簡單的。

+0

謝謝你,是的,這個鏈接讓我更清楚地瞭解手勢檢測,並讓它工作。雖然不是我之後所做的,所以要試用垂直ViewPager https://github.com/JakeWharton/Android-DirectionalViewPager/ - 再次感謝。 – dantibb 2013-04-25 16:46:00

2
public class Test extends Activity{ 

private GestureDetector gesturedetector = null; 

View layout; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.test); 

layout = (LinearLayout)findViewById(R.id.container); 

gesturedetector = new GestureDetector(new MyGestureListener()); 

layout.setOnTouchListener(new OnTouchListener() { 

@Override 

public boolean onTouch(View v, MotionEvent event) { 

gesturedetector.onTouchEvent(event); 

return true; 

} 

}); 

} 

public boolean dispatchTouchEvent(MotionEvent ev){ 

super.dispatchTouchEvent(ev); 

return gesturedetector.onTouchEvent(ev); 

} 

class MyGestureListener extends GestureDetector.SimpleOnGestureListener{ 

private static final int SWIPE_MIN_DISTANCE = 150; 

private static final int SWIPE_MAX_OFF_PATH = 100; 

private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

@Override 

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 

float velocityY) { 

float dX = e2.getX()-e1.getX(); 

float dY = e1.getY()-e2.getY(); 

if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && 

Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && 

Math.abs(dX)>=SWIPE_MIN_DISTANCE) { 

if (dX>0) { 

Toast.makeText(getApplicationContext(), 「Right Swipe」, Toast.LENGTH_SHORT).show(); 

} else { 

Toast.makeText(getApplicationContext(), 「Left Swipe」, Toast.LENGTH_SHORT).show(); 

} 

return true; 

} else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH && 

Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && 

Math.abs(dY)>=SWIPE_MIN_DISTANCE) { 

if (dY>0) { 

Toast.makeText(getApplicationContext(), 「Up Swipe」, Toast.LENGTH_SHORT).show(); 

} else { 

Toast.makeText(getApplicationContext(), 「Down Swipe」, Toast.LENGTH_SHORT).show(); 

} 

return true; 

} 

return false; 

} 

} 

}