【Android 基础】RecyclerView 概述以及使用步骤
来源:互联网 发布:网络监控服务器搭建 编辑:程序博客网 时间:2024/04/29 19:44
RecyclerView是什么?
RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。
RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。
总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果。
一般使用步骤:
1.在build.gradle配置RecyclerView的库
compile 'com.android.support:recyclerview-v7:23.3.0'
2.在布局文件写定义RecyclerView
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> . . . <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
3.在代码中
(1)实例化RecyclerView
(2)初始化数据
(3)设置适配器
(4)设置LayoutManager
..private RecyclerView recyclerview;private ArrayList<String> datas;private MyRecyclerViewAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1 实例化RecyclerView recyclerview = (RecyclerView) findViewById(R.id.recyclerview); initData(); //3 设置RecyclerView的适配器 adapter = new MyAdapter(RecyclerViewActivity.this,datas); recyclerview.setAdapter(adapter); //4.设置LayoutManager recyclerview.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this, LinearLayoutManager.VERTICAL, false));}/** * 2 初始化数据 */private void initData() { datas = new ArrayList<>(); for (int i = 0; i < 100; i++) { datas.add("Content" + i); }}
4.自定义适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private final Context context; private final ArrayList<String> datas; public MyAdapter(Context context,ArrayList<String> datas){ this.context = context; this.datas = datas; } /** * 相当于ListView适配器中的getView的创建holder布局 * * @param parent * @param viewType * @return */ @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_hello, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv_text.setText(datas.get(position)); holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher); } @Override public int getItemCount() { return datas.size(); } class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv_text; private ImageView iv_icon; public MyViewHolder(View itemView) { super(itemView); tv_text = (TextView) itemView.findViewById(R.id.tv_text); iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon); } }}
5.设置Item布局:
item_hello.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="wrap_content" android:background="#33000000" android:padding="2dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/iv_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:src="@drawable/default" /> <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:padding="3dp" android:text="内容" android:textColor="#000000" android:textSize="15sp" /> </LinearLayout></RelativeLayout>
使用上其实和ListView差不多。
1 0
- 【Android 基础】RecyclerView 概述以及使用步骤
- Android-RecyclerView 实现步骤
- RecyclerView 使用步骤
- Android基础总结十二:RecyclerView简单使用
- RecyclerView原理以及使用
- Android MaterialDesign控件使用以及RecyclerView,ItemDecoration的基本使用
- Android MaterialDesign概述样式以及使用
- RecyclerView 的简单使用步骤
- RecyclerView的基本使用步骤
- #Android学习#RecyclerView基础
- android studio recyclerview基础
- android 基础 ---> RecyclerView
- recyclerView的基础使用
- RecyclerView的基础使用
- android TV开发之RecyclerView的使用以及自动加载
- android RecyClerView 使用的教程以及那些坑
- android TV开发之RecyclerView的使用以及自动加载
- Android基础之handler使用步骤
- 网络同步
- 人脸识别主要算法原理
- elasticsearch 自定义索引 _all 设置规则
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- Spring-mybatis事务配置
- 【Android 基础】RecyclerView 概述以及使用步骤
- 58-System V 消息队列
- 微信小程序使用font-awesome图标库
- poj_2828 Buy Tickets(线段树)
- Program "g++" not found in PATH
- Leetcode 282, Expression Add Operators
- Java考试题之三
- JPEG系列二 JPEG文件中的EXIF(下)
- jQuery遮罩效果