在加载活动中的所有其他视图后加载YoutubeSupportFragment

时间:2016-02-26 14:33:12

标签: android android-layout android-fragments

我的应用程序中有一个基于片段的活动,它使用Async任务从API读取数据,数据包括电影详细信息以及一些视频ID。我创建了另一个片段,它扩展了YouTubePlayerSupportFragment并显示缩略图并允许播放视频。我正在使用Supportfragmentmanager在主片段中添加基于youtube播放器的片段。

除了在每个其他视图充气后(例如文本视图,图像视图等),youtube缩略图膨胀之外,一切正常,即使我在为内容片段中的任何其他视图设置值之前添加了youtube片段。我还尝试使所有内容不可见,加载所有内容包括youtube片段,然后将可见性设置为true,但即便如此,youtube片段加载大约2,3秒后其他所有内容已经膨胀。反正有没有让youtube播放器片段加载更快?

这是内容片段布局。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:weightSum="1"
    tools:context=".fragments.MovieDetailsFragment"
    tools:showIn="@layout/activity_movie_details">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/movie_details_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

            <!-- Movie name, poster -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <!-- Youtube thumbnail-->
                <FrameLayout
                    android:id="@+id/thumbnail_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    ></FrameLayout>
                <!-- Backdrop Image -->
                <LinearLayout
                    android:id="@+id/backdrop_image_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/thumbnail_layout"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/movie_details_backdrop_image"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/backdrop_image_layout"
                    android:layout_marginTop="-15dp"
                    android:paddingLeft="10dp">

                    <ImageView
                        android:id="@+id/movie_details_poster_thumb"
                        android:layout_width="75dp"
                        android:layout_height="110dp"
                        android:background="#ffffff"
                        android:elevation="10dp" />

                    <TextView
                        android:id="@+id/movie_details_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:paddingTop="20dp"
                        android:textColor="@color/fontColorMaroon"
                        android:textSize="12sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </RelativeLayout>

            <!-- Movie rating, genre -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/borderset_topbottom"
                android:gravity="center"
                android:orientation="horizontal"
                android:paddingBottom="8dp"
                android:paddingTop="8dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingLeft="16dp">

                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/movie_details_rating_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:src="@drawable/rating" />

                        <TextView
                            android:id="@+id/movie_details_rating"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left|center_vertical"
                            android:paddingLeft="16dp"
                            android:text="9.3"
                            android:textColor="@color/iconColor"
                            android:textSize="13sp" />

                    </FrameLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:orientation="horizontal"
                        android:paddingTop="5dp">

                        <TextView
                            android:id="@+id/movie_details_votes"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="100"
                            android:textAlignment="center"
                            android:textColor="@color/fontColorMaroon"
                            android:textSize="@dimen/smaller_font_size" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:paddingLeft="2dp"
                            android:src="@drawable/voter" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingLeft="16dp">

                    <ImageView
                        android:id="@+id/movie_details_genre_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/sport" />

                    <TextView
                        android:id="@+id/movie_details_genre_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="5dp"
                        android:text="Film"
                        android:textAlignment="center"
                        android:textColor="@color/fontColorMaroon"
                        android:textSize="@dimen/smaller_font_size" />
                </LinearLayout>
            </LinearLayout>

            <!-- Movie overview -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp">

                <TextView
                    android:id="@+id/movie_details_overview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="fill_horizontal"
                    android:textColor="@color/fontColorBlack"
                    android:textSize="@dimen/small_font_size" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <FrameLayout
        android:id="@+id/movie_details_progressbar_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:visibility="gone">
        <include layout="@layout/progressbar_item"/>
    </FrameLayout>
</LinearLayout>

