手势检测和滚动型问题 [英] Gesture detection and ScrollView issue
问题描述
我试图创建包含ScrollViews一个ViewFlipper布局。我们的想法是,以检测水平刷卡移动到previous /下滚动型。此外,滚动型包含含有的ImageView与垂直刷卡器的另一个ViewFlipper去previous /下一个ImageView的。当我更换滚动型用的LinearLayout兼备的姿态检测器正常工作,但与滚动型,无工作(手势听众甚至不触发)。为什么使用滚动型禁用我的手势探测器?我怎样才能使它发挥作用?
感谢
活动
公共类ProduitHome延伸活动{
私人资源资源;
浮密度;
私人诠释的位置,PARENT_ID ;;
私人诠释num_products;
私人导航产品导航产品;
私人ImageDownloader mImageLoader;
私人ViewFlipper product_viewflipper;
私人滚动型current_product_layout;
动画next_product_out,next_product_in,previous_product_in,previous_product_out;
私人GestureDetector galleryGestureDetector;
私人View.OnTouchListener galleryGestureListener;
私人GestureDetector productGestureDetector;
私人View.OnTouchListener productGestureListener;
@覆盖
公共无效的onCreate(包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; I< num_products;我++){
滚动型product_detail =(滚动型)inflater.inflate(R.layout.produit_detail,NULL);
product_viewflipper.addView(product_detail);
}
//设置数据并显示当前产品
current_product_layout =(滚动型)product_viewflipper.getChildAt(位置);
product_viewflipper.setDisplayedChild(位置);
setProductData();
//设置刷卡监听器开关产品
productGestureDetector =新GestureDetector(新ProductGestureListener());
productGestureListener =新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
如果(productGestureDetector.onTouchEvent(事件))
{
返回true;
}
其他{
返回false;
}
}
};
product_viewflipper.setOnTouchListener(productGestureListener);
//设置开关产品动画
next_product_out = AnimationUtils.loadAnimation(这一点,R.anim.next_product_out);
next_product_in = AnimationUtils.loadAnimation(这一点,R.anim.next_product_in);
previous_product_in = AnimationUtils.loadAnimation(这一点,R.anim previous_product_in。);
previous_product_out = AnimationUtils.loadAnimation(这一点,R.anim previous_product_out。);
}
类VerticalSwipeListener扩展SimpleOnGestureListener {
@覆盖
公共布尔onDown(MotionEvent E){
返回true;
}
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
最终诠释SWIPE_MIN_DISTANCE = 80;
最终诠释SWIPE_MAX_OFF_PATH = 250;
最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
尝试 {
如果(Math.abs(e1.getX() - e2.getX())> SWIPE_MAX_OFF_PATH)
返回false;
ViewFlipper画廊=(ViewFlipper)current_product_layout.findViewById(R.id.product_gallery);
如果(e1.getY() - e2.getY()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityY)> SWIPE_THRESHOLD_VELOCITY){
gallery.showNext();
}否则,如果(e2.getY() - e1.getY()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityY)> SWIPE_THRESHOLD_VELOCITY){
gallery.show previous();
}
((RadioGroup)current_product_layout.findViewById(R.id.gallery_nav)).check(gallery.getDisplayedChild());
}赶上(例外五){
}
返回false;
}
}
类ProductGestureListener扩展SimpleOnGestureListener {
@覆盖
公共布尔onDown(MotionEvent E){
返回true;
}
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
最终诠释SWIPE_MIN_DISTANCE = 120;
最终诠释SWIPE_MAX_OFF_PATH = 250;
最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
如果(!Utils.IsOnline(ProduitHome.this)){
SRPDialogs.show(ProduitHome.this,SRPDialogs.NOT_CONNECTED);
}
其他{
尝试 {
如果(Math.abs(e1.getY() - e2.getY())> SWIPE_MAX_OFF_PATH)
返回false;
如果(e2.getX() - e1.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
//显示下一个产品
}否则,如果(e1.getX() - e2.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
//显示previous产品
}
}赶上(例外五){
}
}
返回false;
}
}
公共无效setProductData(){
导航产品= GlobalData.map_produits.get(GlobalData.map_list_produits.get(PARENT_ID)获得(位置).ID);
TextView的名称=(TextView中)current_product_layout.findViewById(R.id.name);
name.setText(produit.libelle);
//装载画廊
INT nPics = produit.list_url_pic.size();
如果(nPics大于0){
ViewFlipper画廊=(ViewFlipper)current_product_layout.findViewById(R.id.product_gallery);
gallery.removeAllViews();
mImageLoader =新ImageDownloader(RES,
((BitmapDrawable)res.getDrawable(R.drawable.default_row_pic))getBitmap(),1);
最后ViewFlipper.LayoutParams params_vf =新ViewFlipper.LayoutParams(ViewFlipper.LayoutParams.FILL_PARENT,ViewFlipper.LayoutParams.FILL_PARENT);
对于(字符串URL:produit.list_url_pic){
//添加图像viewflipper
ImageView的imageView_p =新ImageView的(这一点);
imageView_p.setLayoutParams(params_vf);
imageView_p.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView_p.setTag(URL);
imageView_p.setImageResource(R.drawable.default_row_pic);
mImageLoader.download(URL,imageView_p);
gallery.addView(imageView_p);
}
//刷卡器在多媒体切换画面
galleryGestureDetector =新GestureDetector(新VerticalSwipeListener());
galleryGestureListener =新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
如果(galleryGestureDetector.onTouchEvent(事件))
{
返回true;
}
其他{
返回false;
}
}
};
}
}
}
父布局
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / product_home机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT机器人:方向=垂直
机器人:背景=@色/ grey_bg>
<! - 更多的东西 - >
< ViewFlipper机器人:ID =@ + ID / product_viewflipper
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:layout_below =@ ID / header_logo/>
<! - 更多的东西 - >
< / RelativeLayout的>
ViewFlipper的儿童布局
< XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:背景=@色/ grey_bg>
< LinearLayout中的android:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT机器人:方向=垂直
机器人:重力=center_horizontal>
<! - 更多的东西 - >
< RelativeLayout的机器人:layout_below =@ ID / bg_content_top
机器人:layout_above =@ ID / bg_content_bottom
机器人:layout_width =300dp机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/ bg_content
机器人:paddingRight =3DP机器人:以下属性来=3DP
机器人:layout_centerHorizontal =真正的>
<! - 更多的东西 - >
< RelativeLayout的机器人:ID =@ + ID /内容
机器人:layout_below =@ ID / title_container
机器人:layout_above =@ ID / bg_content_bottom
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:以下属性来=7DP机器人:paddingRight =7DP
机器人:paddingTop =10dp机器人:paddingBottom会=10dp>
< ViewFlipper机器人:ID =@ + ID / product_gallery
机器人:可点击=真正的机器人:可聚焦=假
机器人:layout_width =100dp机器人:layout_height =150dp
机器人:layout_marginRight =10dp
机器人:layout_below =@ ID / title_container
机器人:layout_toRightOf =@ ID / gallery_nav/>
<! - 更多的东西 - >
< / RelativeLayout的>
< / RelativeLayout的>
<! - 更多的东西 - >
< / LinearLayout中>
< /滚动型>
我不得不添加
@覆盖
公共布尔dispatchTouchEvent(MotionEvent EV){
super.dispatchTouchEvent(EV);
返回productGestureDetector.onTouchEvent(EV);
}
在我的活动。
I'm trying to create a layout with a ViewFlipper containing ScrollViews. The idea is to detect horizontal swipes to move to previous/next ScrollView. Moreover, the ScrollView contains another ViewFlipper containing ImageView with a vertical swipe detector to go to previous/next ImageView. When I replace the ScrollView by a LinearLayout both gesture detectors work properly, but with the ScrollView, none work (the gesture listeners are not even triggers). Why does using a ScrollView disable my gesture detectors? How can I make it work?
Thanks
Activity
public class ProduitHome extends Activity{
private Resources res;
float density;
private int position, parent_id;;
private int num_products;
private Produit produit;
private ImageDownloader mImageLoader;
private ViewFlipper product_viewflipper;
private ScrollView current_product_layout;
Animation next_product_out, next_product_in, previous_product_in, previous_product_out;
private GestureDetector galleryGestureDetector;
private View.OnTouchListener galleryGestureListener;
private GestureDetector productGestureDetector;
private View.OnTouchListener productGestureListener;
@Override
public void onCreate(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 inflater = getLayoutInflater();
// Add num_products view to the viewflipper
for(int i=0; i<num_products; i++){
ScrollView product_detail = (ScrollView) inflater.inflate(R.layout.produit_detail, null);
product_viewflipper.addView(product_detail);
}
// Set data and show current product
current_product_layout = (ScrollView) product_viewflipper.getChildAt(position);
product_viewflipper.setDisplayedChild(position);
setProductData();
// Set swipe listener to switch product
productGestureDetector = new GestureDetector(new ProductGestureListener());
productGestureListener = new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (productGestureDetector.onTouchEvent(event))
{
return true;
}
else{
return false;
}
}
};
product_viewflipper.setOnTouchListener(productGestureListener);
// Set switch product animation
next_product_out = AnimationUtils.loadAnimation(this, R.anim.next_product_out);
next_product_in = AnimationUtils.loadAnimation(this, R.anim.next_product_in);
previous_product_in = AnimationUtils.loadAnimation(this, R.anim.previous_product_in);
previous_product_out = AnimationUtils.loadAnimation(this, R.anim.previous_product_out);
}
class VerticalSwipeListener extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_MIN_DISTANCE = 80;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
try {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
return false;
ViewFlipper gallery = (ViewFlipper)current_product_layout.findViewById(R.id.product_gallery);
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
gallery.showNext();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
gallery.showPrevious();
}
((RadioGroup)current_product_layout.findViewById(R.id.gallery_nav)).check(gallery.getDisplayedChild());
} catch (Exception e) {
}
return false;
}
}
class ProductGestureListener extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
if(!Utils.IsOnline(ProduitHome.this)){
SRPDialogs.show(ProduitHome.this, SRPDialogs.NOT_CONNECTED);
}
else{
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show next product
} else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show previous product
}
} catch (Exception e) {
}
}
return false;
}
}
public void setProductData(){
produit = GlobalData.map_produits.get(GlobalData.map_list_produits.get(parent_id).get(position).id);
TextView name = (TextView) current_product_layout.findViewById(R.id.name);
name.setText(produit.libelle);
// Load gallery
int nPics = produit.list_url_pic.size();
if(nPics>0){
ViewFlipper gallery = (ViewFlipper) current_product_layout.findViewById(R.id.product_gallery);
gallery.removeAllViews();
mImageLoader = new ImageDownloader(res,
((BitmapDrawable)res.getDrawable(R.drawable.default_row_pic)).getBitmap(), 1);
final ViewFlipper.LayoutParams params_vf = new ViewFlipper.LayoutParams(ViewFlipper.LayoutParams.FILL_PARENT, ViewFlipper.LayoutParams.FILL_PARENT);
for(String url : produit.list_url_pic){
// Add images to viewflipper
ImageView imageView_p = new ImageView(this);
imageView_p.setLayoutParams(params_vf);
imageView_p.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView_p.setTag(url);
imageView_p.setImageResource(R.drawable.default_row_pic);
mImageLoader.download(url, imageView_p);
gallery.addView(imageView_p);
}
// Swipe detector to switch picture in gallery
galleryGestureDetector = new GestureDetector(new VerticalSwipeListener());
galleryGestureListener = new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (galleryGestureDetector.onTouchEvent(event))
{
return true;
}
else{
return false;
}
}
};
}
}
}
Parent layout
<?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>
ViewFlipper's children layout
<?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>
I had to add
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return productGestureDetector.onTouchEvent(ev);
}
in my Activity.
这篇关于手势检测和滚动型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!