ビュー フリッパーを使用して、画像を切り替えるために左右にスワイプする機能を実装したスライダーのような画像に束を表示しています。
ScrollView 内に ViewFlipper を追加することを考えましたが、それを使用すると、ここでスワイプ機能を使用できなくなり、コードが表示されます
XML レイアウト
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mover"
android:fillViewport="true">
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ViewFlipper>
主な活動
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
ScrollView moverHandle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int gallery_grid_Images[]={R.drawable.itema,R.drawable.itemb,
R.drawable.itemc,R.drawable.itemd,
R.drawable.iteme,R.drawable.itemf,
R.drawable.itemg,R.drawable.itemh,
R.drawable.itemi,R.drawable.itemj,
};
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
for(int i=0;i<gallery_grid_Images.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
setFlipperImage(gallery_grid_Images[i]);
}
moverHandle = (ScrollView) findViewById(R.id.mover);
moverHandle.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
viewFlipper.showPrevious();
}
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
viewFlipper.showNext();
}
});
}
private void setFlipperImage(int res) {
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(res);
viewFlipper.addView(image);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
ついにここに私のタッチリスナーが登場
public class OnSwipeTouchListener implements OnTouchListener {
View view;
MotionEvent motionEvent;
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
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() {
}
}