[toc]
RecyclerView基本用法
1.在dependencies闭包中添加依赖
①首先切换成project模式
②在app--->build.gradle中的dependencies添加如下代码
compile 'com.android.support:recyclerview-v7:24.2.1'
2.然后再activity_main.xml中添加RecyclerView控件:
//因为RecyclerView不是内置在系统SDK中的,所以需要把完整的路径写出来
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
3.新建fruit类
public class Fruit {
private String name;
private int imageId;
//自定义构造函数
public Fruit(String name,int imageId)
{
this.name=name;
this.imageId=imageId;
}
public String getName()
{
return name;
}
public int getImageId()
{
return imageId;
}
}
4.新建fruit_item.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" //如果是match_parent,则一个子项会占满屏幕
>
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
5.新建适配器和类
为RecyclerView建一个适配器,新建FruitAdapter类,让这个适配器继承RecyclerView.Adapter,并将泛型指定为FruitAdapter.ViewHolder。其中ViewHolder是FruitAdapter中定义的一个内部类;
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
//定义ViewHolder的构造函数
public ViewHolder(View view){
super(view);
fruitImage=(ImageView) view.findViewById(R.id.fruit_image);
fruitName=(TextView) view.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList=fruitList;
}
//创建ViewHolder实例,用这个函数将fruit_item布局加载进来,
//并将加载出来的布局传到ViewHolder构造函数中
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.fruit_item,parent,false);
ViewHolder holder=new ViewHolder(view);
return holder ;
}
//用于对RecyclerView子项的数据进行赋值
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit=mFruitList.get(position); //获取当前位置的实例
holder.fruitName.setText(fruit.getName()); //设置条的内容
holder.fruitImage.setImageResource(fruit.getImageId()); //设置图片的来源
}
//告诉我们RecyclerView一共有多少子项,直接返回数据源的长度
@Override
public int getItemCount() {
return mFruitList.size();
}
}
6.修改MainActivity中的代码:
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initfruit(); //初始化数据
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recycler_view);
//LayoutManger用于指定RecyclerView的布局方式,
//使用LinearLayoutManager是表示RecyclerView使用线性布局
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
//设置RecyclerView的布局方式为线性布局
recyclerView.setLayoutManager(linearLayoutManager);
//创建FruitAdapter实例,并将水果数据传入FruitAdapter的构造函数中
FruitAdapter adapter=new FruitAdapter(fruitList);
//用setAdapter()来完成适配器的设置
recyclerView.setAdapter(adapter);
}
private void initfruit(){
for (int i=0;i<5;i++){
Fruit apple=new Fruit("Apple",R.drawable.a);
fruitList.add(apple);
Fruit banana=new Fruit("Banana",R.drawable.b);
fruitList.add(banana);
Fruit orange=new Fruit("Orange",R.drawable.c);
fruitList.add(orange);
Fruit grape=new Fruit("Grepe",R.drawable.d);
fruitList.add(grape);
}
}
}
RecyclerView实现横向滚动
在MainActivity中添加一行代码:
public class MainActivity extends AppCompatActivity {
(.......省略.......)
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recycler_view);
//linearLayoutManager用于指定RecyclerView的布局方式,
//使用LinearLayoutManager是表示RecyclerView使用线性布局
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
//设置线性布局的排列方向为水平的,这样就可以横向滚动了,起初的布局排列方向默认为垂直
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//设置RecyclerView的布局方式为线性布局
recyclerView.setLayoutManager(linearLayoutManager);
//创建FruitAdapter实例,并将水果数据传入FruitAdapter的构造函数中
FruitAdapter adapter=new FruitAdapter(fruitList);
//用setAdapter()来完成适配器的设置
recyclerView.setAdapter(adapter);
}