2013-12-16 27 views
0

我有一個視圖,其中包含一個YouTubePlayerSupportFragment,其周圍有一個小框用於樣式。我想將這個視圖的多個實例(垂直)添加到另一個視圖的頂部。我有它添加單個視圖的工作,但是當我嘗試添加多個時,他們只是彼此堆疊,以便只有最後添加的那個可見。我能夠證明這種情況正在發生,通過編程將第一個視圖添加更長時間,並且我可以在最上面的視圖下看到它的額外高度。添加到linearLayout的片段正在堆疊,導致僅添加最後一個片段可見

這裏是我的代碼:

fragment_youtube_video:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp" 
    android:layout_marginTop="5dp" 
    android:background="#FFFFFFFF"> 

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

    <TextView 
     android:id="@+id/videoTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="8dp" 
     android:textColor="#000000" 
     android:text="Video" /> 

</LinearLayout> 

fragment_videos:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background"> 

    <ImageView 
     android:id="@+id/topBar" 
     android:background="#000000" 
     android:layout_width="match_parent" 
     android:layout_height="45dp" /> 

    <ImageView 
     android:id="@+id/logoImageView" 
     android:layout_width="120dp" 
     android:layout_height="60dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/CD_logo" 
     android:src="@drawable/logo" /> 

    <RelativeLayout 
     android:id="@+id/pagerTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logoImageView" 
     android:background="#80000000" 
     android:paddingBottom="10dp" 
     android:paddingLeft="2dp" 
     android:paddingRight="2dp" 
     android:paddingTop="10dp" 
     android:layout_marginLeft="12dp" 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="12dp" > 
    [REMOVED FOR SPACE] 
    </RelativeLayout> 
    <ScrollView 
     android:id="@+id/scrollableVideoBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/pagerTitle" 
     android:layout_alignRight="@+id/pagerTitle" 
     android:layout_below="@+id/pagerTitle"> 
     <LinearLayout 
      android:id="@+id/videosLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="#a0000000"> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

VideosFragment.java:

private void addVideoViews() { 
    int count = 0; 
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

    for (String vidURL : videoURLs) { 
     new YoutubeFragment(); 

     YoutubeFragment player = YoutubeFragment.newInstance(count, vidURL); 
     fragmentTransaction.add(R.id.videosLayout, player, "x_" + count); 

     count++; 
    } 
    fragmentTransaction.commit(); 
} 

我可以提供更多的代碼,但我相信這些是必要的部分。如果我錯誤地回答這個問題,請隨時告訴我。我很親密,我無法弄清楚爲什麼他們堆疊。

任何幫助,將不勝感激!

回答

1

我們走進聊天室,這就是我們來到:

LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.videosLayout); 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
LayoutInflater inflater = LayoutInflater.from(getActivity()); 

for (String vidURL : videoURLs) { 
final String vidUrl2 = vidURL; 
LinearLayout placeholder = (LinearLayout) inflater.inflate(R.layout.fragment_youtube_video, container, false); 
View youtuber = placeholder.findViewById(R.id.youTubeVideo); 
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment(); 
fragmentTransaction.add(youtuber.getId(), player); 
placeholder.setId(12345); 
parentLayout.addView(placeholder); 

player.initialize("secret", new OnInitializedListener() { 

@Override 
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, 
boolean arg2) { 
if (!arg2) { 
arg1.cueVideo(vidUrl2); 
} 
    } 

    @Override 

public void onInitializationFailure(Provider arg0, 
YouTubeInitializationResult arg1) { 

} 
}); 
} 
fragmentTransaction.commit(); 

佔位符是一個有兩個孩子的LinearLayout,一個LinearLayout中舉行的YouTube片段,和一個TextView舉行的稱號。

+0

我不知道確切的金額。它可能會在1-10之間。我想我可以增加10個左右,然後這樣做。儘管如此,我希望保持活力。我可以動態添加這些佔位符視圖嗎? – PFranchise

+0

我試圖使用鏈接示例中的代碼,但仍然會導致它們被堆疊。這讓我傾向於認爲我有一個佈局問題。 – PFranchise

+0

是的,這將是最有可能的罪魁禍首。如果你把佔位符放在那可以正常工作?如果那是正確的,那麼至少你有一個正確的方法來讓它們一致。 –

0

我假設,當你添加多個視頻時,你只需將它們交換到片段中。沒有多個片段,因此只有一個視頻會在任何給定時間顯示。

我認爲你應該使用ListView來代替,這樣,你可以用衆多視頻來填充它們,這些視頻都具有相同的總體佈局資源。希望這有助於。