如何使用recyclerview打开隐含意图,以便打开YouTube视频

时间:2018-05-13 11:10:06

标签: java android android-recyclerview android-cardview

public class AnimateToolbar extends AppCompatActivity {

    private CollapsingToolbarLayout collapsingToolbar;
    private AppBarLayout appBarLayout;


    private DessertAdapter dessertAdapter;

    private List<Dessert> persons;
    private RecyclerView rv;

    private Menu collapsedMenu;
    private boolean appBarExpanded = true;

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

        setContentView(R.layout.activity_animate_toolbar);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        appBarLayout = (AppBarLayout) findViewById(R.id.appbar);

        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(getString(R.string.android_desserts));

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.header);

        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @SuppressWarnings("ResourceType")
            @Override
            public void onGenerated(Palette palette) {
                int vibrantColor = palette.getVibrantColor(R.color.primary_500);
                collapsingToolbar.setContentScrimColor(vibrantColor);
                collapsingToolbar.setStatusBarScrimColor(R.color.black_trans80);
            }
        });

private void initializeData(){             persons = new ArrayList&lt;&gt;(); //当用户点击此卡片视图或回收视图时,youtube将在youtube应用程序或用户移动设备上可用的浏览器中打开。

        persons.add(new Dessert("Engine", "5.9-litre V12 engine ", R.mipmap.enginelogo));
        persons.add(new Dessert("Lavery Maiss", "25 years old", R.drawable.lavery));
        persons.add(new Dessert("Lillie Watts", "35 years old", R.drawable.lillie));
    }
//I want to open an intent with the first position of persons so that it opens up a youtube video.
    private void initializeAdapter(){
        DessertAdapter adapter = new DessertAdapter(persons);
        rv.setAdapter(adapter);
    }

//I tried different approaches but none of them have worked out for me so far please help





//this is the adapter on which the upper recyclerview is based on


public class DessertAdapter extends RecyclerView.Adapter<DessertAdapter.DessertVh> {



    public static class DessertVh extends RecyclerView.ViewHolder{

        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;


        DessertVh(View itemView){
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
            }

            }


    List<Dessert> persons;

    DessertAdapter(List<Dessert> persons){this.persons = persons;}


    @Override

    public void onAttachedToRecyclerView(RecyclerView recyclerView){


        super.onAttachedToRecyclerView(recyclerView);
        }

    @Override
    public DessertVh onCreateViewHolder(ViewGroup viewGroup, int i){


        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_dessert, viewGroup,false);
        DessertVh dvh = new DessertVh(v);
        return dvh;

    }

    @Override
    public void onBindViewHolder(DessertVh dessertVh, int i){
        dessertVh.personName.setText(persons.get(i).name);
        dessertVh.personAge.setText(persons.get(i).age);
        dessertVh.personPhoto.setImageResource(persons.get(i).photoId);
        }


    @Override
    public int getItemCount(){return persons.size();}



}

2 个答案:

答案 0 :(得分:0)

尝试这样的事情: (在RecylcerView的OnItemClicklistener中):

Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, persons.get(position); intent.setPackage("com.google.android.apps.youtube.app"); startActivity(intent);

我希望这有帮助! :d

答案 1 :(得分:0)

您可以设置点击这样的每个项目。您需要在列表项中传递一些视频网址。然后你将用该网址启动youtube活动。

List<Dessert> persons;
Context context
DessertAdapter(Context context, List<Dessert> persons)
{
  this.persons = persons;
  this.context = context;
}

...

@Override
public void onBindViewHolder(DessertVh dessertVh, int i){
   ...

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri url = Uri.parse("vnd.youtube://" + video_id); // get your url from list item or your code.
            Intent intent = new Intent(Intent.ACTION_VIEW, url);
            context.startActivity(intent);
        }
    });
    }

一样调用你的适配器
    DessertAdapter adapter = new DessertAdapter(AnimateToolbar.this, persons);
    rv.setAdapter(adapter);