1. RecyclerView的三种内置布局排列方式LinearLayoutManager,GridLayoutManager,以及StaggeredGridLayoutManager。
1) LinearLayoutManager
- LinearLayoutManager实现纵向排列
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); //可以不写,因为LinearLayoutManager默认的就是纵向排列方式
recyclerView.setLayoutManager(layoutManager);
- LinearLayoutManager实现横向排列
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
2) GridLayoutManager 网格式排列
GridLayoutManager layoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
其中new GridLayoutManager();
有两个参数,上下文和列数
3) StaggeredGridLayoutManager 瀑布流布局
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
2.RecyclerView的两种点击事件
- 在RecyclerVIew的适配器中的onCreateViewHolder()方法里
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.gril_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.gril_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Intent intent = new Intent(context,SecondActivity.class);
context.startActivity(intent);
}
});
return holder;
}
- 通过接口回调
public class recyclerViewClickAdapter extends RecyclerView.Adapter<recyclerViewClickAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {
private Context context;
private List<StoryBean> mList;
//接口回调步骤二:定义成员变量
private MyOnClickListener myOnClickListener;
private MyOnLongClickListener myOnLongClickListener;
public recyclerViewClick(Context context, List<StoryBean> mList) {
this.context = context;
this.mList = mList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_daily, parent, false);
ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//设置数据
holder.imageView.setImageResource(mList.get(position).getImage());
holder.textView.setText(mList.get(position).getTitle());
//将数据保存在itemView的id中,以便编辑时进行获取
holder.itemView.setId(position);
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textview);
imageView = itemView.findViewById(R.id.imageview);
}
}
//接口回调步骤一:定义监听接口类
public static interface MyOnClickListener{
public void OnItemClickListener(View view, int position);
}
public static interface MyOnLongClickListener{
public void OnItemLongClickListener(View view, int position);
}
//接口回调步骤三:实例化 暴露给外面的调用者, 定义Listener方法
public void setOnClickListener(MyOnClickListener listener){
this.myOnClickListener = listener;
}
public void setOnLongClickListener(MyOnLongClickListener listener){
this.myOnLongClickListener = listener;
}
//接口回调步骤四:实现接口回调方法,将点击事件转移给外面的调用者
@Override
public void onClick(View v) {
if (myOnClickListener != null){
myOnClickListener.OnItemClickListener(v,v.getId());
}
}
@Override
public boolean onLongClick(View v) {
if (myOnLongClickListener != null){
myOnLongClickListener.OnItemLongClickListener(v, v.getId());
}
return true;
}
}
在activit.java中实现RecyclerView的监听
//添加监听
adapter.setOnClickListener(new recyclerViewClickAdapter.MyOnClickListener() {
@Override
public void OnItemClickListener(View view, int position) {
}
});
adapter.setOnLongClickListener(new recyclerViewClickAdapter.MyOnLongClickListener() {
@Override
public void OnItemLongClickListener(View view, int position) {
}
});