Adpter_showadpter
public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.MyViewHolder> implements View.OnClickListener{
private Context context;
private List<Beans.DataBean> chalist;
//利用接口回調(diào)實(shí)現(xiàn)點(diǎn)擊事件
//再實(shí)例,然后創(chuàng)建方法
private OnItemClickListener mOnItemClickListener = null;
//點(diǎn)擊事件
@Override
public void onClick(View view) {
if (mOnItemClickListener!=null){
mOnItemClickListener.onItemClick(view, (Integer) view.getTag());
}
}
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
public ShowAdapter(Context context, List<Beans.DataBean> chalist) {
this.context = context;
this.chalist = chalist;
Log.i("33333", "onBindViewHolder: "+chalist.size());
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.rcy_item_line,null);
MyViewHolder holder=new MyViewHolder(view);
//調(diào)用點(diǎn)擊
view.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(chalist.get(position).getContent());
// String[] split = sousuolist.get(position).getImages().split("\\|");
// DraweeController ff= Fresco.newDraweeControllerBuilder()
// .setUri(String.valueOf(split[0]))
// .setAutoPlayAnimations(true)
// .build();
// holder.iv.setController(ff);
// String s =chalist.get(position).getList().get(0).getImages().split("\\|")[0];
DraweeController ff= Fresco.newDraweeControllerBuilder()
.setUri(chalist.get(position).getImage_url())
.setAutoPlayAnimations(true)
.build();
holder.iv.setController(ff);
//把條目的下標(biāo)position設(shè)置給holder
holder.itemView.setTag(position);
//ImageLoader.getInstance().displayImage(list.get(position).getList().get(0).getImages(),holder.iv);
}
public void setmOnItemClickListener(OnItemClickListener listener){
this.mOnItemClickListener=listener;
}
@Override
public int getItemCount() {
return chalist.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
public TextView tv;
public SimpleDraweeView iv;
public MyViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.img);
}
}
}
Img
public class Img extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context)
.load(path)
.into(imageView);
}
}
model
public interface IModel {
void setObSerVer(Observer obSerVer);
}
public class MyModel implements IModel{
@Override
public void setObSerVer(Observer obSerVer) {
OkHttpClient okclient = new OkHttpClient.Builder().build();
RetrofitManager.getInstance(RequestApi.BASE_URL,okclient)
.setCreate(RequestApi.class)
.getData()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(obSerVer);
}
}
Presenter
public class MyPresenter {
IModel model;
Iview view;
public MyPresenter(Iview view) {
this.view = view;
model=new MyModel();
}
public void loadData(){
model.setObSerVer(new Observer<Beans>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
// Log.i("http:///", "onError: "+e);
}
@Override
public void onNext(Beans beans ) {
List<Beans.DataBean> data = beans.getData();
// Log.i("http://///", "onNext: "+list.size());
view.setData(data);
}
});
}
}
view
public interface Iview {
void setData(List<Beans.DataBean> beanList);
}
utils_Api
public class Api {
public static final String BanUrl="http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio";
}
utils_DialogUtils
public class DialogUtils {
public static long MAX_SIZE = 0;
public static long PROGRESS = -2;
// private static File file;
private static String path;
public static void showUpdataDialog(final Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("是否下載")
.setMessage("下載吧")
.setNegativeButton("以后再說(shuō)", null)
.setPositiveButton("立即下載", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// path= context.getCacheDir() + "/uri=vedio";
// new DownloadThread("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio", context.getCacheDir() + "/uri=vedio").start();
path= context.getCacheDir() + "/911Mothers_2010W-480p.mp4";
new DownloadThread("http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4", context.getCacheDir() + "/911Mothers_2010W-480p.mp4").start();
showProgress(context);
}
}).show();
}
public static interface autopPlay {
void autopplay(String path);
}
public static autopPlay play;
public static void setPlay(autopPlay autopPlay){
play=autopPlay;
};
public static void showProgress(final Context context){
final ProgressDialog pd = new ProgressDialog(context);
pd.setTitle("正在下載");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.show();
new AsyncTask<String, Integer, String>(){
@Override
protected String doInBackground(String... strings) {
while (PROGRESS+1 < MAX_SIZE){
SystemClock.sleep(100);
if (MAX_SIZE>0){
publishProgress((int)(PROGRESS * 100 / MAX_SIZE));
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
play.autopplay(path);
// file=new File(context.getCacheDir() + "/911Mothers_2010W-480p.mp4");
// openApi(context,file);
Toast.makeText(context,"下載完成",Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
pd.setProgress(values[0]);
}
}.execute();
}
}
utils_RequestApi
public static final String BASE_URL = "http://result.eolinker.com/";
@GET("iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio")
Observable<Beans> getData();
utils_RetrofitManager
public class RetrofitManager {
private Retrofit mRetrofit;
private String baseUrl;
OkHttpClient client;
private static RetrofitManager mRetrofitManager;
private RetrofitManager(String baseUrl,OkHttpClient client){
this.baseUrl=baseUrl;
this.client=client;
initRetrofit();
}
public static synchronized RetrofitManager getInstance(String baseUrl,OkHttpClient client){
if (mRetrofitManager == null){
mRetrofitManager = new RetrofitManager(baseUrl,client);
}
return mRetrofitManager;
}
private void initRetrofit() {
mRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
}
public <T> T setCreate(Class<T> reqServer){
return mRetrofit.create(reqServer);
}
}
activity
public class ShowActivity extends AppCompatActivity implements Iview {
private Banner mBan;
List<String> list = new ArrayList<>();
private RecyclerView mRecy;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化 Fresco
Fresco.initialize(this);
setContentView(R.layout.activity_show);
//隱藏標(biāo)題
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
initView();
okUrlBanner();
MyPresenter presenter = new MyPresenter(this);
presenter.loadData();
}
private void initView() {
mBan = (Banner) findViewById(R.id.ban);
mRecy = (RecyclerView) findViewById(R.id.recy);
videoView= (VideoView) findViewById(R.id.video_view);
//創(chuàng)建布局管理器
LinearLayoutManager ll = new LinearLayoutManager(this);
mRecy.setLayoutManager(ll);
}
//Banner輪播
private void okUrlBanner() {
OkHttpUtils.getInstance().doGet(Api.BanUrl, new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) throws IOException {
Gson gson = new Gson();
Beans beans = gson.fromJson(result, Beans.class);
for (int i = 0; i < beans.getData().size(); i++) {
list.add(beans.getData().get(i).getImage_url());
}
mBan.setImageLoader(new Img());
mBan.setImages(list);
mBan.start();
}
});
}
//請(qǐng)求數(shù)據(jù)
@Override
public void setData(List<Beans.DataBean> beanList) {
//創(chuàng)建適配器
ShowAdapter showAdapter = new ShowAdapter(ShowActivity.this, beanList);
mRecy.setAdapter(showAdapter);
//條目點(diǎn)擊
showAdapter.setmOnItemClickListener(new ShowAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(ShowActivity.this,"點(diǎn)擊下載播放",Toast.LENGTH_SHORT).show();
DialogUtils.showUpdataDialog(ShowActivity.this);
DialogUtils.setPlay(new DialogUtils.autopPlay() {
@Override
public void autopplay(String path) {
//播放視頻
Uri uri = Uri.parse(path);
videoView.setMediaController(new MediaController(ShowActivity.this));
videoView.setVideoURI(uri);
videoView.start();
videoView.requestFocus();
}
});
}
});
}
}
net_DownloadTask
public class DownloadTask extends Thread {
String downloadUrl;
String path;
int blockSize;
int startPosition;
public DownloadTask(String downloadUrl, String path, int blockSize, int startPosition) {
this.downloadUrl = downloadUrl;
this.path = path;
this.blockSize = blockSize;
this.startPosition = startPosition;
}
@Override
public void run() {
NetUtils.downloadFile(downloadUrl,path,blockSize,startPosition);
}
}
net_DownloadThread
public class DownloadThread extends Thread{
private String downloadUrl = "";
private String path;
private int threadNum = 4;
public DownloadThread(String downloadUrl, String path) {
this.downloadUrl = downloadUrl;
this.path = path;
}
@Override
public void run() {
int len = NetUtils.getFileLength(downloadUrl);
DialogUtils.MAX_SIZE = len;
//例如 1000kb 3 333
int blockSize = len/threadNum;
// //四舍五入--- 讓一個(gè)數(shù)字+0决侈。5在四舍五入 就變成 向上取整
// int blockSize = (int) Math.round(tempSize + 0.5);
//計(jì)算出下載塊以后 創(chuàng)建線程執(zhí)行下載操作
for (int i = 0; i < threadNum; i++) {
//計(jì)算開(kāi)始位置
int startPosition = blockSize * i;
//讓最后一個(gè)線程下載的大小是正好的延塑, 總長(zhǎng)度 - 除了最后一個(gè)塊的大小和
if(i == threadNum - 1){
blockSize = len - blockSize * (threadNum - 1);
}
new DownloadTask(downloadUrl, path, blockSize, startPosition).start();
}
}
public void setThreadNum(int threadNum){
this.threadNum = threadNum;
}
}
net_NetUtils
public class NetUtils {
public static void downloadFile(String downloadUrl, String path, int blockSize, int startPosition){
BufferedInputStream bis = null;
RandomAccessFile raf = null;
try {
File f = new File(path);
if(!f.exists()){
f.createNewFile();
}
URL url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);
// long start = 0;
if(blockSize > 0){
// //使用線程id來(lái)計(jì)算 當(dāng)前線程的開(kāi)始位置和結(jié)束位置
// start = blockSize * threadId;
long end = blockSize + startPosition - 1;
//多線程下載 需要告訴服務(wù)器我要請(qǐng)求的是哪部分內(nèi)容 需要寫(xiě)在請(qǐng)求頭里面
conn.setRequestProperty("Range", "bytes=" + startPosition + "-" + end);
Log.i(Thread.currentThread() + "======", "bytes=" + startPosition + "-" + end);
}
int code = conn.getResponseCode();
if(code < 400){
bis = new BufferedInputStream(conn.getInputStream());
raf = new RandomAccessFile(f, "rwd");
raf.seek(startPosition);
int len = 0;
byte[] buff = new byte[1024 * 8];
while((len = bis.read(buff)) != -1){
synchronized (NetUtils.class){
raf.write(buff, 0, len);
if ( DialogUtils.PROGRESS<0){
DialogUtils.PROGRESS = 0;
}
DialogUtils.PROGRESS += len;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static int getFileLength(String downloadUrl){
int len = 0;
try {
URL url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);
len = conn.getContentLength();
} catch (Exception e) {
e.printStackTrace();
}
return len;
}
}
mvp+banner+rxjava+retrofit+多線程下載
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)绳瘟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)雕憔,“玉大人,你說(shuō)我怎么就攤上這事糖声〗锉耍” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵蘸泻,是天一觀的道長(zhǎng)琉苇。 經(jīng)常有香客問(wèn)我,道長(zhǎng)悦施,這世上最難降的妖魔是什么并扇? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮抡诞,結(jié)果婚禮上穷蛹,老公的妹妹穿的比我還像新娘。我一直安慰自己昼汗,他們只是感情好俩莽,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著乔遮,像睡著了一般扮超。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蹋肮,一...
- 那天出刷,我揣著相機(jī)與錄音,去河邊找鬼坯辩。 笑死馁龟,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的漆魔。 我是一名探鬼主播坷檩,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼改抡!你這毒婦竟也來(lái)了矢炼?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤阿纤,失蹤者是張志新(化名)和其女友劉穎句灌,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡胰锌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年骗绕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片资昧。...
- 正文 年R本政府宣布践惑,位于F島的核電站腹泌,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏尔觉。R本人自食惡果不足惜凉袱,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侦铜。 院中可真熱鬧专甩,春花似錦、人聲如沸钉稍。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)贡未。三九已至种樱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間俊卤,已是汗流浹背嫩挤。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像狠怨,于是被迫代替她去往敵國(guó)和親约啊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 原文地址: http://www.reibang.com/p/4bbecd0bb027 概述 MVPArms 是一...
- 背景 斷點(diǎn)續(xù)傳下載一直是移動(dòng)開(kāi)發(fā)中必不可少的一項(xiàng)重要的技術(shù)佣赖,同樣的Rxjava和Retrofit的結(jié)合讓這個(gè)技術(shù)解...
- 前言 Kotlin+Retrofit+RxJava+MVP封裝(一)Kotlin+Retrofit+RxJava+...
- 前言 首先恰矩,對(duì)于MVP、RxJava還不了解的同學(xué)茵汰,請(qǐng)先閱讀這幾篇文章: Android MVP模式 簡(jiǎn)單易懂的介...
- Ruby定義帶塊的方法 Ruby中do ~end之間的部分稱(chēng)為塊枢里,也可以寫(xiě)為{..}。下面看一個(gè)簡(jiǎn)單例子蹂午,怎么自定...