1

I have two galleries with different size on relative layout above each other. The gallery behind is smaller than gallery above so I want gallery behind react from touch events if common part of two galleries is touched otherwise the gallery above should react. Currently only gallery above is reacting. Do you have any idea how to realize this ?

Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • 1
    `I want gallery behind react from touch events if common part of two galleries is touched`. Then why not make the small one to be above the larger one? – iTurki Oct 29 '12 at 22:00

1 Answers1

0

I didn't try it, but this may help you:

Override onTouchEvent in your gallary1, then copy the MotionEvent and pass it to the other Gallary lets name it as gallary1

@Override
    public boolean onTouchEvent(MotionEvent event) {

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

        return super.onTouchEvent(event);
    }

Full Code (its working):

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;
        }
    }
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77