//依赖
implementation ‘com.squareup.okhttp3:okhttp:3.7.0’
implementation ‘com.squareup.okio:okio:1.12.0’
implementation ‘com.squareup.retrofit2:retrofit:2.0.2’
implementation ‘com.squareup.retrofit2:converter-gson:2.1.0’
implementation ‘com.squareup.okhttp3:logging-interceptor:3.11.0’
implementation ‘io.reactivex.rxjava2:rxjava:2.0.2’
implementation ‘com.squareup.retrofit2:adapter-rxjava2:2.3.0’
implementation ‘io.reactivex.rxjava2:rxandroid:2.0.2’
implementation’com.youth.banner:banner:1.4.9’
implementation ‘com.github.bumptech.glide:glide:4.8.0’
implementation ‘org.greenrobot:greendao:3.2.2’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
implementation ‘com.facebook.fresco:fresco:0.14.1’
implementation ‘org.greenrobot:eventbus:3.1.1’
//Api
public interface Api {
@GET Observable<ResponseBody> get(@Url String url); @POST Observable<ResponseBody> getData(@Url String url, @FieldMap Map<String,String> map);
}
//Retrofits
public class Retrofits {
public final Api api;
private Retrofits(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://172.17.8.100/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
api = retrofit.create(Api.class);
}
static class RetrofitUtils{
private static Retrofits retrofits=new Retrofits();
}
public static Retrofits getInstance(){
return RetrofitUtils.retrofits;
}
}
//IMainView
public interface IMainView {
void onSuccess(Object o);
}
//MainActivity
public class MainActivity extends AppCompatActivity implements IMainView{
private Presenter presenter;
private Banner banner;
private RecyclerView recy_view;
private MyAdapter myAdapter;
private DaoMaster.DevOpenHelper helper;
private SQLiteDatabase database;
private DaoMaster master;
private DaoSession session;
private UserDao user;
private User user1;
@Override
protected void onCreate(Bundle savedInstanceState) {
Fresco.initialize(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = findViewById(R.id.bannerView);
recy_view = findViewById(R.id.recy_view);
user1 = new User();
creataDataBase();
initData();
}
private void initData() {
recy_view.setLayoutManager(new GridLayoutManager(this, 2));
recy_view.addItemDecoration(new DividerItemDecoration(MainActivity.this, GridLayoutManager.HORIZONTAL));
recy_view.addItemDecoration(new DividerItemDecoration(MainActivity.this, GridLayoutManager.VERTICAL));
myAdapter = new MyAdapter(this);
recy_view.setAdapter(myAdapter);
boolean b = Work.newwork(this);
if (b) {
presenter = new Presenter(this);
presenter.get("small/commodity/v1/bannerShow",BannerBeans.class);
presenter.get("movieApi/movie/v1/findReleaseMovieList?page=1&count=20",UserBeans.class);
} else {//失败显示数据库中的数据
//创建集合
List<UserBeans.ResultBean> result = new ArrayList<>();
//List<BannerBeans.ResultBean> list_banner=new ArrayList<>();
//查询数据库并放入集合
List<User> list2 = user.queryBuilder().list();
for (int i = 0; i < list2.size(); i++) {
//创建对象
UserBeans.ResultBean resultBean = new UserBeans.ResultBean();
//
// BannerBeans.ResultBean resultBean = new BannerBeans.ResultBean();
//写入数据
resultBean.setName(list2.get(i).getName());
resultBean.setImageUrl(list2.get(i).getUrl());
resultBean.setReleaseTimeShow(list2.get(i).getData());
//id替换pid 重要的
resultBean.setId((int) list2.get(i).getId());
result.add(resultBean);
}
myAdapter.setData(result);
}
}
//创建数据库
private void creataDataBase() {
//创建表
helper = new DaoMaster.DevOpenHelper(this, "sport-db", null);
//写入数据
database = helper.getWritableDatabase();
master = new DaoMaster(database);
session = master.newSession();
user = session.getUserDao();
}
@Override
public void onSuccess(Object o) {
if (o instanceof BannerBeans){
BannerBeans bannerBeans = (BannerBeans) o;
List<BannerBeans.ResultBean> result = bannerBeans.getResult();
List<String> list = new ArrayList<>();
List<String> list2 = new ArrayList<>();
//设置图片网址或地址的集合
//设置轮播图的标题集合
for (int i = 0; i < result.size(); i++) {
String title = result.get(i).getTitle();
String imageUrl = result.get(i).getImageUrl();
list.add(imageUrl);
list2.add(title);
}
//设置内置样式
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//设置图片加载器,图片加载器在下方
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
});
//添加图片
banner.setImages(list);
//设置轮播的动画效果,内含多种特效,可点入方法内查找后内逐一体验
banner.setBannerAnimation(Transformer.ScaleInOut);
//设置文字
banner.setBannerTitles(list2);
//设置轮播间隔时间
banner.setDelayTime(3000);
//设置是否为自动轮播,默认是“是”。
//banner.isAutoPlay(true);
//设置指示器的位置,小点点,左中右。
banner.start();
}else{
UserBeans userBeans= (UserBeans) o;
List<UserBeans.ResultBean> result = userBeans.getResult();
Log.i("ssss", "onSuccess: " + result);
for (int i = 0; i < result.size(); i++) {
user1.setName(result.get(i).getName());
user1.setUrl(result.get(i).getImageUrl());
user1.setData(result.get(i).getReleaseTimeShow());
user1.setId(result.get(i).getId());
// 添加或者替换
user.insertOrReplace(user1);
}
myAdapter.setData(result);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestory();
}
}
//IModel
public interface IMaodel {
void get(String url,Class aClass,NewCallBack newCallBack);
public interface NewCallBack{
void onSuccess(Object o);
}
}
//Model
public class Model implements IMaodel{
private final Retrofits instance;
public Model(){
instance = Retrofits.getInstance();
}
@SuppressLint("CheckResult")
@Override
public void get(String url, final Class aClass, final NewCallBack newCallBack) {
instance.api.get(url)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody body) throws Exception {
Gson gson = new Gson();
Object o = gson.fromJson(body.string(), aClass);
newCallBack.onSuccess(o);
}
});
}
}
//IPresenter
public interface IPresenter {
void get(String url,Class aClass);
}
//Presenter
public class Presenter implements IPresenter{
private Model model;
IMainView iMainView ;
public Presenter(IMainView iMainView){
model = new Model();
this.iMainView=iMainView;
}
@Override
public void get(String url, Class aClass) {
model.get(url, aClass, new IMaodel.NewCallBack() {
@Override
public void onSuccess(Object o) {
iMainView.onSuccess(o);
}
});
}
public void onDestory(){
if (model!=null){
model=null;
}
if (iMainView!=null){
iMainView=null;
}
}
}
//adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
MainActivity mainActivity;
public MyAdapter(MainActivity mainActivity) {
this.mainActivity=mainActivity;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mainActivity).inflate(R.layout.item_layout, parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.text_name.setText(result.get(position).getName());
holder.text_price.setText(result.get(position).getReleaseTimeShow());
//Glide.with(mainActivity).load(result.get(position).getImageUrl()).into(holder.img_view);
holder.simp_view.setImageURI(Uri.parse(result.get(position).getImageUrl()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().postSticky(result.get(position).getName());
Toast.makeText(mainActivity,""+result.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return result.size();
}
List<UserBeans.ResultBean> result=new ArrayList<>();
public void setData(List<UserBeans.ResultBean> result) {
Log.i("nqq", "setData: 调用 set==="+result);
if (result!=null){
Log.i("nqq", "setData: 调用 set c传值");
this.result.clear();
this.result.addAll(result);
notifyDataSetChanged();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
//private final ImageView img_view;
private final TextView text_name;
private final TextView text_price;
private final SimpleDraweeView simp_view;
public ViewHolder(View itemView) {
super(itemView);
//img_view = itemView.findViewById(R.id.img_view);
simp_view = itemView.findViewById(R.id.simp_view);
text_name = itemView.findViewById(R.id.text_name);
text_price = itemView.findViewById(R.id.text_price);
}
}
}
//网络判断
public class Work {
//判断网络
public static boolean newwork(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isAvailable();
}
}
//Utils工具类
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}