RecyclerView三种类型
程序员文章站
2022-06-08 16:30:48
...
样式布局
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="0dp" android:width="1dp"></size>
<solid android:color="@android:color/holo_red_light"></solid>
</shape>
异步下载类
class MyTask extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection hu = (HttpURLConnection) url.openConnection();
hu.connect();
int cod = hu.getResponseCode();
if(cod == 200){
InputStream is = hu.getInputStream();
StringBuffer sb = new StringBuffer();
int l = 0;
byte[] b = new byte[1024];
while((l = is.read(b)) != -1){
sb.append(new String(b,0,l));
}
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
布局文件和子布局
主布局
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Demo">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlist"
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
子布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/lay"
android:background="#0ff"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:src="@drawable/ic_launcher_background"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/titles"
android:layout_toRightOf="@id/img"
android:text="123"
/>
</RelativeLayout>
</RelativeLayout>
主类
public class Demo extends AppCompatActivity {
RVAdapter adapter;
RecyclerView rlist;
ArrayList<Map<String,String>> lists = new ArrayList<>();
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
rlist = findViewById(R.id.rlist);
String ss = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
try {
//异步下载
String s = new MyTask().execute(ss).get();
//解析赋值方法
init(s);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//瀑布流
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//网格布局
// GridLayoutManager manager = new GridLayoutManager(this,2);
//线性布局
// LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);//指定布局方向
rlist.setLayoutManager(manager);//设置布局
//设置样式布局
DividerItemDecoration decoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
decoration.setDrawable(getDrawable(R.drawable.divider_style));
rlist.addItemDecoration(decoration);
//设置动画
DefaultItemAnimator animator = new DefaultItemAnimator();
//设置时长
animator.setAddDuration(1000);
animator.setRemoveDuration(1000);
rlist.setItemAnimator(animator);
//适配器
adapter = new RVAdapter(this, lists);
rlist.setAdapter(adapter);
}
public void init(String ss){
try {
JSONObject jo = new JSONObject(ss);
JSONArray data = jo.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject o = data.getJSONObject(i);
String title = o.getString("title");
String pic = o.getString("pic");
HashMap<String, String> map = new HashMap<>();
map.put("title",title);
map.put("pic",pic);
lists.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
适配器
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVHolder> {
Context context;
ArrayList<Map<String,String>> lists;
RVLister lister;
public RVAdapter(Context context, ArrayList<Map<String, String>> lists) {
this.context = context;
this.lists = lists;
}
@NonNull
@Override
public RVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item, viewGroup,false);
return new RVHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RVHolder rvHolder, final int i) {
Map<String, String> map = lists.get(i);
rvHolder.titles.setText(map.get("title"));
Picasso.with(context).load(map.get("pic")).into(rvHolder.img);
//瀑布流设置高度,瀑布流专用
// int max = (int) (Math.random()*101+100);
// ViewGroup.LayoutParams params = rvHolder.lay.getLayoutParams();
// params.height = max;
// rvHolder.lay.setLayoutParams(params);
@Override
public int getItemCount() {
return lists.size();
}
class RVHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView img;
RelativeLayout lay;
public RVHolder(@NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.titles);
img = itemView.findViewById(R.id.img);
lay = itemView.findViewById(R.id.lay);
}
}
}
效果图
线性布局效果图
网格布局效果图
瀑布流效果图
上一篇: 三种经典的布局方式
下一篇: 开发思想: 对象复用