Youtubeplayer将一个片段放入viewpager中

时间:2014-02-16 14:57:03

标签: android android-fragments android-viewpager

我正在尝试将YouTubePlayerSupportFragment中的fragment加载到viewpager,但我无法加载2个或更多YouTubePlayerSupportFragment

如果我加载2或更多,我得到下一个错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

但如果加载1它可以正常工作。

我如何加载?

谢谢大家,抱歉我的英语不好。

xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:orientation="vertical"
        android:padding="20dp" >

        <TextView
            android:id="@+id/tvTituloTecnica"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#045FB4" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#08088A" />

        <ImageView
            android:id="@+id/ivTecnica"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name" />

        <TextView
            android:id="@+id/tvDescripcion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp" />

        <fragment
            android:id="@+id/youtube_fragment"
            android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- <com.google.android.youtube.player.YouTubeThumbnailView -->
        <!-- android:id="@+id/youtube_view" -->
        <!-- android:layout_width="match_parent" -->
        <!-- android:layout_height="match_parent" -->
        <!-- android:layout_marginTop="15dp" /> -->

    </LinearLayout>

</ScrollView>

加载视图的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
// if (view == null) {
view = inflater.inflate(R.layout.tecnica_list_item, container, false);
// }

Bundle b = this.getArguments();
tecnica = ViewPagerAdapter.tecnicas.get(b.getInt("tecnica"));

TextView titulo = (TextView) view.findViewById(R.id.tvTituloTecnica);
titulo.setText(tecnica.getNombre());

TextView descripcion = (TextView) view.findViewById(R.id.tvDescripcion);
descripcion.setText(tecnica.getDescripcion());

ImageView imagenTecnica = (ImageView) view.findViewById(R.id.ivTecnica);
imagenTecnica.setImageResource(this
        .getActivity()
        .getResources()
        .getIdentifier("drawable/" + tecnica.getRutaImagen(), null,
                this.getActivity().getPackageName()));



this.videoId = tecnica.getUrlYoutube();

PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance(videoId);

this.getFragmentManager().beginTransaction()
        .replace(R.id.youtube_fragment, myFragment).commit();
myFragment.init();

return view;
}

1 个答案:

答案 0 :(得分:3)

您正在尝试将新分裂的片段附加到片段,而不是容器。 我猜你试图在布局中使用当前实例化的片段,这个布局是一个ViewPager详细页面框架,对吗?

您不能使用在布局xml上声明它们的嵌套片段。您必须在第一个片段xml上放置占位符布局并将其注入代码。

有些事情:

...

        <TextView
            android:id="@+id/tvDescripcion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp" />

        <FrameLayout
            android:id="@+id/youtube_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>
...

并在ViewPager详细信息片段上:

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.youtube_fragment, youTubePlayerFragment).commit();

...

不要忘记使用

正确初始化玩家
 youTubePlayerFragment.initialize(apiKey, new OnInitializedListener() {

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            // Play, cue or whatever you want to do with the player
    }


    ... // Implement other inteface methods here

    });