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


关于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.

  • Gsoncom.squareup.retrofit2:converter-gson
  • Jacksoncom.squareup.retrofit2:converter-jackson
  • Moshicom.squareup.retrofit2:converter-moshi
  • Protobufcom.squareup.retrofit2:converter-protobuf
  • Wirecom.squareup.retrofit2:converter-wire
  • Simple XMLcom.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"

retrofit-spring-boot-starter是一个用于整合Retrofit库和Spring Boot的starter项目,它可以简化在Spring Boot中使用Retrofit的配置和使用。 以下是retrofit-spring-boot-starter的使用方法: 1. 在你的Spring Boot项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.lianjiatech</groupId> <artifactId>retrofit-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> ``` 2. 创建一个接口,用于定义Retrofit的API接口。例如: ```java import retrofit2.http.GET; import retrofit2.http.Path; public interface MyApi { @GET("/users/{username}") User getUser(@Path("username") String username); } ``` 3. 在你的Spring Boot应用程序中,使用`@Autowired`注解将Retrofit的API接口注入到你的代码中。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import retrofit2.Retrofit; @Service public class MyService { private final MyApi myApi; @Autowired public MyService(Retrofit retrofit) { this.myApi = retrofit.create(MyApi.class); } public User getUser(String username) { return myApi.getUser(username); } } ``` 4. 现在你可以在你的代码中使用`MyService`来调用Retrofit的API接口了。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @GetMapping("/users/{username}") public User getUser(@PathVariable String username) { return myService.getUser(username); } } ``` 以上是retrofit-spring-boot-starter的基本用法。你可以根据自己的需求进行配置和使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值