RecyclerViewActivity
public?class?RecyclerViewActivity?extends?ActivityBase{
ActivityRecyclerviewBinding?recyclerviewBinding;
@O
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
verride
protected?void?onCreate(@Nullable?Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
recyclerviewBinding?=?ActivityRecyclerviewBinding.inflate(getLayoutInflater());
setContentView(recyclerviewBinding.getRoot());
LinearLayoutManager?llManager?=?new?LinearLayoutManager(this);
//默认垂直展示,这里设置为水平显示
llManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//设置?RecyclerView?的布局管理器
recyclerviewBinding.rvUserinfo.setLayoutManager(llManager);
List?userList?=?new?ArrayList<>();
userList.add(new?UserInfo(“苏妲己”,?R.mipmap.scc_1));
…
userList.add(new?UserInfo(“王昭君”,?R.mipmap.scc_7));
BaseRecyclerAdapter?adapter?=?new?BaseRecyclerAdapter(this,userList)?{
@Override
public?int?getItemLayoutId(int?viewType)?{
return?R.layout.item_rv_userinfo;
}
@Override
public?void?convert(RecyclerViewHolder?holder,?int?position,?UserInfo?bean)?{
holder.setText(R.id.tv_name,bean.getName());
holder.setText(R.id.tv_content,bean.getContent());
holder.setImageResource(R.id.iv_head,bean.getHead());
}
};
//设置?RecyclerView?的数据适配器。
recyclerviewBinding.rvUserinfo.setAdapter(adapter);
//addItemDecoration
recyclerviewBinding.rvUserinfo.addItemDecoration(new?RvDividerItemDecoration(this,10));
}
}
activity_recyclerview.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_userinfo"
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
item_rv_userinfo.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“wrap_content”
android:layout_height=“match_parent”>
<ImageView
android:id="@+id/iv_head"
…/>
<TextView
android:id="@+id/tv_name"
…/>
<TextView
android:id="@+id/tv_content"
…/>
BaseRecyclerAdapter
public?abstract?class?BaseRecyclerAdapter?extends?RecyclerView.Adapter?{
protected?final?List?mData;
protected?final?Context?mContext;
protected?LayoutInflater?mInflater;
private?OnItemClickListener?mClickListener;
private?OnItemLongClickListener?mLongClickListener;
public?BaseRecyclerAdapter(Context?ctx,?List?list)?{
mData?=?(list?!=?null)???list?:?new?ArrayList();
mContext?=?ctx;
mInflater?=?LayoutInflater.from(ctx);
}
@Override
public?RecyclerViewHolder?onCreateViewHolder(ViewGroup?parent,?int?viewType)?{
创建一个新视图,该视图定义item的UI
final?RecyclerViewHolder?holder?=?new?RecyclerViewHolder(mContext,
mInflater.inflate(getItemLayoutId(viewType),?parent,?false));
if?(mClickListener?!=?null)?{
holder.itemView.setOnClickListener(new?View.OnClickListener()?{
@Override
public?void?onClick(View?v)?{
mClickListener.onItemClick(holder.itemView,?holder.getLayoutPosition());
}
});
}
if?(mLongClickListener?!=?null)?{
holder.itemView.setOnLongClickListener(new?View.OnLongClickListener()?{
@Override
public?boolean?onLongClick(View?v)?{
mLongClickListener.onItemLongClick(holder.itemView,?holder.getLayoutPosition());
return?true;
}
});
}
return?holder;
}
@Override
public?void?onBindViewHolder(RecyclerViewHolder?holder,?int?position)?{
convert(holder,?position,?mData.get(position));
}
@Override
public?int?getItemCount()?{
return?mData.size();
}
public?void?add(int?pos,?T?item)?{
mData.add(pos,?item);
notifyItemInserted(pos);
}
public?void?delete(int?pos)?{
mData.remove(pos);
notifyItemRemoved(pos);
}
public?void?setOnItemClickListener(OnItemClickListener?listener)?{
mClickListener?=?listener;
}
public?void?setOnItemLongClickListener(OnItemLongClickListener?listener)?{
mLongClickListener?=?listener;
}
abstract?public?int?getItemLayoutId(int?viewType);
abstract?public?void?convert(RecyclerViewHolder?holder,?int?position,?T?bean);
public?interface?OnItemClickListener?{
public?void?onItemClick(View?itemView,?int?pos);
}
public?interface?OnItemLongClickListener?{
public?void?onItemLongClick(View?itemView,?int?pos);
}
}
RecyclerViewHolder
public?class?RecyclerViewHolder?extends?RecyclerView.ViewHolder?{
private?SparseArray?mViews;//集合类,layout里包含的View,以view的id作为key,value是view对象
private?Context?mContext;//上下文对象
public?RecyclerViewHolder(Context?ctx,?View?itemView)?{
super(itemView);
mContext?=?ctx;
mViews?=?new?SparseArray();
}
private??T?findViewById(int?viewId)?{
View?view?=?mViews.get(viewId);
if?(view?==?null)?{
view?=?itemView.findViewById(viewId);
mViews.put(viewId,?view);
}
return?(T)?view;
}
public?View?getView(int?viewId)?{
return?findViewById(viewId);
}
public?TextView?getTextView(int?viewId)?{
return?(TextView)?getView(viewId);
}
public?Button?getButton(int?viewId)?{
return?(Button)?getView(viewId);
}
public?ImageView?getImageView(int?viewId)?{
return?(ImageView)?getView(viewId);
}
public?ImageButton?getImageButton(int?viewId)?{
return?(ImageButton)?getView(viewId);
}
public?EditText?getEditText(int?viewId)?{
return?(EditText)?getView(viewId);
}
public?RecyclerViewHolder?setText(int?viewId,?String?value)?{
TextView?view?=?findViewById(viewId);
view.setText(MStringUtils.isNullOrEmpty(value)???“”?:?value);
return?this;
}
|