RecyclerView比ListView的优势:
1.RecyclerView比listview实现效果更多
2.RecycelrView支持多布局;
3.RecyclerView根据项目需要插拔功能
RecyclerView的三种布局
1.线性布局
2.网格布局
3.瀑布流布局
package com.example.sixeightwork.Work2;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import com.example.sixeightwork.R;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
public class Main3Activity extends AppCompatActivity{
RecyclerView recyclerView; ArrayList<ZiYuan> arrayList;
MyRcAdapter myRcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
recyclerView = findViewById(R.id.rv);
//线性布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//网格布局
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
//瀑布流
StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
DividerItemDecoration did=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
Drawable drawable = getResources().getDrawable(R.drawable.liner);
did.setDrawable(drawable);
recyclerView.addItemDecoration(did);
DefaultItemAnimator dia=new DefaultItemAnimator();
dia.setRemoveDuration(200);
dia.setAddDuration(200);
recyclerView.setItemAnimator(dia);
try {
arrayList = new MyAnstaker().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=30&page=1").get();
Log.e("@###",arrayList.size()+"");
myRcAdapter = new MyRcAdapter(this, arrayList);
recyclerView.setAdapter(myRcAdapter);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//设置接口方法实现效果
myRcAdapter.setRecyListener(new RecyListener() {
@Override
public void shanchu(int index) {
arrayList.remove(index);
myRcAdapter.notifyItemRemoved(index);
myRcAdapter.notifyItemRangeChanged(index,arrayList.size());
}
});
}
}
package com.example.sixeightwork.Work2;
import android.os.AsyncTask;
import android.util.Log;
import java.util.ArrayList;
public class MyAnstaker extends AsyncTask<String,Void, ArrayList<ZiYuan>> {
@Override
protected ArrayList<ZiYuan> doInBackground(String... strings) {
Log.e("###", "doInBackground: "+strings[0] );
ArrayList<ZiYuan> getshuju = new URlhttp().getshuju(strings[0]);
return getshuju;
}
}
package com.example.sixeightwork.Work2;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.sixeightwork.R;
import java.util.ArrayList;
public class MyRcAdapter extends RecyclerView.Adapter<MyRcAdapter.RVhoder> {
Context context;
ArrayList<ZiYuan> ziYuans;
RecyListener recyListener;
public MyRcAdapter(Context context, ArrayList<ZiYuan> ziYuans) {
this.context = context;
this.ziYuans = ziYuans;
}
public void setRecyListener(RecyListener recyListener) {
this.recyListener = recyListener;
}
@NonNull
@Override
public RVhoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.sim,viewGroup,false);
return new RVhoder(view);
}
@Override
public void onBindViewHolder(@NonNull RVhoder rVhoder, final int i) {
//设置瀑布流的高度 线性和网格不需要
int gao= (int) (Math.random()*(301+200));
ViewGroup.LayoutParams layoutParams = rVhoder.textView.getLayoutParams();
layoutParams.height=gao;
rVhoder.textView.setText(ziYuans.get(i).name);
rVhoder.imageView.setImageBitmap(ziYuans.get(i).bitmap);
rVhoder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyListener.shanchu(i);
}
});
}
@Override
public int getItemCount() {
return ziYuans.size();
}
class RVhoder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public RVhoder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.img);
textView=itemView.findViewById(R.id.tv);
}
}
}
package com.example.sixeightwork.Work2;
interface RecyListener {
void shanchu(int index);
}
package com.example.sixeightwork.Work2;
import android.graphics.Bitmap;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class URlhttp {
public ArrayList<ZiYuan> getshuju(String sss) {
ArrayList<ZiYuan> arrayList = new ArrayList<>();
try {
URL url = new URL(sss);
Log.e("###", "getshuju: "+sss );
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
Log.e("###", "getshuju: "+urlConnection.getResponseCode() );
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream();
String ss = "";
int len = 0;
byte[] bytes = new byte[1024];
while ((len=inputStream.read(bytes)) != -1) {
String s = new String(bytes, 0, len);
Log.e("###", "getshuju: "+s );
ss += s;
}
Log.e("###", "getshuju: "+ss );
JSONObject jsonObject = new JSONObject(ss);
JSONArray data = jsonObject.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject jsonObject1 = data.getJSONObject(i);
String pic = jsonObject1.getString("pic");
String title = jsonObject1.getString("title");
Bitmap getshuju = new URlimage().getshuju(pic);
ZiYuan ziYuan = new ZiYuan();
ziYuan.name=title;
ziYuan.bitmap=getshuju;
arrayList.add(ziYuan);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}
}
package com.example.sixeightwork.Work2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URlimage {
public Bitmap getshuju(String s){
try {
URL url=new URL(s);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.example.sixeightwork.Work2;
import android.graphics.Bitmap;
class ZiYuan {
Bitmap bitmap;
String name;
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#000"
></solid>
<size android:height="1dp"
android:width="1dp"
></size>
</shape>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:padding="3dp"
android:background="#00ffff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#ff0">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:src="@mipmap/ic_launcher"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"/>
<TextView
android:id="@+id/tv"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
RelativeLayout>
LinearLayout>
<RelativeLayout 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="wrap_content"
tools:context=".Work2.Main3Activity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content">android.support.v7.widget.RecyclerView>
RelativeLayout>