2

I am pretty certain I cant find a 100% appropriate solution for this, but I intend to ask this anyway given that it is giving me a headache trying to figure it out.

For various reasons, I have a Scrollview (A) containing a FrameLayout (B) which has a RelativeLayout (C) containing a Viewgroup (D) which in turn acts as a Viewpager & contains ImageView (E) children.

The goal of this is to create a layout which can scroll vertically (thanks to (A)) and allow for (B) to function as an Image carousel.

The problem is that if I have a simple onclick listener on the Imageview, then with API 9 & 10 devices, the onclick listener overrides the ontouch events I have coded into the Viewgroup. As a result, I cannot swipe the carousel anymore and the onclick event is called every time i touch the carousel.

I was hoping someone would be able to aid me with this issue.

Code for Child Image Item:

final FrameLayout container = new FrameLayout(_context);
        container.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        if(dataList.size() > 0)
        {
            ImageView iv = new ImageView(_context);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            lp.gravity = Gravity.CENTER;
            iv.setLayoutParams(lp);
            iv.setScaleType(ScaleType.FIT_START);
            iv.setAdjustViewBounds(true);
            container.addView(iv, 0);

            String url = dataList.get(position).getImageUrl();
            if(url != null)
            {
                UrlImageViewHelper.setUrlDrawable(iv, url, R.drawable.placeholder);
            }
            iv.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.e("seiji", "v: "+v);
                }
            });

        }
        ((EndlessCarousel) collection).addView(container, 0);
        return container;

Code for Viewgroup:

_galleryCarousel = (EndlessCarousel) convertView.findViewById(getEndlessCarouselID());
        ArrayList<ImagesObject> gallerydata = _currentComponent.getImageData();
        if(_galleryAdapter == null)
        {
            _galleryAdapter = new GalleryComponentAdapter(_context, gallerydata);               
        }
        else
        {
            _galleryAdapter.setArray(gallerydata);
            _galleryAdapter.notifyDataSetChanged();
        }
        _galleryCarousel.setAdapter(_galleryAdapter);

        if(_slideshowDelay != -1)
        {
            slideShowHandler.removeCallbacks(runSlideshow);
            slideShowHandler.postDelayed(runSlideshow, _slideshowDelay);
        }

        _galleryCarousel.setOnPageChangeListener(new OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                _tsi.setIndex(position);
                _carouselCtn = position;
                if(_slideshowDelay != -1)
                {
                    slideShowHandler.removeCallbacks(runSlideshow);
                    slideShowHandler.postDelayed(runSlideshow, _slideshowDelay);
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });

        _tsi = (GalleryIndicator) convertView.findViewById(getEndlessCarouselIndicatorID());
        _tsi.removeAllViews();
        _tsi.setUp(gallerydata.size(), _context);

I thank anyone who could give me some help in advance.

SKato
  • 95
  • 6
  • 15

1 Answers1

1

override onInterceptTouchEvent in your custom ViewGroup and detect gestures there

pskink
  • 23,874
  • 6
  • 66
  • 77
  • Thanks for the swift response. I am doing so and investigating possibility of using ontouch with the Imageview rather than onclick. – SKato Jun 27 '13 at 10:09
  • leave ImageView as it is, detect your gestures in ViewGroup – pskink Jun 27 '13 at 10:24
  • Ok. Code for the Viewgroup is one made by a friend, who already put in some code for the onITE. I've been investigating and the results are rather odd. If the onclick is present, when you swipe, the onIET detects 2 events: ACTION_DOWN followed by ACTION_CANCEL. this explains why no swipe possible. But if i swipe when the autoscroller activates (set to change child every 5 seconds) it registers ACTION_DOWN & ACTION_MOVE...will investigate further... – SKato Jun 27 '13 at 11:26
  • see my answer (skip my comments) for this question http://stackoverflow.com/questions/16979285/how-to-dispatch-touch-events-to-children-after-consuming/16996012#16996012 – pskink Jun 27 '13 at 11:31
  • Thank you very much for the code. unfortunately, even though I have that replacing the original code, I am still getting ACTION_DOWN followed by ACTION_CANCEL most of the time I try to swipe. Only once in a while can I get a response from the Gesture detector... – SKato Jun 27 '13 at 13:11
  • did my code worked? i mean if you swiped over the button did the detector called onFling and onClickView was not fired? – pskink Jun 27 '13 at 13:22
  • Unfortunately, it seems that your code isnt being fired. all I am getting in my log is ACTION_DOWN and ACTION_CANCEL. Sometimes it works, then I get ACTION_DOWN, ACTION_MOVE *n & Your codes toast. – SKato Jun 27 '13 at 13:29
  • copy/paste my code as it is to your Activity and see if it works. it should detect swipe no matter where you started your swipe – pskink Jun 27 '13 at 14:07
  • That is the odd thing. i copy/pasted your code inside mine, but it seems that my code ignores the ViewGroups ontouch if the onInteruptTouch event has an ACTION_CANCEL. This only happens on devices using API 9 or 10. 11+ are fine... – SKato Jun 27 '13 at 14:18
  • what onIntwrceptTouchEvent returns? – pskink Jun 27 '13 at 14:37
  • it returns false. however, i have noticed that the swiping ontouch and your code activate if the onInterceptTouchEvent returns true. this, however, causes the code to ignore the onclick (understandably) – SKato Jun 27 '13 at 15:13
  • this is done on purpose: if onITE returns true no other events propagate to child views, so when you swipe no child Button receives any click events – pskink Jun 27 '13 at 15:26
  • Understood. unfortunately, since I am trying to fix this for API lvl 9-10, I need to look into why it shoves an ACTION_CANCEL event in after the ACTION_DOWN, which causes my code to ignore the ontouchevent and, consequently your code. Thank you again for your insights. I do appreciate the help! – SKato Jun 27 '13 at 15:37
  • are you sure there is a bug in lvl 9 & 10? what is the bug number? – pskink Jun 27 '13 at 16:04
  • I am not entirely sure. i can just tell that the code i have written up, including the code you supplied, works fine on any android app using an API lvl of 11 or higher. I am trying to get the code to work for devices using 2.3.2 and 2.3.3, but I get the problems I mentioned above... – SKato Jun 27 '13 at 16:17
  • ok, this is very strange. but I managed to get it to work! 0.o In the onintercepttouchevent, I added the code requestDisallowintercepttouchevent(true) so that the viewgroups own parent would not interfere with the touch events...I am not sure entirely how it works, but will investigate. tthank you again so much! I really appreciate the help! – SKato Jun 27 '13 at 16:31