AutoplayVideos

介绍:

在RecyclerView中自动播放或者暂停来自url中的视频。

运行效果:

使用说明:

Gradle

Step 1. 在项目的build.gradle 文件中添加jCenter

allprojects {
	repositories {
		jcenter()
	}
}

Step 2. 在app的build.gradle文件中添加依赖

dependencies {
	 compile 'com.allattentionhere:autoplayvideos:0.0.5'
}

在item的xml(single_card.xml)中添加AAH_VideoImage:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="64dp"
    android:layout_marginRight="64dp"
    android:layout_marginTop="8dp"
    >
 <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    android:layout_gravity="center"
    card_view:cardPreventCornerOverlap="false">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <com.allattentionhere.autoplayvideos.AAH_VideoImage
            android:layout_width="220dp"
            android:layout_height="220dp"/>
	    
        <TextView
            android:gravity="center"
            android:text="hello"
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
 </android.support.v7.widget.CardView>
</LinearLayout>

在Activity的布局文件 MainActivity.xml中添加AAH_CustomRecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="MissingPrefix"
    android:gravity="center">
<com.allattentionhere.autoplayvideos.AAH_CustomRecyclerView
        android:layout_gravity="center"
        android:id="@+id/rv_home"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
</LinearLayout>

设置Adapter需要:

  1. Adapter 需要继承 AAH_VideosAdapter.

  2. ViewHolder 需要继承 AAH_CustomViewHolder.

  3. 在onBindViewHolder方法中设置thumbnail的url和video url。

public class MyVideosAdapter extends AAH_VideosAdapter {
    private List<MyModel> list;
    Picasso picasso;
    public class MyViewHolder extends AAH_CustomViewHolder {
        TextView tv;
        public MyViewHolder(View x) {
            super(x);
            tv = ButterKnife.findById(x, R.id.tv);
        }
    }
    public MyVideosAdapter(List<MyModel> list_urls, Picasso p) {
        this.list = list_urls;
        this.picasso = p;
    }
    @Override
    public AAH_CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_card, parent, false);
        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(AAH_CustomViewHolder holder, int position) {
        ((MyViewHolder) holder).tv.setText(list.get(position).getName());
        //todo
        holder.setImageUrl(list.get(position).getImage_url());
        holder.setVideoUrl(list.get(position).getVideo_url());
        //load image/thumbnail into imageview
        if (list.get(position).getImage_url() != null && !list.get(position).getImage_url().isEmpty())
            picasso.load(holder.getImageUrl()).config(Bitmap.Config.RGB_565).into(holder.getAAH_ImageView());
    }
    
    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}

最后在设置adapter之前调用setActivity以及(可选)用代码滚动去初始化视频:

 //todo before setAdapter
    recyclerView.setActivity(this);
    recyclerView.setAdapter(mAdapter);
 //to init videos before scrolling
    recyclerView.smoothScrollBy(0,1);
    recyclerView.smoothScrollBy(0,-1);
已下载
0