简体   繁体   中英

Android: parent Viewgroup ontouch being ignored in favour of child view onclick

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.

覆盖自定义ViewGroup中的onInterceptTouchEvent并在那里检测手势

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM