💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**转载请注明本文出自Cym的博客([http://blog.csdn.net/cym492224103](http://blog.csdn.net/cym492224103)**),谢谢支持!** ** ** 前言 大家都知道ListView在我们开发中很常用,Android5.0的到来,它给我们带来了一个替代 ListView的新的控件,也可以说是ListView的升级版,那就是RecyclerView。 接下来就让我们看一下,它们的作用的用法吧~! RecyclerView [RecyclerView](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)控件是比[ListView](http://developer.android.com/reference/android/widget/ListView.html)更先进、灵活的版本。 该[RecyclerView](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)类通过以下几点简化了显示和处理大量的数据: 1.为每个条目位置提供了布局管理器(**RecyclerView.setLayoutManager**) [RecyclerView](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)提供这些内置的布局管理器: [LinearLayoutManager](http://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html)(用来)显示垂直或水平滚动的列表项 [GridLayoutManager](http://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html)(用来)显示网格中的item(项) [StaggeredGridLayoutManager](http://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html)(用来)显示交错的网格item(项目) 可以通过继承[RecyclerView.LayoutManager](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html)类来自定义自己的布局管理器。 2.为每个条目设置了操作动画(**RecyclerView.setItemAnimator**) 使用步骤: 1.导入sdk/extras/android/support/v7/recyclerview/libs/android-support-v7-recyclerview.jar  2.xml配置 ~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" tools:listitem="@layout/list_basic_item" /> </RelativeLayout> ~~~ 3.Activity代码 ~~~ public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取RecyclerView对象 final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // 创建线性布局管理器(默认是垂直方向) final LinearLayoutManager layoutManager = new LinearLayoutManager(this); // 为RecyclerView指定布局管理对象 recyclerView.setLayoutManager(layoutManager); // 创建Adapter final SampleRecyclerAdapter sampleRecyclerAdapter = new SampleRecyclerAdapter(); // 填充Adapter recyclerView.setAdapter(sampleRecyclerAdapter); } } ~~~ 4.Adapter代码 ~~~ public class SampleRecyclerAdapter extends RecyclerView.Adapter<SampleRecyclerAdapter.ViewHolder> { private final ArrayList<SampleModel> sampleData = DemoApp.getSampleData(20); // 用于创建控件 @Override public ViewHolder onCreateViewHolder(ViewGroup parentViewGroup, int i) { // 获得列表项控件(LinearLayer对象) // list_basic_item.xml布局文件中只包含一个<LinearLayer>标签,在该标签中包含 // 了一个<TextView>标签 // item是LinearLayout对象 View item = LayoutInflater.from(parentViewGroup.getContext()).inflate( R.layout.list_basic_item, parentViewGroup, false); return new ViewHolder(item); } // 为控件设置数据 @Override public void onBindViewHolder(ViewHolder viewHolder, final int position) { // 获取当前item中显示的数据 final SampleModel rowData = sampleData.get(position); // 设置要显示的数据 viewHolder.textViewSample.setText(rowData.getSampleText()); viewHolder.itemView.setTag(rowData); } @Override public int getItemCount() { return sampleData.size(); } // 删除指定的Item public void removeData(int position) { sampleData.remove(position); // 通知RecyclerView控件某个Item已经被删除 notifyItemRemoved(position); } // 在指定位置添加一个新的Item public void addItem(int positionToAdd) { sampleData.add(positionToAdd,new SampleModel("新的列表项" + new Random().nextInt(10000))); // 通知RecyclerView控件插入了某个Item notifyItemInserted(positionToAdd); } public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textViewSample; public ViewHolder(View itemView) { super(itemView); textViewSample = (TextView) itemView .findViewById(R.id.textViewSample); } } } ~~~