RecyclerView

一、RecyclerView不是Android自带的,需要导入

          implementation 'com.android.support:recyclerview-v7:28.+'

二、引入RecyclerView

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recyclerviewtest.MainActivity">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

三、创建RecyclerView的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/fruit_name"
        android:layout_below="@id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>

四、创建Java实体类

 

public class Fruit {

    private String name;

    private int imageId;

    public Fruit(){

    }

    public Fruit(String name, int imageId){
        this.name = name;
        this.imageId = imageId;
    }

    public String getName(){
        return name;
    }

    public int getImageId(){
        return imageId;
    }
}

五、创建Adapter

public class FruitAdater extends RecyclerView.Adapter<FruitAdater.ViewHolder> {

    private List<Fruit> mFruitList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;
        View fruitView;

        public ViewHolder(View view){
            super(view);
            fruitView = view;
            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            fruitName = (TextView) view.findViewById(R.id.fruit_name);
        }
    }

    public FruitAdater(List<Fruit> fruitList){
        mFruitList = fruitList;
    }

    /**
     * onCreateViewHolder()创建ViewHoder实例
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("FruitAdater", "parent:" + parent + " viewType: " + viewType);
     
  //实例化对象
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fruit_item,parent,false);
     
  //将加载的view布局传到构造函数中
        final ViewHolder holder = new ViewHolder(view);
   
    //设置点击事件
        holder.fruitView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
               
//获取当前布局在Adapter的最新地址
                int position = holder.getLayoutPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(v.getContext(),fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        //fruitImage(图片)点击事件
        holder.fruitImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
             
  //返回数据在Adapter的地址(也许位置的变化还未来得及刷新到布局中)
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(v.getContext(),"OnClickListener",Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    /**
     * onBindViewHolder()方法将Recycler的子项的数据赋值
     */
    @Override
    public void onBindViewHolder(FruitAdater.ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    }

    /**
     * getItemCount()获取RecyclerView的子项,返回数据源的长度
     */
    @Override
    public int getItemCount() {
        return mFruitList.size();
    }
}

六、引用

 

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设置布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //更改为横向排列
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        //将集合传入
        FruitAdater adater = new FruitAdater(fruitList);
        recyclerView.setAdapter(adater);

    }

    private void initFruits(){
        for(int i = 0; i < 3; i++){
            Fruit apple = new Fruit("Apple",R.drawable.apple);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana",R.drawable.banana);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange",R.drawable.orange);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Pear",R.drawable.pear);
            fruitList.add(pear);
            Fruit grape = new Fruit("Grape",R.drawable.grapes);
            fruitList.add(grape);
            Fruit mango = new Fruit("Mango",R.drawable.mango);
            fruitList.add(mango);
        }
    }
}

七、效果,目前为横向排列,可左右滑动

RecyclerView

 

 

RecyclerView瀑布流布局

      //更改StaggeredGridLayoutManager

 //瀑布流布局,三列垂直布局
        StaggeredGridLayoutManager layoutManager = new  StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

效果

RecyclerView