Android侧滑和断点续传
作者:互联网
RecyclerView的侧滑
Swiplayout:可以支持侧滑的控件
注意:要写在布局的位置,替换线性或相对的布局
里面要写两个子布局
第一个:是侧滑的布局,默认显示在屏幕外
第二个:是要显示的布局;
自定义类继承RecyclerSwipelayout
重写getSwipelayoutResourceId(Postion)
常用方法:
setMode:设置侧滑的方式
openItem:打开
closeItem:关闭
适配器
// An highlighted block
public class MyswipAdapter extends RecyclerSwipeAdapter<MyswipAdapter.MyHolder>{
ArrayList<String> list;
public MyswipAdapter(ArrayList<String> list) {
this.list = list;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyc2view,parent,false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder viewHolder, final int position) {
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
viewHolder.textView.setText(list.get(position));
viewHolder.button.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;
}
public class MyHolder extends RecyclerView.ViewHolder{
SwipeLayout swipeLayout;
TextView textView;
Button button;
public MyHolder(@NonNull View itemView) {
super(itemView);
swipeLayout = itemView.findViewById(R.id.recyc2_swi);
textView = itemView.findViewById(R.id.recyc2_text);
button = itemView.findViewById(R.id.recyc2_button);
}
}
Activity
// An highlighted block
public class Main2Activity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView = findViewById(R.id.main2_recyc);
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<10;i++){
String str = "我是文本"+i;
list.add(str);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MyswipAdapter adapter = new MyswipAdapter(list);
recyclerView.setAdapter(adapter);
}
}
断点续传
// An highlighted block
public class MainActivity extends AppCompatActivity {
private long start = 0;//每次下载的位置,初始为0
private long end = 1024*1024;//一次只下载 1M
private Button load;
private int max = 0;
private int time = 0;//一共下载5次,从第0次下载
private TextView textView;
private boolean isstop = false;
private long maxsum = 0;
private Button stop;
private TextView kaishi;
private TextView jieshu;
private ProgressBar seekBar;
private int sum = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 0 && msg.obj != null && !isstop){
if(time<=5){
new MyLoad().start();
time++;
// sum = time;
}else{
handler.sendEmptyMessage(1);
}
textView.setText(msg.obj.toString());
}else if(msg.what == 1){
textView.setText("下载完成");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
load = findViewById(R.id.main_load);
textView = findViewById(R.id.main_text);
stop = findViewById(R.id.main_stop);
kaishi = findViewById(R.id.main_start);
jieshu = findViewById(R.id.main_finish);
seekBar = findViewById(R.id.main_seeek);
new Mycount().start();
seekBar.setMax((int)maxsum);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isstop = false;
start=sum;
new MyLoad().start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//告诉线程:不要继续下载了
isstop = true;
}
});
}
public class MyLoad extends Thread{
@Override
public void run() {
super.run();
try {
URL url1 = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
HttpURLConnection connection = (HttpURLConnection)url1.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
//存文件的位置
String path = Environment.getExternalStorageDirectory().getPath()+"/video2.mp4";
RandomAccessFile randomAccessFile = new RandomAccessFile(path,"rw");
randomAccessFile.seek(start);
connection.setRequestProperty("Range","bytes="+start+"-"+end);
//获取下载文件的总长度
InputStream is = null;
if(connection.getResponseCode() == 206){
Log.e("########updata",206+"");
Log.e("########updata",max+"");
is = connection.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while((len = is.read(bytes))!=-1){
randomAccessFile.write(bytes,0,len);
sum+= len;
Log.e("##########dss",sum+"");
seekBar.setProgress(sum);
if(isstop){
break;
}
}
}
// if(is != null){
// is.close();
// }
// if(randomAccessFile !=null){
// randomAccessFile.close();
// }
//从上次结束的位置+1开始
start = end + 1;
end += 1024 * 1024;
Message message = Message.obtain();
message.what = 0;
String str = "文件:" + time + "下载成功" + start + "-" + end;
message.obj = str;
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Mycount extends Thread{
@Override
public void run() {
super.run();
try {
URL url1 = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
HttpURLConnection connection = null;
connection = (HttpURLConnection)url1.openConnection();
connection.setRequestMethod("GET");
if(connection.getResponseCode() ==200){
int contentLength = connection.getContentLength();
maxsum = contentLength;
Log.e("######dsdsdsdsds",maxsum+"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
标签:断点续传,侧滑,int,list,private,Override,Android,public 来源: https://blog.csdn.net/LH_sunshine/article/details/93915716