MvvM视频页面

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() {

                    }
                });
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值