Recyclerview
RecyclerView比ListView的优势:
1.RecyclerView比listview实现效果更多
2.RecycelrView支持多布局;
3.RecyclerView根据项目需要插拔功能
RecyclerView默认不支持点击事件->程序员代码中通过回调接口的方式添加监听
一.效果:线性/网格/瀑布流
二.重要的方法
1.RecyclerView横向滑动:
LinearLayoutManager.HORIZONTAL横向滑动LinearLayoutManager.VERTICAL垂直滑动
2.RecyclerView.Adapter中刷新方法区别:
notifyDataSetChanged();整体刷新+没有动画效果
notifyItemInserted(int position,Object data):有动画效果+添加一条数据在position位置
notifyItemRemoved(position);有动画效果+删除一条数据并刷新
注意:当添加和删除的时候,要更新下标,不然有错位现象
3.RecyclerView多布局展示:
public int getItemViewType(int position)返回当前数据的itemview类型
4.RecyclerView常见方法:
LinearLayoutManager:recyclerview线性管理器(垂直水平方向);
GridLayoutManager:网格布局管理器;
StaggeredGridLayoutManager:瀑布流布局管理器;
RecyclerView.setLayoutManager(LayoutManager manager):添加布局管理器
RecyclerView.addItemDecoration(ItemDecoration decoration):添加分割线
RecyclerView.setItemAnimator(ItemAnimator animator):添加动画方法
RecyclerView.setAdapter(Adapter adapter):添加适配器
四.代码
package com.example.day1_recycleview;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
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.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MyItemLongonClick,MyItemonClick{
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String jsons = msg.obj.toString();
Gson gson = new Gson();
// Beans bean = gson.fromJson(jsons, Beans.class);
// List<Beans.DataBean> data = bean.getData();
// list.addAll(data);
Bean bean = gson.fromJson(jsons, Bean.class);
List<Bean.DataBean> data = bean.getData();
list.addAll(data);
adpter = new My_Adpter(list,MainActivity.this);
adpter.setMyItemLongonClick(MainActivity.this);
adpter.setMyItemonClick(MainActivity.this);
rv.setAdapter(adpter);
}
};
String src = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
String srcs = "https://www.apiopen.top/satinApi?type=1&page=1";
RecyclerView rv;
ArrayList<Bean.DataBean> list = new ArrayList<>();
My_Adpter adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
new My_Thread(srcs, handler).start();
//线性布局
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//网格布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
//瀑布流布局:一般和图片结合使用,自适应图片的高度
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//设置布局
rv.setLayoutManager(staggeredGridLayoutManager);
//设置下划线
// DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
// rv.addItemDecoration(dividerItemDecoration);
//设置动画
DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
//删除动画(动画时间)
defaultItemAnimator.setRemoveDuration(2000);
//添加动画(动画时间)
defaultItemAnimator.setAddDuration(2000);
//给item添加动画
rv.setItemAnimator(defaultItemAnimator);
}
@Override
public void ItemonLongClick(int Postion) {
}
@Override
public void ItemonClick(int Postion) {
list.remove(Postion);
adpter.notifyItemRemoved(Postion);
adpter.notifyItemRangeChanged(0,list.size());
}
}
package com.example.day1_recycleview;
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.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class My_Adpter extends RecyclerView.Adapter<My_Adpter.ViewHolder> {
ArrayList<Bean.DataBean> list;
private MainActivity context;
MyItemonClick myItemonClick;
MyItemLongonClick myItemLongonClick;
public void setMyItemonClick(MyItemonClick myItemonClick) {
this.myItemonClick = myItemonClick;
}
public void setMyItemLongonClick(MyItemLongonClick myItemLongonClick) {
this.myItemLongonClick = myItemLongonClick;
}
public My_Adpter(ArrayList<Bean.DataBean> list, MainActivity context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.layout, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
Glide.with(context).load(list.get(i).getCdn_img()).into(viewHolder.iv);
Glide.with(context).load(list.get(i).getImage0()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.tx);
//获取屏幕宽度
WindowManager windowManager = context.getWindowManager();
final float width = windowManager.getDefaultDisplay().getWidth()/2;
//获取图片宽度
float wd = Float.parseFloat(list.get(i).getWidth());
//比例
float bl = wd/width;
//获取高度
float hd = Float.parseFloat(list.get(i).getHeight());
//获取高比例
int gao = (int) (hd/bl);
ViewGroup.LayoutParams layoutParams = viewHolder.iv.getLayoutParams();
// int heithg = (int) ((Math.random()*200)+200);
layoutParams.height = gao;
viewHolder.iv.setLayoutParams(layoutParams);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myItemonClick.ItemonClick(i);
}
});
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
myItemLongonClick.ItemonLongClick(i);
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView iv;
ImageView tx;
public ViewHolder(@NonNull View itemView) {
super(itemView);
iv = itemView.findViewById(R.id.img);
tx = itemView.findViewById(R.id.iv);
}
}
}
五.实现多布局只给出适配器代码:微信通讯录页面
/**
RecyclerView.ViewHolder:由于进行多布局操作
需要多个ViewHolder对象对应过个xml布局
自定义ViewHolder父类
*/
package com.example.day1_recyleview_dbj;
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.TextView;
import java.util.ArrayList;
public class SecondAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<PhoneBean> list;
MainActivity context;
public SecondAdapter(ArrayList<PhoneBean> list, MainActivity context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == 0){
return new TitleHolder(LayoutInflater.from(context).inflate(R.layout.layout_title,viewGroup,false));
}else {
return new PhoneHolder(LayoutInflater.from(context).inflate(R.layout.layout_phone,viewGroup,false));
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (getItemViewType(i) == 0){
TitleHolder titleHolder = (TitleHolder) viewHolder;
titleHolder.title.setText(list.get(i).getTitle());
}else {
PhoneHolder phoneHolder = (PhoneHolder) viewHolder;
phoneHolder.name.setText(list.get(i).getName());
phoneHolder.number.setText(list.get(i).getNumber());
}
}
@Override
public int getItemViewType(int position) {
return list.get(position).type;
}
@Override
public int getItemCount() {
return list.size();
}
class TitleHolder extends RecyclerView.ViewHolder{
TextView title;
public TitleHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}
}
class PhoneHolder extends RecyclerView.ViewHolder{
TextView name;
TextView number;
public PhoneHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
number = itemView.findViewById(R.id.number);
}
}
}