RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳
本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳的具體代碼,供大家參考,具體內(nèi)容如下
RecyclerView側(cè)滑
主布局
<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>
側(cè)滑布局
<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+"個(gè)");
? ? }
? ? myAdapter1.refershData(list);
}
}效果

網(wǎng)絡(luò)斷點(diǎn)續(xù)傳
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();
? ? ? ? }
? ? }
}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RecyclerView實(shí)現(xiàn)側(cè)滑拖拽功能
- android RecyclerView側(cè)滑菜單,滑動刪除,長按拖拽,下拉刷新上拉加載
- Android recyclerview實(shí)現(xiàn)拖拽排序和側(cè)滑刪除
- android的RecyclerView實(shí)現(xiàn)拖拽排序和側(cè)滑刪除示例
- RecyclerView進(jìn)階:使用ItemTouchHelper實(shí)現(xiàn)拖拽和側(cè)滑刪除效果
- LRecyclerView側(cè)滑iOS阻塞效果不完整的解決辦法
- Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
- Android實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能
- 詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度)
- android實(shí)現(xiàn)多線程下載文件(支持暫停、取消、斷點(diǎn)續(xù)傳)
相關(guān)文章
Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析
這篇文章主要介紹了Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android自定義View Material Design理念詳解
這篇文章主要為大家介紹了Android自定義View Material Design理念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android根據(jù)電話號碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09
Android通過SharedPreferences實(shí)現(xiàn)自動登錄記住用戶名和密碼功能
最近使用SharedPreferences實(shí)現(xiàn)了一個(gè)android自動登錄功能,特此分享到腳本之家平臺供大家參考2017-07-07
Android之ImageSwitcher的實(shí)例詳解
這篇文章主要介紹了Android之ImageSwitcher的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家理解這個(gè)控件的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08

