1

我有两个相对布局不同大小的画廊。后面的画廊比上面的画廊小,所以如果两个画廊的公共部分被触摸,我希望后面的画廊对触摸事件做出反应,否则上面的画廊应该做出反应。目前只有上面的画廊有反应。你知道如何实现这一点吗?

4

1 回答 1

0

我没有尝试过,但这可能会对您有所帮助:

覆盖onTouchEvent你的gallary1,然后复制MotionEvent并将它传递给另一个画廊,让它命名为gallary1

@Override
    public boolean onTouchEvent(MotionEvent event) {

        MotionEvent event2=MotionEvent.obtain(event);
        gallery2.onTouchEvent(event2);

        return super.onTouchEvent(event);
    }

完整代码(其工作):

public class MyGallary extends Gallery {

    public MyGallary(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
   }

public MyGallary(Context context, AttributeSet attrs) {
    super(context, attrs);
   }

public MyGallary(Context context) {
    super(context);
  }

    private MyTouchListener myTouchListener;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (myTouchListener != null) {
            myTouchListener.onTouch(event);
        }
        return super.onTouchEvent(event);
    }

    public MyTouchListener getMyTouchListener() {
        return myTouchListener;
    }

    public void setMyTouchListener(MyTouchListener myTouchListener) {
        this.myTouchListener = myTouchListener;
    }

}

public interface MyTouchListener {

    public void onTouch(MotionEvent event);

}

public class MainActivity extends Activity {

    MyGallary gallary1;
    Gallery gallary2;
    MyTouchListener listener1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gallary1 = (MyGallary) findViewById(R.id.myGallary1);
        listener1 = new MyTouchListener() {

            @Override
            public void onTouch(MotionEvent event) {
                MotionEvent event2 = MotionEvent.obtain(event);
                gallary2.onTouchEvent(event2);

            }
        };
        gallary1.setMyTouchListener(listener1);

        gallary2 = (Gallary) findViewById(R.id.Gallary2);

        int[] array = new int[] { R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher };

        ImageAdapter adapter1 = new ImageAdapter(array);
        gallary1.setAdapter(adapter1);

        ImageAdapter adapter2=new ImageAdapter(array);
        gallary2.setAdapter(adapter2);

    }

    class ImageAdapter extends BaseAdapter {

        int[] array;

        public ImageAdapter(int[] array) {
            this.array = array;
        }

        @Override
        public int getCount() {
            return array.length;
        }

        @Override
        public Object getItem(int position) {

            return array[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                ImageView imageView = new ImageView(MainActivity.this);
                Gallery.LayoutParams params = new Gallery.LayoutParams(
                        Gallery.LayoutParams.MATCH_PARENT,
                        Gallery.LayoutParams.MATCH_PARENT);

                imageView.setLayoutParams(params);

                convertView = imageView;

            }

            ImageView imageView = (ImageView) convertView;
            imageView.setImageResource((Integer) getItem(position));

            return convertView;
        }
    }
于 2012-10-29T21:59:37.523 回答