我正在使用的Async类的代码在这里(我从上面的父片段的OnCreateView函数调用我的Async类)。

 protected void onPostExecute(Movie movie) {
    super.onPostExecute(movie);
    Utilities utilities = new Utilities(context);

    TextView itemRating = (TextView) activity.findViewById(R.id.movie_details_rating);
    TextView itemVotes = (TextView) activity.findViewById(R.id.movie_details_votes);
    TextView itemGenre = (TextView) activity.findViewById(R.id.movie_details_genre_text);
    TextView itemOverview = (TextView) activity.findViewById(R.id.movie_details_overview);
    TextView itemName = (TextView) activity.findViewById(R.id.movie_details_name);

    ImageView itemGenreImage = (ImageView) activity.findViewById(R.id.movie_details_genre_image);
    ImageView itemBackdropImage  = (ImageView) activity.findViewById(R.id.movie_details_backdrop_image);
    ImageView itemPoster = (ImageView) activity.findViewById(R.id.movie_details_poster_thumb);

    FrameLayout thumbnailFrame = (FrameLayout)activity.findViewById(R.id.thumbnail_layout);

    if(movie.isInternetAvailable()== true && movie != null){
        if(movie.getVideos().getVideos() != null && movie.getVideos().getVideos().size() != 0) {

            YoutubeThumbnailFragment youtubeThumbnailFragment = YoutubeThumbnailFragment.newInstance(movie.getVideos().getVideos().get(0).getKey());
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.thumbnail_layout,youtubeThumbnailFragment).commit();
            //thumbnailFrame.setVisibility(View.VISIBLE);
        }
        else{
            String imageSize = utilities.GetBackdropSize();
            com.squareup.picasso.Picasso.with(context).load(context.getString(R.string.tmdb_image_url) + imageSize + movie.getBackdropPath()).into(itemBackdropImage);
            itemBackdropImage.setVisibility(View.VISIBLE);
            thumbnailFrame.setVisibility(View.GONE);
        }
        itemName.setText(movie.getTitle());
        com.squareup.picasso.Picasso.with(context).load(context.getString(R.string.tmdb_image_url) + "w780" + movie.getPosterPath()).fit().into(itemPoster);

        itemRating.setText(String.format("%.1f", movie.getVoteAverage()));
        itemVotes.setText(NumberFormat.getNumberInstance(Locale.US).format(movie.getVoteCount()));
        if(movie.getGenres() != null) {
            itemGenre.setText(movie.getGenres().get(0).getName());
            itemGenreImage.setImageResource(GetGenreImageResourceId(movie.getGenres().get(0).getId()));
        }
        itemOverview.setText(movie.getOverview());
        progressLayout.setVisibility(View.GONE);
        contentLayout.setVisibility(View.VISIBLE);

    }
    else {
        progressLayout.setVisibility(View.GONE);
        utilities.ShowInternetUnavailablityDialog(activity);
    }
}

这是youtube fragement布局。

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

        <com.google.android.youtube.player.YouTubeThumbnailView
            android:id="@+id/trailer_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/playImageView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/playbutton"
            />
</RelativeLayout>

youtube片段的代码。

public class YoutubeThumbnailFragment extends Fragment implements YouTubeThumbnailView.OnInitializedListener {
    private static final String KEY_VIDEO_ID = "video_id";
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private String currentVideoID;
    private YouTubeThumbnailView thumbnailView;
    private YouTubeThumbnailLoader thumbnailLoader;


    public YoutubeThumbnailFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static YoutubeThumbnailFragment newInstance(String videoId) {
        YoutubeThumbnailFragment fragment = new YoutubeThumbnailFragment();
        Bundle args = new Bundle();
        args.putString(KEY_VIDEO_ID, videoId);
        fragment.setArguments(args);
        return fragment;
    }

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

        final Bundle arguments = getArguments();

        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_VIDEO_ID)) {
            currentVideoID = savedInstanceState.getString(KEY_VIDEO_ID);
        } else if (arguments != null && arguments.containsKey(KEY_VIDEO_ID)) {
            currentVideoID = arguments.getString(KEY_VIDEO_ID);
        }

    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
        //Toast.makeText(getContext(),
        //        "onInitializationSuccess", Toast.LENGTH_SHORT).show();

        thumbnailLoader = youTubeThumbnailLoader;
        youTubeThumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailListener());
        youTubeThumbnailLoader.setVideo(currentVideoID);
    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
        String errorMessage =
                String.format("onInitializationFailure (%1$s)",
                        youTubeInitializationResult.toString());
        Toast.makeText(getContext(), errorMessage, Toast.LENGTH_LONG).show();
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_youtube_thumbnail, container, false);
        ImageView playImageView = (ImageView)view.findViewById(R.id.playImageView);
        thumbnailView = (YouTubeThumbnailView)view.findViewById(R.id.trailer_thumbnail);

        thumbnailView.initialize(getString(R.string.youtube_api_key), this);


        thumbnailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartVideo();
            }
        });
        playImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartVideo();
            }
        });
        return view;
    }

    @Override
    public void onStop() {
        super.onStop();
        thumbnailLoader.release();

    }

    private void StartVideo(){
        Intent intent = new Intent(getActivity(),YoutubeVideo.class);
        intent.putExtra(KEY_VIDEO_ID, currentVideoID);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        thumbnailView = null;
        thumbnailLoader = null;
    }

    private final class ThumbnailListener implements
            YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView thumbnail, String videoId) {
            //Toast.makeText(getContext(),
            //        "onThumbnailLoaded", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView thumbnail,
                                     YouTubeThumbnailLoader.ErrorReason reason) {
           // Toast.makeText(getContext(),
           //         "onThumbnailError", Toast.LENGTH_SHORT).show();
        }

    }

}

0 个答案:

没有答案