ViewPager包含视图和片段

时间:2018-03-25 02:55:47

标签: android android-fragments android-viewpager

我目前正在制作视频播放列表。这是来自ListView的流程,它显示播放列表中的视频列表。单击视频后,它将使用以下布局打开PlaylistActivity:

播放列表布局:

playlist layout

问题:

视频工作正常,因为我可以听到他们正在播放。但是,可能是因为我使用了片段,每次单击上一个或下一个按钮时,视频都会在ViewPager的最后打开的页面上播放。

在自定义PagerAdapter上,我使用自定义播放器和空LinearLayout容器对布局进行了充气,​​我将在其中添加片段。

PlaylistPagerAdapter.java

public class PlaylistPagerAdapter extends PagerAdapter {

    List<Playlist> videos;

    @Override
    public int getCount() {
        return videos != null ? videos.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View pagerView = inflater.inflate(R.layout.view_video_player, container, false);
        pagerView.setTag(videos.get(position).getId());
        container.addView(pagerView);
        return pagerView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    public void setVideos(List<Playlist> videos) {
        this.videos = videos;
        notifyDataSetChanged();
    }

    public Playlist getItemAt(int position) {
        if (position > -1 && position < getCount()) {
            return videos.get(position);
        } else {
            return null;
        }
    }

    public void cleanup() {
        if (videos != null && !videos.isEmpty()) {
            videos.clear();
            videos = null;
        }
    }
}

view_video_player.xml

<com.example.abd.ui.playlist.PlaylistVideoPlayerView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="@color/white">

    <com.example.abd.ui.misc.CustomVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="top"
        android:background="@color/gray_holo_light" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <LinearLayout
        android:id="@+id/video_exit_btn_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:background="@drawable/semi_transparent_button_bg"
        android:visibility="gone">

        <com.gc.materialdesign.views.ButtonFlat
            android:id="@+id/btn_video_exit"
            android:layout_width="wrap_content"
            android:layout_height="38dp"
            android:background="@color/white"
            android:text="@string/action_done" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/yt_video_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="top"
        android:background="@color/gray_holo_light"
        android:orientation="vertical" />

</com.example.abd.ui.playlist.PlaylistVideoPlayerView>

如果视频的网址来自YouTube,那么我会将CustomVideoView的可见性设置为GONE,并将LinearLayout ytVideoContainer设置为VISIBLE,以便显示片段。

PlaylistVideoPlayerView.java

public void bindYouTubeVideo(FragmentTransaction fragmentTransaction, YouTubeVideo youTubeVideo) {
    if (videoView != null && ytVideoContainer != null) {
        ytVideoContainer.setVisibility(VISIBLE);
        videoView.setVisibility(GONE);
        progressBar.setVisibility(GONE);
    }

    Bundle bundle = new Bundle();
    bundle.putSerializable(YOUTUBE_VIDEO_OBJ, youTubeVideo);

    YouTubePlayerFragment yt_fragment = new YouTubePlayerFragment();
    yt_fragment.setArguments(bundle);

    fragmentTransaction
            .replace(ytVideoContainer.getId(), yt_fragment)
            .commit();
}

onPageChanged()的{​​{1}}听众摘录:

PlaylistActivity.java

ViewPager

似乎片段未添加到当前页面,这是否与if (url.contains("youtube.com") || url.contains("youtu.be")) { GetVideosDetailsByIDs getVideosDetailsByIDs = new GetVideosDetailsByIDs(); final String youtubeId = extractYTId(url); try { getVideosDetailsByIDs.init(youtubeId); GetYouTubeVideosTask task = new GetYouTubeVideosTask(getVideosDetailsByIDs, new GetYouTubeVideosTask.VideoResultListener() { @Override public void onResults(List<YouTubeVideo> videosList) { if (videosList != null && !videosList.isEmpty()) { YouTubeVideo youTubeVideo = videosList.get(0); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); if (!isFinishing()) { videoView.bindYouTubeVideo(fragmentTransaction, youTubeVideo); } } else { handleGetVideoStreamFailure(); } } }); task.execute(); } catch (IOException e) { Timber.e(e, "Failed to play the youtube video."); handleGetVideoStreamFailure(); } } 有关?或者我添加片段的方式?

1 个答案:

答案 0 :(得分:0)

如果要替换viewpager中的片段,则必须通知适配器更改。需要更新您的片段列表以删除第3个片段并在其位置添加新片段。完成后,您可以在适配器中调用notifyDataSetChanged。

您可以在适配器中使用这样的方法:

public void replaceFragment(Fragment fragment, String title, int index) {
    mFragmentList.remove(index);
    mFragmentList.add(index, fragment);
    // do the same for the title
    notifyDataSetChanged();
}