1
我試圖在fragment
中加載到viewpager
,但我無法加載2個或更多YouTubePlayerSupportFragment
。Youtubeplayer在一個視圖片段中的片段
如果我加載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;
}
嗨正確initializar玩家非常感謝您!爲你的答案,併爲延誤抱歉。 它工作得很好,但是當我快速更改視圖時,youtube會中斷。 我該如何解決它? – Dracknes