当前位置: 代码迷 >> 综合 >> RecyclerView 视频播放列表 用到 retrofit-convert,fresco,SurfaceView
  详细解决方案

RecyclerView 视频播放列表 用到 retrofit-convert,fresco,SurfaceView

热度:61   发布时间:2023-12-16 16:26:07.0

关于recyclerView 需要
1:RecyclerView 需要设置排列的形式
2:RecyclerView 需要adapter ,并且adapter 里面必须要设置ViewHolder,在onCreateViewHolder 里面要填充一个item的layout
3:RecyclerView 的数据,来自Retrofit,
4:RecyclerView 里面的图片显示,使用到Fresco的SimpleDraweeView,这个可以通过setImageUri来添加网络的图片,并且还有缓存的效果。


设置3个列的GridLayout形式的recyclerView

        app:layoutManager="GridLayoutManager"

       app:spanCount="3"



注意:recycleView 的自定义属性的使用 要添加appNs
-------------------------------------------------------------------------------


7:RecycleView


首先在dependency 里面添加recycleView,retrofit-convert,fresco


或者直接在build.gradle文件里面的dependencies直接添加三句话:


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'
    compile 'com.squareup.retrofit: converter-gson:2.0.0-beta2'
    compile 'com.facebook.fresco:fresco:0.8.1'

Retrofit is the class through which your API interfaces are turned into callable objects. By default, Retrofit will give you sane defaults for your platform but it allows for customization.

CONVERTERS

By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.

Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.

  • Gson: com.squareup.retrofit2:converter-gson
  • Jackson: com.squareup.retrofit2:converter-jackson
  • Moshi: com.squareup.retrofit2:converter-moshi
  • Protobuf: com.squareup.retrofit2:converter-protobuf
  • Wire: com.squareup.retrofit2:converter-wire
  • Simple XML: com.squareup.retrofit2:converter-simplexml
  • Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars

Here's an example of using the GsonConverterFactory class to generate an implementation of the GitHubService interface which uses Gson for its deserialization.

Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com").addConverterFactory(GsonConverterFactory.create()).build();GitHubService service = retrofit.create(GitHubService.class);
CUSTOM CONVERTERS

If you need to communicate with an API that uses a content-format that Retrofit does not support out of the box (e.g. YAML, txt, custom format) or you wish to use a different library to implement an existing format, you can easily create your own converter. Create a class that extends the Converter.Factory class and pass in an instance when building your adapter.



新建一个BaseApplication:
public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}




新建一个HttpUtils 
public class HttpUtils  {
    public interface Service{
        public Call<>
    }
}




新建一个Entity


在浏览器中输入:http://m2.qiushibaike.com/article/list/video?page=
然后在拷贝里面的json 部分。
在android Studio 里面按alt+insert,点击GsonFormat
把刚才的内容拷贝进去,
然后点击ok ok




返回HttpUtils


public class HttpUtils  {
    public interface Service{
        @GET("article/list/video")
        Call<Entity> getVideo(@Query("page") int page);
    }
    private static Service service;
    static{
        service = new Retrofit.Builder().baseUrl("http://m2.qiushibaike.com/").addConverterFactory(GsonConverterFactory.create()).build().create(Service.class);
    }
    public static Service getService() {
        return service;
    }
}


下面写item的layout
为什么下面要有FrameLayout 的结构,是为了让两个一样大。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_content"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/item_video"/>
        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/item_pic"/>


    </FrameLayout>
</LinearLayout>


新建一个VideoAdapter,ViewHolder


public class VideoAdapter {
    public static class VideoViewHolder extends RecyclerView.ViewHolder{


        private final TextView content;
        private final SimpleDraweeView pic;
        private final SurfaceView video;


        public VideoViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.item_content);
            pic = (SimpleDraweeView) itemView.findViewById(R.id.item_pic);
            video = (SurfaceView) itemView.findViewById(R.id.item_video);
            pic.getHierarchy().setProgressBarImage(new ProgressBarDrawable());
pic.setAspectRatio(1);//设置高宽比 为 1
        }
    }
}


ViewHolder 做完,开始做Adapter


package com.example.xue.listmediareview;


import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.view.SimpleDraweeView;


import java.util.List;


/**
 * Created by XUE on 2016/1/8.
 */
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder>{
    private Context context;
    private List<Entity.ItemsEntity> list;


    public VideoAdapter(Context context, List<Entity.ItemsEntity> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        return new VideoViewHolder(view);
//        return null;
    }


    @Override
    public void onBindViewHolder(VideoViewHolder holder, int position) {
        Entity.ItemsEntity itemsEntity = list.get(position);
        holder.content.setText(itemsEntity.getContent());
        holder.pic.setImageURI(Uri.parse(itemsEntity.getPic_url()));


    }


    @Override
    public int getItemCount() {
//        return 0;
        return list.size();
    }


public void addAll(Collection<? extends Entity.ItemsEntity> collection){
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeChanged(size,collection.size());
    }


    public static class VideoViewHolder extends RecyclerView.ViewHolder{


        private final TextView content;
        private final SimpleDraweeView pic;
        private final SurfaceView video;


        public VideoViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.item_content);
            pic = (SimpleDraweeView) itemView.findViewById(R.id.item_pic);
            video = (SurfaceView) itemView.findViewById(R.id.item_video);
            pic.getHierarchy().setProgressBarImage(new ProgressBarDrawable());
pic.setAspectRatio(1);//设置高宽比 为 1
        }
    }
}




在mainxml 里面添加recycleView


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">




    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="LinearLayoutManager"
        android:id="@+id/recycle"
        >






    </RecyclerView>
</RelativeLayout>




修改mainActivity


public class MainActivity extends AppCompatActivity implements Callback<Entity> {


    private VideoAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle);
        adapter = new VideoAdapter(this, new ArrayList<Entity.ItemsEntity>());
        recyclerView.setAdapter(adapter);
        HttpUtils.getService().getVideo(1).enqueue(this);
    }


    @Override
    public void onResponse(Response<Entity> response, Retrofit retrofit) {
        adapter.addAll(response.body().getItems());
    }


    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
        Toast.makeText(this,"Fail",Toast.LENGTH_LONG).show();
    }
}






清单文件中添加:
<uses-permission android:name="android.permission.INTERNET"/>


application里面添加
        android:name=".BaseApplication"

  相关解决方案