RecyclerView和百分比布局类似,属于新增的控件,为了能使在各个版本上运行,将RecyclerView定义到support库中,要想使用的话,需要在build.gradle中添加相应的库
dependencies{
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation ‘com.android.support:recyclerview-v7:29.0.0’
implementation 'com.android.support:appcompat-v7:29.0.
testImplementation ‘junit:junit:4.13-beta-3’
旧版本代码如下:
dependencies{
compile fileTree(dir: ‘libs’, include: [’*.jar’])
compile ‘com.android.support:recyclerview-v7:29.0.0’
compile 'com.android.support:appcompat-v7:29.0.
testCompile ‘junit:junit:4.13-beta-3’
}
添加完之后需要点击一下Sync Now来进行同步
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
其中旧版本代码如下
<?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.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
1、定义一个实体类作为RecyclerView适配器的适配类型,新建类Fruit,代码如下
package com.example.recycleviewtest;
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;
}
}
Fruit类中有两个字段,name是水果的名字,imageId是水果对应的图片的资源id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp">
<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"
android:layout_marginTop="10dp"/>
</LinearLayout>
1、让这个适配器继承自RecyclerView.Adapter,并将泛型指定为FruitAdapter.ViewHolder。ViewHolder是在FruitAdapter中定义的一个内部类。
package com.example.recycleviewtest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
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;
}
@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;
}
@Override
public void onBindViewHolder(ViewHolder holder,int position){
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount(){
return mFruitList.size();
}
}
解释:
1、首先定义了一个内部类ViewHolder,其要继承RecyclerView.ViewHolder。然后ViewHolder的构造函数中要传入一个View参数,这个参数通常是RecycleView子项的最外层布局,那么能通过findViewById()方法来获取到布局中ImageView和TestView的实例。
2、FruitAdapter中有一个构造函数,这个方法是将要展示的数据源传进来并赋值给一个全局变量mFruitList。
3、FruitAdapter继承自RecyclerView.Adapter,那么必须重写onCreateViewHolder()、onBindViewHolder()、getItemCount()这三个方法,onCreateViewHolder()是用与创建ViewHolder实例的,onBindViewHolder()对RecyclerView子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行。getItemCount()是告诉RecyclerView一共有几个子项,直接返回数据源的长度。
MainActivity中的代码
package com.example.recycleviewtest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
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);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits(){
for(int i=0;i<2;i++){
Fruit apple = new Fruit("apple",R.mipmap.fruits);
fruitList.add(apple);
}
}
}
方法中的第二个参数,传入的是listview(或Recyclerview),
第二个参数指的是加载布局的root
Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (ifattachToRoot is false.)
大概就是说如果后面attachToRoot为true的情况下,这个布局会被解析并加载在root下面,如果为false,则会依照root去解析该xml并返回view,但是这个view不会被加载到root里。
其实如果为false,就是讲xml解析了,并依照root的类型给生成的view set一个LayoutParams ,但不将其add到root里。