xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.baway.liuheng.dialog.activity.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/sfl"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
android.support.v4.widget.SwipeRefreshLayout>
RelativeLayout>
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="100dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="0%"/>
LinearLayout>
LinearLayout>
LinearLayout>
view
package com.baway.liuheng.dialog.view;
import com.baway.liuheng.dialog.bean.UserBean;
import java.util.List;
public interface RecyView {
void showRecy(List ub);
}
model
package com.baway.liuheng.dialog.model;
import com.baway.liuheng.dialog.presenter.IRecyPresenter;
public interface IRecyModel {
void recy(int type, IRecyPresenter rp);
}
package com.baway.liuheng.dialog.model;
import android.util.Log;
import com.baway.liuheng.dialog.bean.UserBean;
import com.baway.liuheng.dialog.inter.APIservice;
import com.baway.liuheng.dialog.inter.APi;
import com.baway.liuheng.dialog.presenter.IRecyPresenter;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import org.reactivestreams.Publisher;
import java.util.List;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by lenovo on 2017/11/11.
*/
public class RecyModel implements IRecyModel {
@Override
public void recy(int type, final IRecyPresenter rp) {
Retrofit build = new Retrofit.Builder().baseUrl(APi.PATH)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
APIservice apIservice = build.create(APIservice.class);
Flowable> data = apIservice.getData(type);
data.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) //主线程
.flatMap(new Function, Publisher>() {
@Override
public Publisher apply(List userBeen) throws Exception {
return Flowable.fromIterable(userBeen);
}
})
.subscribeWith(new DisposableSubscriber() {
@Override
public void onNext(UserBean userBean) {
List been = userBean.getData();
Log.i("====-=-=-=-=-",been.toString());
rp.show(been);
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
}
}
API
package com.baway.liuheng.dialog.inter;
/**
* Created by lenovo on 2017/11/11.
*/
public class APi {
public static final String PATH="http://mnews.gw.com.cn/wap/data/news/txs/";
}
API
package com.baway.liuheng.dialog.inter;
import com.baway.liuheng.dialog.bean.UserBean;
import java.util.List;
import io.reactivex.Flowable;
import retrofit2.http.GET;
import retrofit2.http.Path;
/**
* Created by lenovo on 2017/11/11.
*/
public interface APIservice {
@GET("page_{type}.json")
Flowable> getData(@Path("type") int a);
}
PRESENTER
package com.baway.liuheng.dialog.presenter;
import com.baway.liuheng.dialog.bean.UserBean;
import java.util.List;
/**
* Created by lenovo on 2017/11/11.
*/
public interface IRecyPresenter {
void show(List ub);
void showModel(int type);
}
PRESENTER
package com.baway.liuheng.dialog.presenter;
import com.baway.liuheng.dialog.bean.UserBean;
import com.baway.liuheng.dialog.model.IRecyModel;
import com.baway.liuheng.dialog.model.RecyModel;
import com.baway.liuheng.dialog.view.RecyView;
import java.util.List;
/**
* Created by lenovo on 2017/11/11.
*/
public class RecyPresenter implements IRecyPresenter {
RecyView view;
IRecyModel model;
public RecyPresenter(RecyView view) {
this.view = view;
model=new RecyModel();
}
@Override
public void show(List ub) {
view.showRecy(ub);
}
@Override
public void showModel(int type) {
model.recy(type,this);
}
}
BEAN
package com.baway.liuheng.dialog.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* Created by lenovo on 2017/11/11.
*/
@Entity
public class ImgBean {
@Id(autoincrement = true)
Long Img;
String Url;
public String getUrl() {
return this.Url;
}
public void setUrl(String Url) {
this.Url = Url;
}
public Long getImg() {
return this.Img;
}
public void setImg(Long Img) {
this.Img = Img;
}
@Generated(hash = 1351764128)
public ImgBean(Long Img, String Url) {
this.Img = Img;
this.Url = Url;
}
@Generated(hash = 2027780091)
public ImgBean() {
}
}
ADAPTER
package com.baway.liuheng.dialog.adapter;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.baway.liuheng.dialog.R;
import com.baway.liuheng.dialog.bean.UserBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* Created by lenovo on 2017/11/11.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private Context context;
private List list;
public interface OnLintener{
void setOnLintener(int position, View view, View view1, View view2);
}
OnLintener onLintener;
public void setOnLintener(OnLintener onLintener) {
this.onLintener = onLintener;
}
public MyAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
String img = list.get(position).getImg();
Uri uri = Uri.parse(img);
holder.img.setImageURI(uri);
holder.title.setText(list.get(position).getTitle());
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLintener.setOnLintener(position,holder.ll,holder.sb,holder.tv);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final SimpleDraweeView img;
private final LinearLayout ll;
private final SeekBar sb;
private final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
img = (SimpleDraweeView)itemView.findViewById(R.id.img);
title = (TextView)itemView.findViewById(R.id.title);
ll = (LinearLayout)itemView.findViewById(R.id.ll);
sb = (SeekBar)itemView.findViewById(R.id.sb);
tv = (TextView)itemView.findViewById(R.id.tv);
}
}
}
ACTIVITY
package com.baway.liuheng.dialog.activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.baway.liuheng.dialog.greendao.ImgBeanDao;
import com.baway.liuheng.dialog.R;
import com.baway.liuheng.dialog.adapter.MyAdapter;
import com.baway.liuheng.dialog.bean.ImgBean;
import com.baway.liuheng.dialog.bean.UserBean;
import com.baway.liuheng.dialog.myapp.MyApp;
import com.baway.liuheng.dialog.presenter.RecyPresenter;
import com.baway.liuheng.dialog.view.RecyView;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import zlc.season.rxdownload2.RxDownload;
import zlc.season.rxdownload2.entity.DownloadStatus;
public class MainActivity extends AppCompatActivity implements RecyView {
private RecyPresenter presenter;
private RecyclerView rlv;
private SwipeRefreshLayout sfl;
int a=1;
private ImgBeanDao dao;
private RxDownload rxDownload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
presenter = new RecyPresenter(this);
presenter.showModel(a);
dao = MyApp.getApplication().getDaoSession().getImgBeanDao();
//配置断点续传
rxDownload = RxDownload.getInstance(this).defaultSavePath(Environment.getExternalStorageDirectory() + "/LH").maxThread(3);
}
private void initView() {
rlv = (RecyclerView) findViewById(R.id.rlv);
rlv.setLayoutManager(new LinearLayoutManager(this));
//刷新条目
sfl = (SwipeRefreshLayout) findViewById(R.id.sfl);
sfl.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimaryDark);
sfl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sfl.setRefreshing(false);
a++;
presenter.showModel(a);
}
},2000);
}
});
}
//回传数据
@Override
public void showRecy(List ub) {
MyAdapter myAdapter = new MyAdapter(this, ub);
rlv.setAdapter(myAdapter);
for (int i = 0; i new MyAdapter.OnLintener() {
@Override
public void setOnLintener(int position, View view, View view1, View view2) {
xiazai(query().get(position).getUrl(),(LinearLayout)view,(SeekBar)view1,(TextView)view2);
}
});
}
private void xiazai(String url, final LinearLayout view, final SeekBar view1, final TextView view2) {
Disposable disposable = rxDownload
.download(url) //只传url即可
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(DownloadStatus status) throws Exception {
//DownloadStatus为下载进度
view.setVisibility(View.VISIBLE);
double l = status.getDownloadSize() * 100 / status.getTotalSize();
view1.setProgress((int) l);
view2.setText(l+"%");
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) throws Exception {
//下载失败
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.setVisibility(View.GONE);
}
},2000);
Toast.makeText(MainActivity.this,"下载失败",Toast.LENGTH_SHORT).show();
}
}, new Action() {
@Override
public void run() throws Exception {
//下载成功
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.setVisibility(View.GONE);
}
},2000);
Toast.makeText(MainActivity.this,"下载成功",Toast.LENGTH_SHORT).show();
}
});
}
//插入数据
public void add(String Url){
ImgBean imgBean = new ImgBean();
imgBean.setUrl(Url);
dao.insert(imgBean);
}
//查询数据
public List query(){
List img=dao.loadAll();
return img;
}
}
myapp
package com.baway.liuheng.dialog.myapp;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import com.baway.liuheng.dialog.greendao.DaoMaster;
import com.baway.liuheng.dialog.greendao.DaoSession;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* Created by lenovo on 2017/11/11.
*/
public class MyApp extends Application {
private static MyApp application;
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase db;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
@Override
public void onCreate() {
super.onCreate();
application = this;
setDatabase();
Fresco.initialize(this);
}
public static MyApp getApplication() {
return application;
}
/** * 设置greenDao */
private void setDatabase() {
// 通过DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的SQLiteOpenHelper 对象。
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为greenDAO 已经帮你做了。
// 注意:默认的DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
mHelper = new DaoMaster.DevOpenHelper(this,"notes-db", null);
db =mHelper.getWritableDatabase();
// 注意:该数据库连接属于DaoMaster,所以多个 Session 指的是相同的数据库连接。
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession() {
return mDaoSession;
}
public SQLiteDatabase getDb() {
return db;
}
}
QUANXIAN
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'io.reactivex.rxjava2:rxjava:2.0.0-RC3'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0-RC3'
compile 'io.reactivex.rxjava2:rxandroid:2.0.0-RC1'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile'org.greenrobot:greendao:3.1.0'
compile'org.greenrobot:greendao-generator:3.1.0'
compile 'zlc.season:rxdownload2:2.0.4'
<uses-permission android:name="android.permission.INTERNET"/>
android:name=".myapp.MyApp"
GREENDAO的集成
classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
apply plugin:'org.greenrobot.greendao'
greendao{
schemaVersion 1
daoPackage'com.baway.liuheng.dialog'
targetGenDir'src/main/java'
}
compile'org.greenrobot:greendao:3.1.0'
compile'org.greenrobot:greendao-generator:3.1.0'