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