【问题标题】:Android how to add videfragment dynamically to my layoutAndroid如何动态添加视频碎片到我的布局
【发布时间】:2017-01-18 13:32:19
【问题描述】:

我的活动中有一个 VideoFragment 类。

  public static  class VideoFragment extends YouTubePlayerFragment
        implements YouTubePlayer.OnInitializedListener {

    private YouTubePlayer player;
    private String videoId;
    public int height;

    public static VideoFragment newInstance() {
        return new VideoFragment();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initialize(getString(R.string.api_key), this);
    }

    @Override
    public void onDestroy() {
        if (player != null) {
            player.release();
        }
        super.onDestroy();
    }

    public void setVideoId(String videoId) {
        if (videoId != null && !videoId.equals(this.videoId)) {
            this.videoId = videoId;
            if (player != null) {
                player.cueVideo(videoId);
            }
        }
    }

    public void pause() {
        if (player != null) {
            player.pause();
        }
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer player, boolean restored) {
        this.player = player;
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
        player.setOnFullscreenListener((VideoOptionTemplate) getActivity());
        if (!restored && videoId != null) {
            player.loadVideo(videoId);
            // player.play();
        }
        player.play();
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        this.player = null;
    }
}

我需要创建一个带有静态计数的 for 循环,以便将每个循环中的片段添加到我的线性输出中,每个片段必须有不同的 id,这样我就可以在每个片段中加载一个 youtube 视频。

这就是我创建循环的方式

for(int i=0;i<10;i++)
 {
        LayoutInflater inflater = VideoOptionTemplate.this.getLayoutInflater();
    View to_add = inflater.inflate(R.layout.video_layout,
            pagelayout, false);
    final LinearLayout childlayout = (LinearLayout) to_add.findViewById(R.id.layoutoption);//this layout contains the fragment of                 video player with specific id
    TextView counttxt = (TextView) to_add.findViewById(R.id.counttxt);
    counttxt.setText(String.valueOf(i + 1));
    pagelayout.addView(childlayout, i);
 }

这是我在每个循环中调用的布局并添加到我的父布局中

<LinearLayout
    android:id="@+id/uploadlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="upload"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView

            android:id="@+id/optionimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="10dp"
            android:src="@drawable/video"

            android:textColor="@android:color/black" />


        <EditText

            android:id="@+id/optionvideo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_toRightOf="@+id/subject"
            android:layout_weight="1"

            android:background="@android:color/transparent"
            android:paddingBottom="15dp"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:paddingTop="15dp"
            android:text="Add your video"
            android:textSize="12sp" />

    </LinearLayout>

    <fragment
        android:id="@+id/video_fragment_container"
        class="u2vote.activities.VideoOptionTemplate$VideoFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"
        android:gravity="center" />
</LinearLayout>

【问题讨论】:

    标签: android android-fragments youtube-api


    【解决方案1】:

    这有点棘手。您实际上是在尝试将片段添加到动态视图,开箱即用的支持并不好。也就是说,您可能可以做两件事:

    1. 您可以在布局内添加片段,而不是 用 FrameLayout 替换片段标签并使用添加片段 一个片段交易。为此,您需要指定 不过 FrameLayout 显然对你所有的人来说都是一样的 动态视图。你可以做些什么来解决这个问题是 手动给那些 FrameLayout 自定义 id,如下所述: https://stackoverflow.com/a/18296943/504855

    2. 一种不那么笨拙的方法可能是将这些片段包装在另一个片段中。片段支持库支持子片段,因此您可以将该布局转换为片段,并使用包装片段中的 getChildFragmentManager() 以编程方式将 PlayerFragment 添加到该布局中。然后可以使用您活动的常规 FragmentManager 将包装器直接添加到“页面布局”中。

    【讨论】:

      猜你喜欢
      • 2016-09-25
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多