タッチ リスナーをページャーに追加してから、水平方向の動きと垂直方向の動きを区別する必要があります。私はこれがあなたが探しているものだと思います
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#8888">
</android.support.v4.view.ViewPager>
</LinearLayout>
シンプルなページャー アダプター
PagerAdapter.java
package com.virtoos.tests;
public class PagerAdapter extends android.support.v4.view.PagerAdapter {
private static final int[] WELCOME_IMAGES = new int[] {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
Context mContext;
LayoutInflater mLayoutInflater;
public PagerAdapter(Context context){
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public Object instantiateItem(ViewGroup parent, int position){
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(WELCOME_IMAGES[position]);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
parent.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup parent, int position, Object view){
parent.removeView((View) view);
}
@Override
public int getCount(){
return WELCOME_IMAGES.length;
}
@Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
}
そして、タッチハンドラーを持つ最後のアクティビティクラス
MainActivity.java
package com.virtoos.tests;
public class MainActivity extends FragmentActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(new PagerAdapter(this));
pager.setOnTouchListener(dragTouchListener);
ViewConfiguration vc = ViewConfiguration.get(this);
slop = vc.getScaledTouchSlop();
}
private float downY;
private int slop;
private VelocityTracker velocityTracker;
private boolean isVerticalScroll = false;
private View.OnTouchListener dragTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
downY = event.getY();
velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(event);
} else if(event.getAction()==MotionEvent.ACTION_MOVE){
float deltaY = event.getY() - downY;
float deltaMode = Math.abs(deltaY);
velocityTracker.addMovement(event);
velocityTracker.computeCurrentVelocity(1000);
float velocityX = Math.abs(velocityTracker.getXVelocity());
float velocityY = Math.abs(velocityTracker.getYVelocity());
if (isVerticalScroll || (deltaMode > slop && velocityY > velocityX)) {
//this.getParent().requestDisallowInterceptTouchEvent(true);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) pager.getLayoutParams();
lp.height = (int) (lp.height-deltaY);
pager.setLayoutParams(lp);
isVerticalScroll = true;
}else {
isVerticalScroll = false;
return false;
}
} else if(event.getAction()==MotionEvent.ACTION_UP){
if(!isVerticalScroll) return false;
//float deltaY = event.getRawY() - downY;
//float deltaMode = Math.abs(deltaY);
downY = 0;
isVerticalScroll = false;
//float velocityY = Math.abs(velocityTracker.getYVelocity());
velocityTracker.recycle();
velocityTracker = null;
/*if((velocityY>100 && deltaMode > slop) ) {
if(*//*pager above half of the screen*//*){
animateOpen();
}else {
animateClose();
}
mGestureDetector.onTouchEvent(event);
return true;
}*/
}
//return mGestureDetector.onTouchEvent(event);
return true;
}
};
}
ジェスチャを処理するために GestureDetector を追加することで、この例を強化できます。これにより、onFling
より没入型の制御が可能になります。