美文网首页程序员
RecyclerView之小白

RecyclerView之小白

作者: Amy木婉清 | 来源:发表于2020-08-31 16:12 被阅读0次

    什么是RecyclerView?

    RecyclerView是谷歌V7包下新增的控件,用来替代ListView的使用,在RecyclerView标准化了ViewHolder类似于ListView中convertView用来做视图缓.是一种全新的列表展现形式.
    关于他的使用步骤:
    对于一个小白,简单的总结了一下使用RecyclerView必须要有的几个东东
    (1)item.xml(自定义公用的那部分布局)
    (2)Activity(简单来说用来显示你自定义的那部分布局 页面)
    (3)main.xml(使用recyclerview用的)
    (4)adapter(适配器,所有的实际操作都在适配器中进行)

    那让小白开启RecyclerView之旅吧

    写在前面

    我们来说明一个问题,目前由于Android Studio不停升级,现在很多都是androidx版本,因此导入的依赖也都是x版本的.
    先看一下demo实现的效果:文章看到最后附赠惊喜

    recycler.gif

    1.导入依赖

    implementation ‘androidx.recyclerview:recyclerview:1.0.0’
    

    2.main.xml使用recyclerview:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    3.创建Fruit类,用来定义水果的图片和名字

    package com.example.recyclerviewmy;
    
    public class Fruit {
        private String FruitName;
        private int ImageId;
    
        public Fruit(String fruitName, int imageId) {
            FruitName = fruitName;
            ImageId = imageId;
        }
    
        public String getFruitName() {
            return FruitName;
        }
    
        public int getImageId() {
            return ImageId;
        }
    }
    

    4.创建自定义通用item:

    //这个xml里主要是显示图片和文字
    <?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="wrap_content">
    <ImageView
        android:id="@+id/iv_fruit"
        android:layout_width="30dp"
        android:layout_height="30dp"
        />
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
    

    5.创建adapter以及进行具体操作:(包括自定义构造函数FruitAdapter)
    注意(Adapter继承于recyclerview下的adapter,并且ViewHolder千篇一律 ,都是你创建的adapter.ViewHolder)

    package com.example.recyclerviewfruit;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.List;
    
    public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
        public List<Fruit> mFruitList;
        public FruitAdapter (List<Fruit> fruitList){
            mFruitList = fruitList;
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Fruit fruit = mFruitList.get(position);
            holder.fruitName.setText(fruit.getName());
            holder.fruitImage.setImageResource(fruit.getImageId());
    
        }
    
        @Override
        public int getItemCount() {
            return mFruitList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView fruitImage;
            TextView fruitName;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                fruitImage = itemView.findViewById(R.id.fruit_image);
                fruitName = itemView.findViewById(R.id.fruitName);
            }
        }
    }
    
    

    6.利用activity展示recyclerview内容

    package com.example.recyclerviewfruit;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.os.Bundle;
    
    import java.util.ArrayList;
    import java.util.List;
    
    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 = findViewById(R.id.recyclerView);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(linearLayoutManager);
            FruitAdapter adapter = new FruitAdapter(fruitList);
            recyclerView.setAdapter(adapter);
        }
    
        private void initFruits() {
            for (int i = 0; i < 2; i++) {
                Fruit apple = new Fruit("Apple", R.drawable.appele);
                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.xigua);
                fruitList.add(watermelon);
                Fruit pear = new Fruit("Pear", R.drawable.pear);
                fruitList.add(pear);
                Fruit grape = new Fruit("Grape", R.drawable.grape);
                fruitList.add(grape);
                Fruit pineapple = new Fruit("Pineapple", R.drawable.boluo);
                fruitList.add(pineapple);
                Fruit strawberry = new Fruit("Strawberry", R.drawable.caomei);
                fruitList.add(strawberry);
                Fruit cherry = new Fruit("Cherry", R.drawable.cherry);
                fruitList.add(cherry);
                Fruit mango = new Fruit("Mango", R.drawable.manglo);
                fruitList.add(mango);
            }
        }
    }
    

    本人采坑:
    demo写好后运行显示空白页面,由于Adapter中count条目返回值自定义为0,应该显示所显示的条目:

     @Override
        public int getItemCount() {
            return mFruitList.size();
        }
    

    附:demo链接:
    https://github.com/Rabbittpig/RecyclerViewFruit

    相关文章

      网友评论

        本文标题:RecyclerView之小白

        本文链接:https://www.haomeiwen.com/subject/chnusktx.html