我尝试使用YouTube API在Listview中显示YouTubeThumbnailView,但无法执行此操作。我需要显示文本和视频(如果没有视频,则显示图像),这需要自定义适配器和YouTubeThumbnailView。我知道有一些话题可以公开,但没有办法解决它。 (一切都很适合YouTubeThumbnailView)
这是适配器代码:
public class NewsAdapter extends BaseAdapter implements YouTubeThumbnailView.OnInitializedListener {
public static final String TAG = NewsAdapter.class.getSimpleName();
private Context mContext;
private List<News> mListNews;
private Map<View, YouTubeThumbnailLoader> mLoaders;
public NewsAdapter(Context context, List<News> list) {
this.mListNews = list;
this.mContext = context;
this.mLoaders = new HashMap<>();
}
@Override
public int getCount() {
return this.mListNews.size();
}
@Override
public Object getItem(int i) {
return this.mListNews.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
Holder viewHolder;
String videoId = mListNews.get(position).youtube_code;
if (v == null) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.item_news, null);
viewHolder = new Holder(v);
viewHolder.youTubePlayerView.setTag(videoId);
viewHolder.youTubePlayerView.initialize(BuildConfigData.getKEY_API(), this);
v.setTag(viewHolder);
} else {
viewHolder = (Holder) v.getTag();
// 2) and 3) The view is already created...
YouTubeThumbnailLoader loader = mLoaders.get(viewHolder.youTubePlayerView);
// ...and is currently being initialized. We store the current videoId in the tag.
if (loader == null) {
viewHolder.youTubePlayerView.setTag(videoId);
// ...and already initialized. Simply set the right videoId on the loader.
} else {
viewHolder.youTubePlayerView.setImageBitmap(null);
loader.setVideo(videoId);
}
}
if(this.mListNews.get(position).title != null) viewHolder.newsTitleTextView.setText(Html.fromHtml(mListNews.get(position).title));
viewHolder.newsCategoryTextView.setVisibility(View.GONE);
if(!this.mListNews.get(position).main_picture.isEmpty()) {
final Holder finalview = viewHolder;
return v;
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
String videoId = (String) youTubeThumbnailView.getTag();
mLoaders.put(youTubeThumbnailView, youTubeThumbnailLoader);
youTubeThumbnailView.setImageBitmap(null);
youTubeThumbnailLoader.setVideo(videoId);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
class Holder {
ImageView newsImgImageView;
TextView newsTitleTextView;
TextView newsCategoryTextView;
YouTubeThumbnailView youTubePlayerView;
public Holder(View base) {
newsImgImageView = (ImageView) base.findViewById(R.id.news_img);
newsTitleTextView = (TextView) base.findViewById(R.id.news_title);
newsCategoryTextView = (TextView) base.findViewById(R.id.news_category);
youTubePlayerView = (YouTubeThumbnailView) base.findViewById(R.id.youtube_player_thumb);
}
}
这是堆栈:
java.lang.IllegalStateException: android.os.DeadObjectException
at com.google.android.youtube.player.internal.p.a(Unknown Source)
at com.google.android.youtube.player.internal.a.setVideo(Unknown Source)
at com.meetphone.fabvillesdk.adapter.NewsAdapter.getView(NewsAdapter.java:85)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillGap(ListView.java:662)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4972)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4516)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at com.google.android.youtube.player.internal.k$a$a.a(Unknown Source)
希望有人可以提供帮助。在此先感谢所有人
答案 0 :(得分:0)
如果你需要自定义视图(显示视频,如果存在或显示图像)我认为可能需要将视频视图和图像视频分开使用不同的getView
将您的listView
方法替换为getVideoView
适配器,并为youtube视频视图创建两个单独的getImageView
方法,@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
String videoId = mListNews.get(position).youtube_code
if (videoId.isEmpty()) {
return getVideoView(position, convertView, viewGroup);
} else {
return getVideoView(position, convertView, viewGroup);
}
}
public View getVideoView(int position, View convertView, ViewGroup viewGroup) {
VideoViewHolder viewHolder;
if (convertView == null || (convertView != null && convertView.getTag() instanceof VideoViewHolder)) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.item_news, null);
viewHolder = new Holder(v);
convertView.setTag(viewHolder);
} else {
viewHolder = (VideoViewHolder) convertView.getTag();
}
viewHolder.youTubePlayerView.setTag(videoId);
viewHolder.youTubePlayerView.initialize(BuildConfigData.getKEY_API(), this);
// 2) and 3) The view is already created...
YouTubeThumbnailLoader loader = mLoaders.get(viewHolder.youTubePlayerView);
// ...and is currently being initialized. We store the current videoId in the tag.
if (loader == null) {
viewHolder.youTubePlayerView.setTag(videoId);
// ...and already initialized. Simply set the right videoId on the loader.
} else {
viewHolder.youTubePlayerView.setImageBitmap(null);
loader.setVideo(videoId);
}
if(this.mListNews.get(position).title != null) {
viewHolder.newsTitleTextView.setText(Html.fromHtml(mListNews.get(position).title));
} else {
viewHolder.newsCategoryTextView.setVisibility(View.GONE);
}
return convertView;
}
public View getImageView(int position, View convertView, ViewGroup viewGroup) {
ImageViewHolder viewHolder;
if (convertView == null || (convertView != null && convertView.getTag() instanceof ImageViewHolder)) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.item_news, null);
viewHolder = new Holder(v);
convertView.setTag(viewHolder);
} else {
viewHolder = (ImageViewHolder) v.getTag();
}
if(this.mListNews.get(position).title != null) {
viewHolder.newsTitleTextView.setText(Html.fromHtml(mListNews.get(position).title));
} else {
viewHolder.newsCategoryTextView.setVisibility(View.GONE);
}
if(!this.mListNews.get(position).main_picture.isEmpty()) {
final Holder finalview = viewHolder;
return v;
}
图像视图方法
Holder
修改了您的VideoViewHolder
课程并创建了ImageViewHolder
和class Holder {
ImageView newsImgImageView;
TextView newsTitleTextView;
TextView newsCategoryTextView;
YouTubeThumbnailView youTubePlayerView;
public Holder(View base) {
newsImgImageView = (ImageView) base.findViewById(R.id.news_img);
newsTitleTextView = (TextView) base.findViewById(R.id.news_title);
newsCategoryTextView = (TextView) base.findViewById(R.id.news_category);
youTubePlayerView = (YouTubeThumbnailView) base.findViewById(R.id.youtube_player_thumb);
}
}
class VideoViewHolder extends Holder {
YouTubeThumbnailView youTubePlayerView;
public Holder(View base) {
super(base);
youTubePlayerView = (YouTubeThumbnailView) base.findViewById(R.id.youtube_player_thumb);
}
}
class ImageViewHolder extends Holder {
ImageView newsImgImageView;
public Holder(View base) {
super(base);
newsImgImageView = (ImageView) base.findViewById(R.id.news_img);
}
}
课程
pay.php