RecyclerView是Android 5.0推出的,是support-v7包中的新组件,它被用来代替ListView和GridView,并且能够实现瀑布流的布局,更加高级并且更加灵活,提供更为高效的回收复用机制,同时实现管理与视图的解耦合。
官方文档解释:
A flexible view for providing a limited window into a large data set.
我将通过演示一个简单的例子让大家来感受RecyclerView的魅力。
1.添加依赖
1.1打开File —> Project Structure或点击快捷图标或者快捷键ctrl+shift+alt+s
1.2选中app —> Dependencies —> 点击加号 —> Library dependence
1.3在选择框中输入recyclerview,选择v7包
1.4成功添加
2.添加布局
在activity_main.xml中添加如下代码
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
3.新建一个item_dome.xml,为RecyclerView内的元素设定xml样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="测试"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:textColor="@android:color/holo_red_dark"
android:background="@android:color/holo_blue_dark" />
</LinearLayout>
4.创建适配器继承RecyclerView.Adapter
步骤:
1.创建适配器类继承自RecyclerView.Adapter,泛型传入RecyclerView.ViewHolder类。
2.创建内部类即RecyclerView.ViewHolder类的子类,并初始化item的控件。
3.重写RecyclerView.Adapter类的相关方法。
package lession.example.com.recyclerviewdemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/*
① 创建一个继承RecyclerView.Adapter<VH>的Adapter类
② 创建一个继承RecyclerView.ViewHolder的静态内部类
③ 在Adapter中实现3个方法:
onCreateViewHolder()
onBindViewHolder()
getItemCount()
*/
public class RecycleAdapterDome extends RecyclerView.Adapter<RecycleAdapterDome.MyViewHolder>{
private Context context;
private List<String> list;
private View inflater;
//构造方法,传入数据
public RecycleAdapterDome(Context context, List<String> list){
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建ViewHolder,返回每一项的布局
inflater = LayoutInflater.from(context).inflate(R.layout.item_dome,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(inflater);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//将数据和控件绑定
holder.textView.setText(list.get(position));
}
@Override
public int getItemCount() {
//返回Item总条数
return list.size();
}
//内部类,绑定控件
class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
}
}
}
5.MainActivity.java中
步骤:
1.获取RecyclerView对象 。
2.初始化数据 。
3.适配器实例化 。
4.设置LayoutManager
5. 设置Adapter 。
package lession.example.com.recyclerviewdemo;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;//声明RecyclerView
private RecycleAdapterDome adapterDome;//声明适配器
private Context context;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
list = new ArrayList<>();
for (int i=0;i<10;i++){
list.add("这是第"+i+"个测试");
}
//
adapterDome = new RecycleAdapterDome(context,list);
/*
与ListView效果对应的可以通过LinearLayoutManager来设置
与GridView效果对应的可以通过GridLayoutManager来设置
与瀑布流对应的可以通过StaggeredGridLayoutManager来设置
*/
//LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
//RecyclerView.LayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
//GridLayoutManager manager1 = new GridLayoutManager(context,2);
//manager1.setOrientation(GridLayoutManager.VERTICAL);
//StaggeredGridLayoutManager manager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapterDome);
}
}
运行结果:
这就是RecyclerView的简单使用,如果转载以及CV操作,请务必注明出处,谢谢!