gpt4 book ai didi

android - CardView在Android中未显示Youtube缩略图 View

转载 作者:行者123 更新时间:2023-12-03 05:54:57 33 4
gpt4 key购买 nike

我在应用程序中使用了Youtube API以及FirebaseRecyclerAdapter和RecyclerView,由于某些原因,我的YouTube缩略图未显示在应用程序中,尽管我已将其添加到CardView中,并且FirebaseRecyclerAdapter具有访问CardView的代码,但是当我启动时应用程序并转到该 Activity ,那里没有缩略图显示。我已将其实现为没有Firebase适配器的硬编码。

我尝试过的步骤:

  • 添加了布局管理器
  • 调整cardView和thumbnailView的高度和宽度

  • 我正在使用Android Studio

    YouTube类文件:

    public class YoutubeVideos extends YouTubeBaseActivity {

    private RecyclerView YouRecycler;
    private DatabaseReference YDatabaseReference;


    @Override
    protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.youtube_videos);
    YDatabaseReference = FirebaseDatabase.getInstance().getReference().child("youtubed");
    YouRecycler = (RecyclerView)findViewById(R.id.Youtube_recycler);

    }

    @Override
    public void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder> YfirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder>(

    post2youtube.class,
    R.layout.youtube_videos_card,
    YoutubeViewHolder.class,
    YDatabaseReference
    ) {
    @Override
    protected void populateViewHolder(YoutubeViewHolder viewHolder, post2youtube model, int position) {
    viewHolder.setYoutube(model.getYoutube());
    }

    };

    YouRecycler.setAdapter(YfirebaseRecyclerAdapter);
    }


    public static class YoutubeViewHolder extends RecyclerView.ViewHolder {
    View yView;

    public YoutubeViewHolder(View YitemView) {
    super(YitemView);
    yView = YitemView;
    }
    public void setYoutube(final String youtube){
    final YouTubePlayerView youPlay = (YouTubePlayerView) yView.findViewById(R.id.youtuber);
    youPlay.initialize("Some key",
    new YouTubePlayer.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    youTubePlayer.loadVideo(youtube);
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

    }

    });
    }

    }
    }

    YouTube RecyclerView文件:

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


    <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id ="@+id/Youtube_recycler"/>

    </LinearLayout>

    YouTube cardView xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/youtube_cardView"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtuber"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


    </RelativeLayout>

    </android.support.v7.widget.CardView>

    最佳答案

    YoutubePlayer View 太大,无法添加到recyclerview。因此,请使用YouTubeThumbnailView来显示缩略图。当用户单击其中之一时,您可以启动YouTubePlayerFragment或具有YouTubeplayerView View 的 Activity 。

    下面是引用代码,

    在xml中:

    <com.google.android.youtube.player.YouTubeThumbnailView
    android:id="@+id/youtube_view"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_marginBottom="@dimen/ten_dp"
    android:visibility="gone"/>

    在java中:
    // Initializing video player with developer key
    youtube_view.initialize(Config.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
    youTubeThumbnailLoader.setVideo(videoId);
    youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
    @Override
    public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
    youTubeThumbnailLoader.release();
    Toast.makeText(getActivity(), "It's a valid youtube url.", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
    try {
    Toast.makeText(getActivity(), "Not a valid youtube url.", Toast.LENGTH_SHORT).show();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    });
    }
    @Override
    public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
    }
    });

    关于android - CardView在Android中未显示Youtube缩略图 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46744226/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com