package com.example.myapplication49.mvvm.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.hardware.lights.LightState;
import android.os.Bundle;
import com.example.myapplication49.R;
import com.example.myapplication49.databinding.ActivityVideoBinding;
import com.example.myapplication49.mvvm.adapter.VideoAdapter;
import com.example.myapplication49.mvvm.entity.VideoEntity;
import com.example.myapplication49.mvvm.viewmodel.VideoViewModel;
import java.util.ArrayList;
import java.util.List;
public class VideoActivity extends AppCompatActivity {
ActivityVideoBinding mainBinding;
VideoAdapter videoAdapter;
private List<VideoEntity.DataBean> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_video);
mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_video);
VideoViewModel videoViewModel = new ViewModelProvider(this).get(VideoViewModel.class);
//启动mvvm
videoViewModel.getFood(8,10);
videoViewModel.videoLiveData.observe(this, new Observer<VideoEntity>() {
@Override
public void onChanged(VideoEntity videoEntity) {
List<VideoEntity.DataBean> data = videoEntity.getData();
videoAdapter = new VideoAdapter(R.layout.item,data);
mainBinding.videoRv.setLayoutManager(new LinearLayoutManager(VideoActivity.this));
mainBinding.videoRv.setAdapter(videoAdapter);
videoAdapter.notifyDataSetChanged();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data></data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mvvm.view.VideoActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/video_rv"/>
</LinearLayout>
</layout>
package com.example.myapplication49.mvvm.http;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitManager {
//私有构造
private RetrofitManager(){
}
//申明变量
private static RetrofitManager retrofitManager = null;
//对外提供接口
public static RetrofitManager getRetrofitManager() {
if (retrofitManager == null) {
retrofitManager = new RetrofitManager();
}
return retrofitManager;
}
private Retrofit retrofit;
public Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = createRetrofit();
}
return retrofit;
}
private Retrofit createRetrofit(){
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5,TimeUnit.MINUTES)
.readTimeout(5,TimeUnit.MINUTES)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://10.161.9.80:7012/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
package com.example.myapplication49.mvvm.viewmodel;
import android.util.Log;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.myapplication49.mvvm.entity.VideoEntity;
import com.example.myapplication49.mvvm.model.VideoModel;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class VideoViewModel extends ViewModel {
private VideoModel videoModel;
public MutableLiveData<VideoEntity> videoLiveData = new MutableLiveData<>();
public VideoViewModel() {
videoModel = new VideoModel();
}
public void getFood(int currentPage,int pageSize){
videoModel.getVideoData(currentPage, pageSize)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<VideoEntity>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(VideoEntity videoEntity) {
Log.i("TagA","onNext:"+videoEntity.getCode());
videoLiveData.postValue(videoEntity);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}