写点什么

Android RecyclerView 的简单使用,我的阿里手淘面试经历分享

发布于: 2021 年 11 月 08 日

LinearLayoutManager?llManager?=?new?LinearLayoutManager(this);


//默认垂直展示,这里设置为水平显示


llManager.setOrientation(LinearLayoutManager.HORIZONTAL);


//设置?RecyclerView?的布局管理器


recyclerviewBinding.rvUserinfo.setLayoutManager(llManager);


List<UserInfo>?userList?=?new?ArrayList<>(


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


);


userList.add(new?UserInfo("苏妲己",?R.mipmap.scc_1));


...


userList.add(new?UserInfo("王昭君",?R.mipmap.scc_7));


BaseRecyclerAdapter<UserInfo>?adapter?=?new?BaseRecyclerAdapter<UserInfo>(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"/>


</RelativeLayout>

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"


.../>


</RelativeLayout>

BaseRecyclerAdapter

public?abstract?class?BaseRecyclerAdapter<T>?extends?RecyclerView.Adapter<RecyclerViewHolder>?{


protected?final?List<T>?mData;


protected?final?Context?mContext;


protected?LayoutInflater?mInflater;


private?OnItemClickListener?mClickListener;


private?OnItemLongClickListener?mLongClickListener;


public?BaseRecyclerAdapter(Context?ctx,?List<T>?list)?{


mData?=?(list?!=?null)???list?:?new?ArrayList<T>();


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<View>?mViews;//集合类,layout 里包含的 View,以 view 的 id 作为 key,value 是 view 对象


private?Context?mContext;//上下文对象


public?RecyclerViewHolder(Context?ctx,?View?itemView)?{


super(itemView);


mContext?=?ctx;


mViews?=?new?SparseArray<View>();


}


private?<T?extends?View>?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;


}


public?RecyclerViewHolder?setVisible(int?viewId,?int?visible)?{


View?view?=?findViewById(viewId);


view.setVisibility(visible);


return?this;


}


public?RecyclerViewHolder?setBackground(int?viewId,?int?resId)?{


View?view?=?findViewById(viewId);


view.setBackgroundResource(resId);


return?this;

评论

发布
暂无评论
Android RecyclerView的简单使用,我的阿里手淘面试经历分享