列表视图中的YouTube播放器片段

时间:2017-02-27 18:32:55

标签: android listview android-listfragment youtube-data-api

我正在尝试使用来自json的youtube视频创建一个列表视图,但我无法将youtube播放器片段实现到我的VideoAdapter中。

我已经检查了来自youtube api的示例代码,但我还没有进展。如何将youtube播放器片段实现到我的VideoAdapter文件中。

VideoAdapter.java

public class VideoAdapter extends ArrayAdapter<VideosActivity.Videos>{

    ArrayList<VideosActivity.Videos> videoList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;

    public VideoAdapter(Context context, int resource, ArrayList<VideosActivity.Videos> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        videoList = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);


            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.thumb.setImageResource(R.drawable.noimage);
        new DownloadImageTask(holder.thumb).execute(videoList.get(position).getThumb());
        return v;
    }

    static class ViewHolder {
        public ImageView thumb;

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我实现了我的需求。任何需要代码在上面的播放列表中列出视频的人。