专高二 day2(MVVM 初步接触)

http   接口数据

api

package com.example.work12_10.mvvm.http;



import com.example.work12_10.entity.VideoEntity;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface Api {
    @GET("/video/findVideos?")
    Observable<VideoEntity> getVideoData(@Query("currentPage")int currentPage, @Query("pageSize")int pageSize);
}

RetrofitUtils

package com.example.work12_10.mvvm.http;

import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {
    private RetrofitUtils() {
    }

    private static RetrofitUtils retrofitUtils = null;

    private Retrofit retrofit;

    public static RetrofitUtils getInstance() {
        if(retrofitUtils == null) {
            retrofitUtils = new RetrofitUtils();
        }
        return retrofitUtils;
    }

    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()
                .baseUrl("http://10.161.9.80:7005/")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }


}

model 解耦  

VideoModel

package com.example.work12_10.mvvm.model;

import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.http.Api;
import com.example.work12_10.mvvm.http.RetrofitUtils;

import io.reactivex.Observable;

public class VideoModel {
    protected Api api;

    public VideoModel() {
        api = RetrofitUtils.getInstance().getRetrofit().create(Api.class);
    }

    public Observable<VideoEntity> getVideoData(int currentPage, int pageSize) {
        return api.getVideoData(currentPage,pageSize);
    }
}

viewmodel   暂时没搞清楚实际作用(待改进)

VideoModel

package com.example.work12_10.mvvm.model;

import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.http.Api;
import com.example.work12_10.mvvm.http.RetrofitUtils;

import io.reactivex.Observable;

public class VideoModel {
    protected Api api;

    public VideoModel() {
        api = RetrofitUtils.getInstance().getRetrofit().create(Api.class);
    }

    public Observable<VideoEntity> getVideoData(int currentPage, int pageSize) {
        return api.getVideoData(currentPage,pageSize);
    }
}

view   视图层  展示数据

MainActivity

package com.example.work12_10.mvvm.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import com.example.work12_10.R;
import com.example.work12_10.adapter.MyVideoAdapter;
import com.example.work12_10.databinding.ActivityMainBinding;
import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.viewmodel.VideoViewModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding mainBinding;
    private List<VideoEntity.DataDTO> list = new ArrayList<>();
    private MyVideoAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        VideoViewModel videoViewModel = new ViewModelProvider(this).get(VideoViewModel.class);

        videoViewModel.getVideo(1,20);
        videoViewModel.videoEntityData.observe(this, videoEntity -> {
            list.addAll(videoEntity.getData());
            adapter = new MyVideoAdapter(R.layout.item_video,list);

            mainBinding.rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
            mainBinding.rv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        });

    }
}

  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值