RecyclerView的用法
这是一个RecyclerView 的一个最简单实例
如果当前的APP还不支持RecyclerView那么需要配置一下,在build.gradle 文件里面添加
implementation 'androidx.recyclerview:recyclerview:1.1.0'
RecyclerActivity.java文件
public class RecyclerActivity extends AppCompatActivity { private RecyclerView recyclerView; private ArrayList<String> mDatas = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recycler_activity); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); for(int i=0 ; i<120 ; i++){ mDatas.add("第"+i+"个"); } //System.out.println(mDatas); //设置垂直滚动或者水平滚动 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); MyAdapter myAdapter = new MyAdapter(this,mDatas); recyclerView.setAdapter(myAdapter); } }
补充:如果要实现为网格的布局则将上面的垂直滚动替换为,这是4列的布局
//设置成为4列 GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4); recyclerView.setLayoutManager(gridLayoutManager);
recycler_activity xml文件,在这个文件中创建了RecyclerView标签
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="recyclerView" android:textSize="18sp" android:gravity="center" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" > </androidx.recyclerview.widget.RecyclerView> </LinearLayout>
接下来创建Adapter文件 MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context mContext; private ArrayList<String> mData; public MyAdapter(Context context,ArrayList<String> datas){ this.mContext = context; this.mData = datas; } @NonNull @Override //该方法用于创建列表项组件。使用该方法创建的组件会被自动缓存。 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_1,parent,false); return new NormalHolder(view); } @Override //该方法负责为列表项组件绑定数据,每次组件重新显示出来都会执行该方法 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { NormalHolder normalHolder = (NormalHolder) holder; normalHolder.mTV.setText(mData.get(position)); } @Override //该方法的返回值决定包含多少个列表项 public int getItemCount() { return mData.size(); } public class NormalHolder extends RecyclerView.ViewHolder{ public TextView mTV; public NormalHolder(@NonNull View itemView) { super(itemView); mTV = (TextView) itemView.findViewById(R.id.tv_item_1); } } }
在MyAdapter文件中 recycler_item_1作为一个列表项
<?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"> <TextView android:id="@+id/tv_item_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="recycler_item" /> </LinearLayout>
好了,这就是一个简单的RecyclerView 实例的全部代码。