我正在我的Android应用中使用youtube api来加载RecyclerView
中视频的缩略图。以下是我在适配器旁边的实现:
private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
这将保存每个适配器元素的YouTubeThumbnailView
的装载程序。这是我们用于初始化YouTubeThumbnailLoader
的代码:
YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(holder.thumbnail);
final String videoId = Utils.getYoutubeIdFromUrl(m.getYoutubeUrl());
holder.thumbnail.setTag(videoId);
if(loader != null) {
loader.setVideo(videoId);
}
}
初始化ViewHolder
的元素时,我按如下方式初始化缩略图加载器:
private void setupYoutubeThumbnail() {
thumbnail.initialize(RiyazApplication.applicationInstance
.getString(R.string.youtube_api_developer_key),
thumbnailListener);
thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag() != null && itemClickListener != null) {
itemClickListener.onClickVideo(mList.get(getAdapterPosition()));
}
}
});
以及释放缩略图加载器的代码如下:
public void releaseThumbnailLoaders() {
final Set<YouTubeThumbnailView> keysSet = thumbnailViewToLoaderMap.keySet();
for(YouTubeThumbnailView view: keysSet) {
final YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(view);
if(loader != null) {
loader.release();
}
}
thumbnailViewToLoaderMap.clear();
}
在我的crashalytics中,出现以下异常:
Fatal Exception: java.lang.IllegalStateException: YouTubeServiceEntity not initialized
at android.os.Parcel.readException(Parcel.java:1497)
at android.os.Parcel.readException(Parcel.java:1443)
at com.google.android.youtube.player.internal.l$a$a.a(Unknown Source)
at com.google.android.youtube.player.internal.o.a(Unknown Source)
at com.google.android.youtube.player.internal.p.(Unknown Source)
at com.google.android.youtube.player.internal.ac.a(Unknown Source)
at com.google.android.youtube.player.YouTubeThumbnailView$a.a(Unknown Source)
at com.google.android.youtube.player.internal.r.g(Unknown Source)
at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(NativeStart.java)
我不确定,为什么要得到这个。谁能帮忙吗?
答案 0 :(得分:1)
将您的initaizle语句放入try-catch语句中,如下所示:
try { thumbnail.initialize(RiyazApplication.applicationInstance.getString(R.string.youtube_api_developer_key), thumbnailListener);
}
catch(IllegalStateException w){}
(我知道它的格式不正确,无论出于何种原因,Stack Overflow都不会让我将其放在新行上)
答案 1 :(得分:0)
请检查以下代码。我正在使用静态视频列表来填充RecyclerView。下面的代码已在活动中使用RecyclerView实现。
活动:-
public class YouTubeVideoListActivity extends AppCompatActivity {
private RecyclerView recyclerViewList;
private RecyclerAdapter adapter;
private static final List<VideoEntry> VIDEO_LIST;
static {
List<VideoEntry> list = new ArrayList<VideoEntry>();
list.add(new VideoEntry("YouTube Collection", "Y_UmWdcTrrc"));
list.add(new VideoEntry("GMail Tap", "1KhZKNZO8mQ"));
list.add(new VideoEntry("Chrome Multitask", "UiLSiqyDf4Y"));
list.add(new VideoEntry("Google Fiber", "re0VRK6ouwI"));
list.add(new VideoEntry("Autocompleter", "blB_X38YSxQ"));
list.add(new VideoEntry("GMail Motion", "Bu927_ul_X0"));
list.add(new VideoEntry("Translate for Animals", "3I24bSteJpw"));
VIDEO_LIST = Collections.unmodifiableList(list);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_video_list);
recyclerViewList=findViewById(R.id.recyclerViewList);
adapter = new RecyclerAdapter(this, VIDEO_LIST);
recyclerViewList.setLayoutManager(new LinearLayoutManager(this));
recyclerViewList.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
adapter.releaseLoaders();
}
}
模型类:-
public class VideoEntry {
private final String text;
private final String videoId;
public VideoEntry(String text, String videoId) {
this.text = text;
this.videoId = videoId;
}
public String getText() {
return text;
}
public String getVideoId() {
return videoId;
}
}
回收站适配器:-
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<VideoEntry> entries;
private final List<View> entryViews;
private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
private final LayoutInflater inflater;
private final ThumbnailListener thumbnailListener;
private boolean labelsVisible;
private Context context;
public RecyclerAdapter(Context context,List<VideoEntry> entries) {
this.entries = entries;
this.context = context;
entryViews = new ArrayList<>();
thumbnailViewToLoaderMap = new HashMap<>();
inflater = LayoutInflater.from(context);
thumbnailListener = new ThumbnailListener();
labelsVisible = true;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.you_tube_row, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
final Holder h = (Holder) holder;
VideoEntry entry = entries.get(position);
YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(h.thumbnail);
if (loader == null) {
// 2) The view is already created, and is currently being initialized. We store the
// current videoId in the tag.
h.thumbnail.setTag(entry.getVideoId());
} else {
// 3) The view is already created and already initialized. Simply set the right videoId
// on the loader.
h.thumbnail.setImageResource(R.drawable.loading_thumbnail);
loader.setVideo(entry.getVideoId());
}
h.label.setText(entry.getText());
h.label.setVisibility(labelsVisible ? View.VISIBLE : View.GONE);
}
@Override
public int getItemCount() {
return entries.size();
}
@Override
public long getItemId(int position) {
return 0;
}
public void releaseLoaders() {
for (YouTubeThumbnailLoader loader : thumbnailViewToLoaderMap.values()) {
loader.release();
}
}
public void setLabelVisibility(boolean visible) {
labelsVisible = visible;
for (View view : entryViews) {
view.findViewById(R.id.text).setVisibility(visible ? View.VISIBLE : View.GONE);
}
}
class Holder extends RecyclerView.ViewHolder {
TextView label;
YouTubeThumbnailView thumbnail;
Holder(final View itemView) {
super(itemView);
label = itemView.findViewById(R.id.text);
thumbnail = itemView.findViewById(R.id.thumbnail);
thumbnail.initialize(DEVELOPER_KEY, thumbnailListener);
}
}
private final class ThumbnailListener implements
YouTubeThumbnailView.OnInitializedListener,
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
@Override
public void onInitializationSuccess(
YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
loader.setOnThumbnailLoadedListener(this);
thumbnailViewToLoaderMap.put(view, loader);
view.setImageResource(R.drawable.loading_thumbnail);
String videoId = (String) view.getTag();
loader.setVideo(videoId);
}
@Override
public void onInitializationFailure(
YouTubeThumbnailView view, YouTubeInitializationResult loader) {
view.setImageResource(R.drawable.no_thumbnail);
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
}
@Override
public void onThumbnailError(YouTubeThumbnailView view, YouTubeThumbnailLoader.ErrorReason errorReason) {
view.setImageResource(R.drawable.no_thumbnail);
}
}
}
回收站列表行布局(you_tube_row.xml):-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:gravity="center"
android:background="?android:attr/activatedBackgroundIndicator">
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/no_thumbnail"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"/>
</LinearLayout>
如果您想了解更多详细信息,可以从here下载示例代码。