網站首頁 編程語言 正文
本文實例為大家分享了RecyclerView實現側滑和網絡斷點續傳的具體代碼,供大家參考,具體內容如下
RecyclerView側滑
主布局
<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" ? ? tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView ? ? android:id="@+id/myrecycler" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout>
側滑布局
<com.daimajia.swipe.SwipeLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" ? ? android:layout_height="wrap_content"> <LinearLayout ? ? android:layout_width="wrap_content" ? ? android:layout_height="wrap_content"> ? ? <Button ? ? ? ? android:text="刪除" ? ? ? ? android:id="@+id/b2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" /> ? ? <Button ? ? ? ? android:text="添加" ? ? ? ? android:id="@+id/b3" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout> <TextView ? ? android:textSize="20dp" ? ? android:id="@+id/t1" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" /> </com.daimajia.swipe.SwipeLayout>
適配器
public class MyAdapter1 extends RecyclerView.Adapter<MyAdapter1.RVHolder> {
ArrayList<String> list= new ArrayList<>();
public void refershData(ArrayList<String> mlist){
? ? list.addAll( mlist);
? ? notifyDataSetChanged();
}
@NonNull
@Override
public RVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
? ? View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.name1,viewGroup,false);
? ? return new RVHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RVHolder rvHolder, final int i) {
? ? rvHolder.textView.setText(list.get(i));
? ? rvHolder.button.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? isonclick.itemonclick(i);
? ? ? ? }
? ? });
}
public interface Isonclick{
? ? public void itemonclick(int id);
}
Isonclick isonclick;
public void ?Onclick(Isonclick isonclick) {
? ? this.isonclick = isonclick;
}
@Override
public int getItemCount() {
? ? return list.size();
}
class RVHolder extends RecyclerView.ViewHolder{
? ? TextView textView;
? ? Button button;
? ? public RVHolder(@NonNull View itemView) {
? ? ? ? super(itemView);
? ? ? ? textView = itemView.findViewById(R.id.t1);
? ? ? ? button = itemView.findViewById(R.id.b2);
? ? }
}
}
Activity
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter1 myAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.activity_main);
? ? init();
? ? initData();
}
private void init() {
? ? recyclerView = findViewById(R.id.myrecycler);
? ? final LinearLayoutManager manager = new LinearLayoutManager(this);
? ? manager.setOrientation(LinearLayoutManager.VERTICAL);
? ? recyclerView.setLayoutManager(manager);
? ? myAdapter1 = new MyAdapter1();
? ? recyclerView.setAdapter(myAdapter1);
? ? myAdapter1.Onclick(new MyAdapter1.Isonclick() {
? ? ? ? @Override
? ? ? ? public void itemonclick(int id) {
? ? ? ? ? ? myAdapter1.list.remove(id);
? ? ? ? ? ? myAdapter1.notifyDataSetChanged();
? ? ? ? }
? ? });
}
public void initData(){
? ?ArrayList<String> list= new ArrayList<>();
? ? for (int i=0;i<80;i++){
? ? ? ? list.add("第"+i+"個");
? ? }
? ? myAdapter1.refershData(list);
}
}
效果
網絡斷點續傳
public class Demo extends AppCompatActivity implements View.OnClickListener {
long start = 0;
long end = 1024*1024;
long max;
int time = 0;
int sum = 0;
SeekBar bar;
Button jixu;
Button parse;
int temp;
Handler handler = new Handler(){
? ? @Override
? ? public void handleMessage(Message msg) {
? ? ? ? switch (msg.what){
? ? ? ? ? ? case 100:
? ? ? ? ? ? ? ? if(msg.obj != null){
? ? ? ? ? ? ? ? ? ? if(time < 5){
? ? ? ? ? ? ? ? ? ? ? ? new MyThread().start();
? ? ? ? ? ? ? ? ? ? ? ? time++;
? ? ? ? ? ? ? ? ? ? }else if(sum >= max){
? ? ? ? ? ? ? ? ? ? ? ? handler.sendEmptyMessage(200);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? Log.e("##########",msg.obj.toString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 200:
? ? ? ? ? ? ? ? ? ? Log.e("###########","下載完成");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 300:
? ? ? ? ? ? ? ? bar.setProgress(sum);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.activity_demo);
? ? initView();
? ? try {
? ? ? ? max = new MyTask().execute("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").get();
? ? ? ? bar.setMax((int) max);
? ? } catch (ExecutionException e) {
? ? ? ? e.printStackTrace();
? ? } catch (InterruptedException e) {
? ? ? ? e.printStackTrace();
? ? }
? ? new MyThread().start();
}
@Override
public void onClick(View v) {
? ? switch (v.getId()){
? ? ? ? case R.id.start:
? ? ? ? ? ? if(temp >= 0) {
? ? ? ? ? ? ? ? time = temp;
? ? ? ? ? ? ? ? temp = -1;
? ? ? ? ? ? ? ? new MyThread().start();
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case R.id.parse:
? ? ? ? ? ? temp = time;
? ? ? ? ? ? time = 5;
? ? ? ? ? ? Message message = new Message();
? ? ? ? ? ? message.what = 100;
? ? ? ? ? ? handler.sendMessage(message);
? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? }
}
class MyTask extends AsyncTask<String,String,Integer>{
? ? @Override
? ? protected Integer doInBackground(String... strings) {
? ? ? ? try {
? ? ? ? ? ? URL url = new URL(strings[0]);
? ? ? ? ? ? HttpURLConnection hu = (HttpURLConnection) url.openConnection();
? ? ? ? ? ? if(hu.getResponseCode() == 200){
? ? ? ? ? ? ? ? int length = hu.getContentLength();
? ? ? ? ? ? ? ? return length;
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }
}
private void initView() {
? ? bar = findViewById(R.id.bar);
? ? jixu = findViewById(R.id.start);
? ? parse = findViewById(R.id.parse);
? ? jixu.setOnClickListener(this);
? ? parse.setOnClickListener(this);
}
private class MyThread extends Thread {
? ? @Override
? ? public void run() {
? ? ? ? try {
? ? ? ? ? ? URL url = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
? ? ? ? ? ? HttpURLConnection hu = (HttpURLConnection) url.openConnection();
? ? ? ? ? ? hu.setRequestMethod("GET");
? ? ? ? ? ? hu.setDoInput(true);
? ? ? ? ? ? hu.setDoOutput(true);
? ? ? ? ? ? String path = Environment.getExternalStorageDirectory().getPath();
? ? ? ? ? ? File ff = new File(path, "wang.mp4");
? ? ? ? ? ? RandomAccessFile file = new RandomAccessFile(ff, "rw");
? ? ? ? ? ? file.seek(start);
? ? ? ? ? ? hu.setRequestProperty("Range","bytes="+start+"-"+end);
? ? ? ? ? ? InputStream is = null;
? ? ? ? ? ? if(hu.getResponseCode() == 206){
? ? ? ? ? ? ? ? int l = 0;
? ? ? ? ? ? ? ? byte[] b = new byte[1024];
? ? ? ? ? ? ? ? is = hu.getInputStream();
? ? ? ? ? ? ? ? while ((l = is.read(b)) != -1){
? ? ? ? ? ? ? ? ? ? file.write(b,0,l);
? ? ? ? ? ? ? ? ? ? sum += l;
? ? ? ? ? ? ? ? ? ? Message msg = new Message();
? ? ? ? ? ? ? ? ? ? msg.what = 300;
? ? ? ? ? ? ? ? ? ? handler.sendMessage(msg);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? start = end+1;
? ? ? ? ? ? ? ? end += 1024*1024;
? ? ? ? ? ? ? ? Message message = new Message();
? ? ? ? ? ? ? ? message.what = 100;
? ? ? ? ? ? ? ? String ss = "正在下載"+start+"--"+end;
? ? ? ? ? ? ? ? message.obj = ss;
? ? ? ? ? ? ? ? handler.sendMessage(message);
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
}
原文鏈接:https://blog.csdn.net/GaoYue3321/article/details/93909233
相關推薦
- 2022-09-20 C#單線程和多線程端口掃描器詳解_C#教程
- 2022-08-01 詳解docker?API管理接口增加CA安全認證_docker
- 2022-04-10 WPF關鍵幀動畫介紹與實現_實用技巧
- 2022-04-09 IDEA中命令行安裝git報錯:error: failed to push some refs to
- 2022-07-24 docker容器使用GPU方法實現_docker
- 2023-02-10 python自定義函數中的return和print使用及說明_python
- 2022-12-25 pytorch中model.named_parameters()與model.parameters(
- 2022-04-14 C#可變參數params示例詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支