Android sideslip and breakpoint sequel

Note: to write in the position of the layout, replace the linear or relative layout, and write two sub layouts. The first is the sideslip layout, which is displayed off the screen by default. The second is the layout to be displayed;

The custom class inherits recyclershiplayout and overrides getswipelayoutresourceid (postion)

Common methods: setmode: set sideslip mode openitem: open closeitem: close

Adapter

// 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();
            }


        }
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>