Search code examples
javaandroiduniversal-image-loader

Android threading order / ontouch webview


I have a GridView fill with images, when I click on one image, it is displayed in full screen in a PagerActivity and for each images in full screen a piece of html is displayed at the bottom in a webview. On touch some webViews launch a video and some do nothing.

My problem is if I touch a "webView video", the video is displayed in the next layout.

Example : I have 3 images in my Grid. I clicked on the first, this image is displayed in full screen with a webview at the bottom and when I clicked on the webview in order to launch the video on top of the image 1 the video is launched on top of the image 2.

Here a part of my code, ImagePagerActivity :

public class ImagePagerActivity extends BaseActivity {

...

private class ImagePagerAdapter extends PagerAdapter implements OnTouchListener,Handler.Callback {

    ...

    public Object instantiateItem(View view, final int position) {
        final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
        final WebView webView = (WebView) imageLayout.findViewById(R.id.webView1);
        final TextView textView=(TextView) imageLayout.findViewById(R.id.edit_message);
        topLevelLayout = (RelativeLayout) imageLayout.findViewById(R.id.top_layout);
        videoView = (VideoView) imageLayout.findViewById(R.id.videoView);
        topLevelLayout.setBackgroundColor(0x00000000);


        ((ViewPager) view).addView(imageLayout, 0);


        imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {

            @Override
            public void onLoadingStarted() {
                ...
            }

            @Override
            public void onLoadingFailed(FailReason failReason) {
                ...
            }

            @Override
            public void onLoadingComplete(Bitmap loadedImage) {

                // HERE ADD THE WEBVIEW ON THE BOTTOM OF THE IMAGE

            }
        });

        return imageLayout;
    }


    public boolean onTouch(View v, MotionEvent event) {

        if(isWebViewLaunchViedo()){
            PlayVideo(positionVideo);
        }
    }

  }

I use this project for the GridActivity and PagerActivity Universal image loader

I understand the instantiateItem(..) method is called twice, so that's why when I try to play a video the videoView is already affect to the new one. But I don't know How can I fix this issue?


Solution

  • I finally create a new transparent activity and play the video in the new activity.

    Manifest :

    <activity
      android:name="com.ad.AdVideoActivity"
      android:theme="@style/Theme.Transparent"         <------
      android:label="@string/title_activity_ad_video" >
    </activity>
    

    New Ontouch :

    public boolean onTouch(View v, MotionEvent event) {
            if (ADS_VIDEO.contains(v.getId()) && event.getAction() == MotionEvent.ACTION_DOWN) {
                intentVideo = new Intent(getBaseContext(),AdVideoActivity.class);
                startActivity(intentVideo);
            }
    
            return false;
        }