RecyclerView(侧滑删除)和下载文件
作者:互联网
@TOC
一、RecyclerView(侧滑删除)
1.SwipeLayout概念:可以支持侧滑的布局控件
常用方法
设置侧滑方式 :setMoe(SwipeLayout.show.PULLOUT)
setMoe(SwipeLayout.show.LAYDOUN)
打开 :openItem(下标)
关闭 :closeItem(下标)
判断是否打开 :isopen(下标)
获得所有已经打开的条目:getopenItem()
注意事项:
1.要写在布局的位置,替换线性或相对布局
2.SwipeLayout嵌套两个子布局,第一个显示在屏幕外,第二个显示内容
3.继承RecyclerSwipeAdapter这个类
效果图
2.代码实例
//主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
tools:context="com.bw.day17.Main17Activity">
<android.support.v7.widget.RecyclerView
android:id="@+id/mrecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
//行布局
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipe_layout">
<LinearLayout
android:background="#66ddff00"
android:id="@+id/bottom_wrapper"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bottom"
android:text="删除"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bottom1"
android:text="收藏"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
package com.bw.day17;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import day01.bw.com.myapplication.R;
public class Main17Activity extends AppCompatActivity {
private RecyclerView mrecycleView;
private List<String> list;
private MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17);
mrecycleView=findViewById(R.id.mrecycleView);
list=new ArrayList<String>();
for(int i=0;i<9;i++){
list.add("条目"+i);
}
adapter=new MyRecyclerViewAdapter(this,list);
mrecycleView.setLayoutManager(new LinearLayoutManager(this));
mrecycleView.setAdapter(adapter);
}
}
package com.bw.day17;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;
import java.util.List;
import day01.bw.com.myapplication.R;
public class MyRecyclerViewAdapter extends RecyclerSwipeAdapter<MyRecyclerViewAdapter.MyViewHolder> {
private Context context;
private List<String> list;
public MyRecyclerViewAdapter(Context context,List<String> list){
this.context=context;
this.list=list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.zuohua_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder,final int position) {
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
viewHolder.surface.setText(list.get(position));
viewHolder.bottom.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getSwipeLayoutResourceId(int position) {
return position;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private SwipeLayout swipeLayout;
private Button bottom;
private TextView surface;
public MyViewHolder(View itemView) {
super(itemView);
swipeLayout = itemView.findViewById(R.id.swipe_layout);
bottom = itemView.findViewById(R.id.bottom);
surface = itemView.findViewById(R.id.surface);
}
}
}
一、RecyclerView(侧滑删除)
效果图
package bw.com.breakpointresume;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutionException;
/**
* 指定位置的下载
*
* 2.显示下载进度 总长度 当前进度
*
* 3.分批写入文件
* */
public class MainActivity extends AppCompatActivity {
long start = 0;
long end = 1024*1024;//要下载多少
int max;//给总进度
Button start_btn;
Button pare_btn;
Button button;
ProgressBar progressBar;
TextView textView;
int time = 1;//第几次下载,一共下载5次
boolean flag = false;
int sum = 0;
int num = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100 && msg.obj!= null){
if (time < num){
new LoadFileThread().start();
progressBar.setProgress((int) end);
time++;
}else {
//不下了发了一个空消息
handler.sendEmptyMessage(200);
progressBar.setProgress(max);
}
textView.setText(msg.obj.toString());
Log.e("@@@",msg.obj.toString());
}else if (msg.what == 200){
textView.setText("下载完成");
}else if (msg.what == 300){
progressBar.setProgress(msg.arg1);
}
}
};
Handler handler2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start_btn = findViewById(R.id.button_start);
pare_btn = findViewById(R.id.button_parse);
button = findViewById(R.id.start_btn);
progressBar = findViewById(R.id.pb);
try {
max = new MyThread().execute("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").get();
num = (int) (max/end);
progressBar.setMax(max);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
textView = findViewById(R.id.tv);
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new LoadFileThread().start();
}
});
//暂停
pare_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = true;
Message message = Message.obtain();
message.obj = flag;
handler2.sendMessage(message);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = false;
Message message = Message.obtain();
message.obj = flag;
handler2.sendMessage(message);
}
});
}
class MyThread extends AsyncTask<String,String, Integer> {
@Override
protected Integer doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode() == 200){
return httpURLConnection.getContentLength();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
class LoadFileThread extends Thread{
@Override
public void run() {
super.run();
Looper.prepare();//开启
RandomAccessFile randomAccessFile = null;
InputStream is = null;
try {
URL url = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
//存文件的位置
String path = "";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
path = Environment.getExternalStorageDirectory().getPath()+"/test.mp4";
}
randomAccessFile = new RandomAccessFile(path,"rw");
randomAccessFile.seek(start);
httpURLConnection.setRequestProperty("Range","bytes="+start+"-"+end);
if (httpURLConnection.getResponseCode() == 206){
Log.e("###","来了老弟");
// max = httpURLConnection.getContentLength();//总长度 Range byte = 0-1024*1024
// Log.e("MAX",max+"");
// progressBar.setMax(max);
is = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes))!=-1){
randomAccessFile.write(bytes,0,len);
}
sum+=len;
}
//从上次结束的位置+1开始读
start = end+1;
end += 1024*1024;
handler2 = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
boolean flag = Boolean.parseBoolean(msg.obj.toString());
if (flag){
Message message = Message.obtain();
message.what = 300;
message.arg1 = (int) end;
handler.sendMessage(message);
}else {
Log.e("start",start+"");
Log.e("end",end+"");
Message message = Message.obtain();
message.what = 100;//编号
message.obj = "文件第"+time+"次下载成功:"+start+"-"+end;
message.arg1 = (int) start;
handler.sendMessage(message);
}
}
};
Log.e("start",start+"");
Log.e("end",end+"");
Message message = Message.obtain();
message.what = 100;//编号
message.obj = "文件第"+time+"次下载成功:"+start+"-"+end;
message.arg1 = (int) start;
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (randomAccessFile != null){
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Looper.loop();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="30dp"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始下载"
/>
<Button
android:id="@+id/button_parse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停下载"
/>
<Button
android:id="@+id/start_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="继续下载"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
</LinearLayout>
标签:侧滑,删除,int,public,start,import,android,RecyclerView,message 来源: https://blog.csdn.net/weixin_43863384/article/details/93915654