Android RecyclerView是一个加载列表最方便的组件,也是使用非常广泛的,下面就来学习一下RecyclerView的使用吧!
步骤一:在layout xml中添加RecyclerView组件,代码如下:
<android.support.v7.widget.RecyclerView android:id = "@+id/gridView" android:layout_width="750px" android:layout_height="260px" android:numColumns="4" android:scrollbars="none" android:listSelector="@null" />
步骤二:在layout layout里面添加一个文件作为RecyclerView的item,如“xxx_item.xml”文件,小编在的项目中的文件名称是“money_item.xml”,代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="180px" android:layout_height="230px" android:background="@mipmap/moneybg" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:layout_marginTop="45px"> <TextView android:id="@+id/tv_moneyNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥10.00" android:textSize="30px" android:textStyle="bold" /> <TextView android:id="@+id/tv_timeNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15px" android:text="- 10分钟 -" android:textSize="20px" /> </LinearLayout> </RelativeLayout>
效果大致如下:
步骤三:初始化RecycleView,并加载数据,这是小编在自定义Dialog里面加载RecycleView列表的示例,代码如下:
package com.xuanyin.homektv.fragment; public class PayMoneyDialog extends Dialog { private boolean cancelable = true; private RecyclerView gridView; //存储列表的数据的集合 private List<Map<String, Object>> priceMapList = new ArrayList<Map<String, Object>>(); public PayMoneyDialog(@NonNull Context context) { super(context, R.style.MyDialog); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pay_money_dialog); gridView = findViewById(R.id.gridView); //alertDialog是否可以点击外围消失 setCanceledOnTouchOutside(cancelable); setCancelable(cancelable); //加载数据并显示列表 loadPriceList(); } private void loadPriceList() { //给priceMapList集合添加数据 for (int i=1;i<10;i++){ Map<String, Object> map = new HashMap<>(); map.put("tv_moneyNum","¥"+i*10+".00"); map.put("totalMoney",i*10); if(i % 5 == 0){ map.put("currType",0); map.put("tv_timeNum","- 一首歌 -"); }else{ map.put("currType",1); map.put("tv_timeNum","- "+i*10+"分钟 -"); map.put("payTimeNum",i*10); } priceMapList.add(map); } //设置RecycleView列表 setGridView(); } public class MyHolder extends RecyclerView.ViewHolder { public TextView tv_moneyNum, tv_timeNum; public MyHolder(View itemView) { super(itemView); tv_moneyNum = (TextView) itemView.findViewById(R.id.tv_moneyNum); tv_timeNum = (TextView) itemView.findViewById(R.id.tv_timeNum); } } private static RecyclerView.Adapter simpleAdapter = null; private int curr_row = -1; private void setGridView() { // 设置数据 simpleAdapter = new RecyclerView.Adapter() { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(getContext()).inflate(R.layout.money_item,viewGroup, false); return new PayMoneyDialog.MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { Map<String, Object> map = priceMapList.get(position); ((MyHolder) viewHolder).tv_moneyNum.setText(map.get("tv_moneyNum")+""); ((MyHolder) viewHolder).tv_moneyNum.setTypeface(tf); ((MyHolder) viewHolder).tv_timeNum.setText(map.get("tv_timeNum")+""); ((MyHolder) viewHolder).tv_timeNum.setTypeface(tf); //设置RecycleView item的点击事件 viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { curr_row = position; notifyDataSetChanged(); } }); //点击item显示不同的背景 if(curr_row != position){ viewHolder.itemView.setBackgroundResource(R.mipmap.moneybg); }else{ viewHolder.itemView.setBackgroundResource(R.mipmap.moneybg_selected); } } @Override public int getItemCount() { return priceMapList.size(); } }; //设置adapter gridView.setAdapter(simpleAdapter); //设置RecycleView最小宽度 gridView.setMinimumWidth(700); //设置RecycleView横向滑动,如果是竖向滑动,则HORIZONTAL设置成VERTICAL gridView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); //设置item右边间距,下左上分别为:bottom,left,top RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() { @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.right = 10; } }; //应用间距设置属性 gridView.addItemDecoration(decoration); } }