我正在使用视图翻转器在图像上显示一堆图像,比如一个滑块,我在其中实现了左右滑动来切换图像的功能,现在我的一些图像大于屏幕尺寸(就高度而言)被隐藏了。
我想在 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() {
}
}