Android 手势检测与滚动视图问题
我正在尝试使用包含滚动视图的ViewFlipper创建布局。其思想是检测水平滑动以移动到上一个/下一个滚动视图。此外,ScrollView还包含另一个ViewFlipper,其中包含带有垂直滑动检测器的ImageView,用于转到上一个/下一个ImageView。当我用线性布局替换ScrollView时,两个手势检测器都能正常工作,但在ScrollView中,没有一个能正常工作(手势侦听器甚至不是触发器)。为什么使用ScrollView会禁用我的手势检测器?我怎样才能让它工作 活动Android 手势检测与滚动视图问题,android,scrollview,gesture,Android,Scrollview,Gesture,我正在尝试使用包含滚动视图的ViewFlipper创建布局。其思想是检测水平滑动以移动到上一个/下一个滚动视图。此外,ScrollView还包含另一个ViewFlipper,其中包含带有垂直滑动检测器的ImageView,用于转到上一个/下一个ImageView。当我用线性布局替换ScrollView时,两个手势检测器都能正常工作,但在ScrollView中,没有一个能正常工作(手势侦听器甚至不是触发器)。为什么使用ScrollView会禁用我的手势检测器?我怎样才能让它工作 活动 public
public类ProduitHome扩展活动{
私人资源;
漂浮密度;
私有int位置,父_id;;
私人国际数字产品;
私人产品;
私有图像下载器;
私有ViewFlipper产品\u ViewFlipper;
私有滚动查看当前产品布局;
动画下一个产品,下一个产品,上一个产品,上一个产品;
私人手势检测器厨房手势检测器;
private View.OnTouchListener galleryGestureListener;
私人手势检测器产品手势检测器;
private View.OnTouchListener productGestureListener;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.produit_home);
num_products=GlobalData.map_list_produits.get(parent_id).size();
product_viewflipper=(viewflipper)findViewById(R.id.product_viewflipper);
LayoutInflater充气机=getLayoutInflater();
//将num_products视图添加到viewflipper
for(int i=0;我从路径中滑动了\u MAX\u)
返回false;
ViewFlipper gallery=(ViewFlipper)当前产品布局。findViewById(R.id.product\u gallery);
如果(e1.getY()-e2.getY()>滑动最小距离和&Math.abs(速度y)>滑动阈值速度){
gallery.showNext();
}else if(e2.getY()-e1.getY()>滑动最小距离和&Math.abs(velocityY)>滑动阈值速度){
gallery.showPrevious();
}
((放射组)当前_产品_布局.findviewbyd(R.id.gallery_nav)).check(gallery.getDisplayedChild());
}捕获(例外e){
}
返回false;
}
}
类ProductGestureListener扩展了SimpleOnGestureListener{
@凌驾
公共布尔onDown(运动事件e){
返回true;
}
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
最终整数滑动距离=120;
最终整数滑动路径=250;
最终整数滑动阈值速度=200;
if(!Utils.IsOnline(ProduitHome.this)){
SRPDialogs.show(ProduitHome.this,SRPDialogs.NOT_CONNECTED);
}
否则{
试一试{
if(Math.abs(e1.getY()-e2.getY())>swip\u MAX\u OFF\u路径)
返回false;
if(e2.getX()-e1.getX()>滑动最小距离和&Math.abs(速度x)>滑动阈值速度){
//展示下一个产品
}else if(e1.getX()-e2.getX()>滑动最小距离和&Math.abs(velocityX)>滑动阈值速度){
//展示以前的产品
}
}捕获(例外e){
}
}
返回false;
}
}
public void setProductData(){
produit=GlobalData.map\u produits.get(GlobalData.map\u list\u produits.get(parent\u id).get(position).id);
TextView name=(TextView)当前产品布局。findViewById(R.id.name);
name.setText(produit.libelle);
//负载通道
int nPics=produit.list_url_pic.size();
如果(nPics>0){
ViewFlipper gallery=(ViewFlipper)当前产品布局。findViewById(R.id.product\u gallery);
gallery.removeAllViews();
mImageLoader=新图像下载器(res,
((BitmapDrawable)res.getDrawable(R.drawable.default_row_pic)).getBitmap(),1);
final ViewFlipper.LayoutParams params\u vf=新ViewFlipper.LayoutParams(ViewFlipper.LayoutParams.FILL\u父级,ViewFlipper.LayoutParams.FILL\u父级);
for(字符串url:produit.list\u url\u pic){
//将图像添加到viewflipper
ImageView ImageView\u p=新的ImageView(本);
imageView\u p.setLayoutParams(params\u vf);
imageView\u p.setScaleType(imageView.ScaleType.CENTER\u裁剪);
imageView_p.setTag(url);
imageView\u p.setImageResource(R.drawable.default\u row\u pic);
下载(url,imageView\p);
gallery.addView(图像视图);
}
//滑动检测器以切换多媒体资料中的图片
galleryGestureDetector=新的GestureDetector(新的垂直SwipeListener());
galleryGestureListener=新建视图。OnTouchListener()
{
公共布尔onTouch(视图v,运动事件)
{
if(galleryGestureDetector.onTouchEvent(事件))
{
返回true;
}
否则{
返回false;
}
}
};
}
}
}
父布局
ViewFlipper的子级布局
我的答案与上一个答案相同,只是我要更明确一些 改变
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@color/grey_bg">
这个代码来自这里的最上面的答案:(sog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/product_home" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:background="@color/grey_bg">
<!-- more stuff -->
<ViewFlipper android:id="@+id/product_viewflipper"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_below="@id/header_logo" />
<!-- more stuff -->
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@color/grey_bg">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:gravity="center_horizontal">
<!-- more stuff -->
<RelativeLayout android:layout_below="@id/bg_content_top"
android:layout_above="@id/bg_content_bottom"
android:layout_width="300dp" android:layout_height="fill_parent"
android:background="@drawable/bg_content"
android:paddingRight="3dp" android:paddingLeft="3dp"
android:layout_centerHorizontal="true">
<!-- more stuff -->
<RelativeLayout android:id="@+id/content"
android:layout_below="@id/title_container"
android:layout_above="@id/bg_content_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="7dp" android:paddingRight="7dp"
android:paddingTop="10dp" android:paddingBottom="10dp">
<ViewFlipper android:id="@+id/product_gallery"
android:clickable="true" android:focusable="false"
android:layout_width="100dp" android:layout_height="150dp"
android:layout_marginRight="10dp"
android:layout_below="@id/title_container"
android:layout_toRightOf="@id/gallery_nav" />
<!-- more stuff -->
</RelativeLayout>
</RelativeLayout>
<!-- more stuff -->
</LinearLayout>
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@color/grey_bg">
<your.packagename.CustomScrollView ... etc>
public class CustomScrollView extends ScrollView {
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Call super first because it does some hidden motion event handling
boolean result = super.onInterceptTouchEvent(ev);
//Now see if we are scrolling vertically with the custom gesture detector
if (gestureDetector.onTouchEvent(ev)) {
return result;
}
//If not scrolling vertically (more y than x), don't hijack the event.
else {
return false;
}
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
try {
if (Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
} else {
return false;
}
} catch (Exception e) {
// nothing
}
return false;
}
}
if (Math.abs(distanceY) > Math.abs(distanceX)) {
if (Math.abs(distanceY) < Math.abs(distanceX)) {
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return productGestureDetector.onTouchEvent(ev);
}
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return productGestureDetector.onTouchEvent(event);
}
});