ViewPager destroyItem()[位置1]无法正常工作

时间:2014-12-20 13:46:38

标签: android android-viewpager

首先我要做的是,一个包含3页ViewPager的简单视频程序,第一页有视频列表,第二页只有一个VideoView,第三个是另一个列表视图。程序工作得很好但是当我尝试在页面之间滑动时,视频不会停止播放(默认情况下,应用程序在第1页开始,即视频视图)。我已经尝试添加onClickListener来启动视频,这解决了程序启动时自动启动的问题。但是,当我改变页面时,它仍在继续播放。

这是页面启动部分:

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view;
        switch (position) {
            case 0: {
                // Inflate a new layout from our resources
                view = getActivity().getLayoutInflater().inflate(R.layout.activity_listed_videos,
                        container, false);
                // Add the newly created View to the ViewPager
                container.addView(view);
                list = (ListView) view.findViewById(R.id.list);
                GetVideoPosts getVideoPosts = new GetVideoPosts();
                getVideoPosts.execute();
                updateList();
                list.setAdapter(new CustomList(getActivity(), mVideoUrls, mVideoTitles));
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        try {
                            JSONObject job1 = mVideoData.getJSONObject("data");
                            JSONArray jsonPosts = job1.getJSONArray("items");
                            JSONObject post = jsonPosts.getJSONObject(position);
                            JSONObject url = post.getJSONObject("player");
                            String title = url.getString("default");
                            title = Html.fromHtml(title).toString();
                            Intent intent = new Intent(getActivity(), VideoViewActivity.class);
                            intent.putExtra("url", title);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                // Return the View
                return view;
            }
            case 1: {
                ((ViewPager)container).setCurrentItem(1);
                view = getActivity().getLayoutInflater().inflate(R.layout.activity_video_view,
                        container, false);
                container.addView(view);
                GetVideoPosts getVideoPosts = new GetVideoPosts();
                getVideoPosts.execute();
                updateList();
                final VideoView vv = (VideoView) view.findViewById(R.id.videoView);
                MediaController mediacontroller = new MediaController(
                        getActivity());
                mediacontroller.setAnchorView(vv);
                String videoUrl ="http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4";
                Uri video = Uri.parse(videoUrl);
                vv.setMediaController(mediacontroller);
                vv.setVideoURI(video);
                // Add the newly created View to the ViewPager

                container.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showLoadingImageDialog();
                        vv.requestFocus();
                        vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            public void onPrepared(MediaPlayer mp) {
                                pDialog.dismiss();
                                vv.start();
                            }
                        });
                    }
                });

                return view;
            }
            case 2:
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                        container, false);
                // Add the newly created View to the ViewPager
                container.addView(view);

                // Retrieve a TextView from the inflated View, and update it's text
                TextView title = (TextView) view.findViewById(R.id.item_title);
                title.setText(String.valueOf(position + 1));

                // Return the View
                return view;
        }
        return null;
    }

和输出:

12-20 3435-3435/SlidingTabsBasicFragment﹕ destroyItem() [position: 0]
12-20 3435-3435/SlidingTabsBasicFragment﹕ destroyItem() [position: 2]
12-20 3435-3435/SlidingTabsBasicFragment﹕ destroyItem() [position: 0]
12-20 3435-3435/SlidingTabsBasicFragment﹕ destroyItem() [position: 2]

我正在跳过位置1,但没有显示destroyItem函数日志。

注意:我们非常感谢您展示视频的任何其他想法。您可以将此应用视为Vine应用,以便更好地理解。

1 个答案:

答案 0 :(得分:1)

您可以在此处设置mViewPager.setOffscreenPageLimit(0);,以便在您离开该网页时,您的网页也会销毁,从而也会破坏您的VideoView。简单