当前位置:编程学习 > wap >>

Gallery自定义View点击事件不灵敏的问题

使用Gallery展示图片,Adapter代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
View view = View.inflate(context, R.layout.three_image, null);
ImageView img_1 = (ImageView) view.findViewById(R.id.img_1);
ImageView img_2 = (ImageView) view.findViewById(R.id.img_2);
ImageView img_3 = (ImageView) view.findViewById(R.id.img_3);
fb.display(img_1, Constant.URL_STUFF + "/" + imgs.get(position*3).pic);
img_1.setAdjustViewBounds(true);
img_1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         clickImageUrl(pos*3, images);
        }
    });
if (images.size() > position*3 + 1) {
fb.display(iv_2, Constant.URL_STUFF + "/" + imgs.get(position*3 + 1).pic);
img_2.setAdjustViewBounds(true);
img_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clickmageUrl(pos*3+1, images);
}
});
}

if (images.size() > position*3 + 2) {
fb.display(iv_3, Constant.URL_STUFF + "/" + imgs.get(position*3 + 2).pic);
img_3.setAdjustViewBounds(true);
img_3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clickImageUrl(pos*3+2, images);
            }
        });
}

view.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return view;
}


three_image布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_1"
        android:layout_width="0dp"
        android:layout_height="100dip" 
        android:layout_weight="1.0"
        />
    <ImageView
        android:id="@+id/img_2"
        android:layout_width="0dp"
        android:layout_height="100dip" 
        android:layout_weight="1.0"
         android:layout_marginLeft="5dip"
        />
    <ImageView
        android:id="@+id/img_3"
        android:layout_width="0dp"
        android:layout_height="100dip" 
        android:layout_weight="1.0"
         android:layout_marginLeft="5dip"
        />

</LinearLayout>


重写Gallery类方法代码:


public class MyGallery extends Gallery {

private float gTouchStartX;  
    private float gTouchStartY;
  
    public MyGallery(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
    }  
  
    public MyGallery(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }
  
    public MyGallery(Context context) {  
        super(context);  
    }  

    @Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        int action = ev.getAction();  
        switch(action){  
        case MotionEvent.ACTION_DOWN:  
            gTouchStartX = ev.getX();  
            gTouchStartY = ev.getY();  
            super.onTouchEvent(ev);  
            break;  
        case MotionEvent.ACTION_MOVE:  
            final float touchDistancesX = Math.abs(ev.getX()-gTouchStartX);  
            final float touchDistancesY = Math.abs(ev.getY()-gTouchStartY);  
            if(touchDistancesY *2 >= touchDistancesX){  
                return false;  
            }else{  
                return true;  
            }  
        case MotionEvent.ACTION_CANCEL:  
            break;  
        case MotionEvent.ACTION_UP:  
            break;  
        }  
        return false;  
    }  
      
    @Override  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        if(e2.getX() > e1.getX()){  
            onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);  
        }else{  
            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);  
        }  
        return false;  
    }  
}


现在的问题是:浏览这个Gallery点击里面的小图之后没反应,需要双击才有反应。请问这是怎么回事。 --------------------编程问答-------------------- 应该是点第一次是获得焦点,点第二次才有反应。
这个可能和onTouch事件冲突了。onTouch先获得了焦点

img_3.requestFocuse  试试。
--------------------编程问答-------------------- 求助,碰到和你同样的问题。。。 --------------------编程问答-------------------- 我确定是跟onFling这个方法有关系,如果去了这个方法就没有问题,就是gallery滑动太快 --------------------编程问答-------------------- 亲,咱把那个case MotionEvent.ACTION_DOWN里面的super.onTouchEvent(ev);  去了试下,都整一天了 --------------------编程问答--------------------
引用 4 楼 u010411826 的回复:
亲,咱把那个case MotionEvent.ACTION_DOWN里面的super.onTouchEvent(ev);  去了试下,都整一天了
怎么解决这个问题
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,