初识RecyclerView
效果图
RecyclerView与ListView对比(官方)
RecyclerView 类别将通过提供下列功能简化庞大数据集的显示与处理:
- 用于项目定位的布局管理器
- 用于通用项目操作(例如删除或添加项目)的默认动画(RecyclerView 在默认情况下启用增添与删除项目的动画)
您也可灵活选择如何为 RecyclerView 小组件定义定制布局管理器与动画。
使用
依赖
dependencies {
...
compile 'com.android.support:recyclerview-v7:23.0.+'
}
布局管理器
RecyclerView 提供这些内置布局管理器:
- LinearLayoutManager 以垂直或水平滚动列表方式显示项目。
- GridLayoutManager 在网格中显示项目。
- StaggeredGridLayoutManager 在分散对齐网格中显示项目。
适配器
package com.kongqw.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by kongqw on 2016/1/30.
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
// RecyclerView.ViewHolder
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
// 用来创建新视图(由布局管理器调用)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
// 用来替换视图的内容(由布局管理器调用)
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
}
// 返回数据集的大小(由布局管理器调用)
@Override
public int getItemCount() {
return 0;
}
}
布局
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
初始化
// 找到RecyclerView控件
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// 如果数据的填充不会改变RecyclerView的布局大小,那么这个设置可以提高RecyclerView的性能
mRecyclerView.setHasFixedSize(true);
// 设置这个RecyclerView是线性布局
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
填充数据
// 给RecyclerView添加一个适配器显示数据
RecyclerView.Adapter mAdapter = new MyAdapter(100);
mRecyclerView.setAdapter(mAdapter);
效果图Demo源码
布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.kongqw.myapplication.MainActivity"> <android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" /> </RelativeLayout>my_text_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="10dp"
card_view:cardCornerRadius="4dp"> <TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:textSize="30px" />
</android.support.v7.widget.CardView>
</LinearLayout>
主类
package com.kongqw.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到RecyclerView控件
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// 如果数据的填充不会改变RecyclerView的布局大小,那么这个设置可以提高RecyclerView的性能
mRecyclerView.setHasFixedSize(true);
// 设置这个RecyclerView是线性布局
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// 给RecyclerView添加一个适配器显示数据
RecyclerView.Adapter mAdapter = new MyAdapter(100);
mRecyclerView.setAdapter(mAdapter);
}
}
适配器
package com.kongqw.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by kongqw on 2016/1/30.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private int mCount;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
}
}
public MyAdapter(int count) {
mCount = count;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
ViewHolder vh = new ViewHolder(v);
vh.mTextView = (TextView) v.findViewById(R.id.info_text);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText("这是第 [ " + position + " ] 条数据");
}
@Override
public int getItemCount() {
return mCount;
}
}