app-Fragment中运用ReclerView
仓库地址 https://gitee.com/yagao/codes/u74yjnts6zo5rwevgb0xp79
在Fragment控件中增加ReclerView控件,增添首页内容。
步骤
- 在fragment对应的layout中添加ReclerView控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BBBBBB"/>
</LinearLayout>
2.对控件的函数控制.
class GoodsEntity implements Serializable {
public String imgPath;//图片地址
public String goodsName;//货物名称
public String goodsPrice;//货物价格
public String goodsNameTitle;//商品名称标签
public String goodsPriceTitle;//商品价格标签
public GoodsEntity() {
}
public GoodsEntity(String imgPath, String goodsName, String goodsPrice,String goodsNameTitle,String goodsPriceTitle) {
this.imgPath = imgPath;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNameTitle = goodsNameTitle;
this.goodsPriceTitle = goodsPriceTitle;
}
//图片的路径获取方法
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
//商品名字的获取方法
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
//商品名字标签的获取方法
public String getGoodsNameTitle(){
return goodsNameTitle;
}
public void setGoodsNameTitle(String goodsNameTitle){
this.goodsNameTitle = goodsNameTitle;
}
//商品价格标签的获取方法
public String getGoodsPriceTitle(){
return goodsPriceTitle;
}
public void setGoodsPriceTitle(String goodsPriceTitle){
this.goodsPriceTitle = goodsPriceTitle;
}
//商品价格的获取方法
public String getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(String goodsPrice) {
this.goodsPrice = goodsPrice;
}
@Override
public String toString() {
return "GoodsEntity{" +
"imgPath='" + imgPath + '\'' +
", goodsName='" + goodsName + '\'' +
", goodsNameTitle='" + goodsNameTitle + '\'' +
", goodsPrice='" + goodsPrice + '\'' +
", goodsPriceTitle='" + goodsPriceTitle + '\'' +
'}';
}
}
- 创建ReclerView的adapter
class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.myViewHolder> {
private OnItemClickListener onItemClickListener;
private Context context;
private ArrayList<GoodsEntity> goodsEntities;
public LinearAdapter(Context context,ArrayList<GoodsEntity> goodsEntities){
this.context = context;
this.goodsEntities = goodsEntities;
}
@NonNull
@Override
public LinearAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = View.inflate(context, R.layout.layout_linear_item,null);
return new myViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull LinearAdapter.myViewHolder holder, int position) {
GoodsEntity data = goodsEntities.get(position);
holder.mItemGoodsName.setText(data.goodsName);
holder.mItemGoodsPrice.setText(data.goodsPrice);
holder.mItemGoodsNameTitle.setText(data.goodsNameTitle);
holder.mItemGoodsPriceTitle.setText(data.goodsPriceTitle);
}
@Override
public int getItemCount() {
return goodsEntities.size();
}
class myViewHolder extends RecyclerView.ViewHolder {
//定义控件
private ImageView mItemGoodsImg;
private TextView mItemGoodsName;
private TextView mItemGoodsPrice;
private TextView mItemGoodsNameTitle;
private TextView mItemGoodsPriceTitle;
public myViewHolder(@NonNull View itemView) {
super(itemView);
//找到控件
mItemGoodsImg = itemView.findViewById(R.id.item_goods_img);
mItemGoodsName = itemView.findViewById(R.id.item_goods_name);
mItemGoodsPrice = itemView.findViewById(R.id.item_goods_price);
mItemGoodsNameTitle = itemView.findViewById(R.id.item_goods_nametitle);
mItemGoodsPriceTitle = itemView.findViewById(R.id.item_goods_pricetitle);
//设置点击事件
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener!=null){
onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
}
}
});
}
}
//设置点击事件监听器
public interface OnItemClickListener {
public void OnItemClick(View view, GoodsEntity data);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}
- weixinFragment:
public class weixinFragment extends Fragment {
private RecyclerView mRvMain; //定义ReclerView控件
private View view;//定义view来设置fragment中的layout
private ArrayList<GoodsEntity> goodsEntities = new ArrayList<GoodsEntity>();
private LinearAdapter mLinearAdapter;
public weixinFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.tab01, container, false);
initRecyclerView();
initData();
return view;
}
//
private void initRecyclerView() {
mRvMain = (RecyclerView)view.findViewById(R.id.rv_main);
mLinearAdapter = new LinearAdapter(getActivity(),goodsEntities);
mRvMain.setAdapter(mLinearAdapter);
mRvMain.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
mRvMain.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
mLinearAdapter.setOnItemClickListener(new LinearAdapter.OnItemClickListener() {
@Override
public void OnItemClick(View view, GoodsEntity data) {
Toast.makeText(getActivity(),"图片售罄",Toast.LENGTH_SHORT).show();
}
});
}
//
private void initData(){
for (int i=0;i<10;i++){
GoodsEntity goodsEntity = new GoodsEntity();
goodsEntity.setGoodsNameTitle(" 图片名称:");
goodsEntity.setGoodsName("图片序号"+i);
goodsEntity.setGoodsPriceTitle(" 图片价格:");
goodsEntity.setGoodsPrice("1"+i*100+"RMB");
goodsEntities.add(goodsEntity);
}
}
}