安卓RecyclerView的基本使用,简单易懂

最开始接触RecyclerView的时候,百度各种教程,感觉都很复杂,不容易看懂,或许是讲的比较深,这里不谈RecyclerView的好处,只谈最基本的使用,让他能跑起来再说。

效果图

recyclerView.gif

首先在Gradle中引入依赖:compile 'com.android.support:design:28.0.0'

上java目录截图和xml截图

项目目录.png

xml:

fragment_02.xml:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:background="#56abe4"
        android:fontFamily="cursive"
        android:gravity="center"
        android:text="RecyclerView"
        android:textColor="#FFF"
        android:textSize="34sp"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"></android.support.v7.widget.RecyclerView>

    <TextView
        android:id="@+id/backTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="50dp"
        android:text="回到顶部"
        android:textColor="#931f26"
        android:textSize="30sp"
        android:textStyle="bold" />

</RelativeLayout>

recyclerview_fg2.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="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:src="@drawable/girl" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="20dp" />

            <TextView
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="20dp" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

java

LinkMan(实体类)

package com.example.vulcan.newstudiodemo.fragment.recyclerview;

public class LinkMan {
    /**
     * 姓名
     */
    private String name;

    /**
     * 电话号码
     */
    private String phone;

    public LinkMan(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

Fragment2:

package com.example.vulcan.newstudiodemo.fragment.recyclerview;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.vulcan.newstudiodemo.R;

import java.util.ArrayList;
import java.util.List;

public class Fragment2 extends Fragment {
    private TextView backTop;//回到顶部按钮
    private RecyclerView recyclerView;

    AdapterRecycler adapterRecycler;//定义适配器

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_02, null);

        initView(view);

        //region 数据模拟
        List<LinkMan> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(new LinkMan("莉莉" + i, "123456789"));
        }
        //endregion

        adapterRecycler = new AdapterRecycler(getActivity(), list);
//        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//设置布局为线性布局
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));//设置布局为网格布局,第二个参数3代表每行三个
        recyclerView.setAdapter(adapterRecycler);//适配

        /**
         * 回到顶部监听
         */
        backTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.scrollToPosition(0);
            }
        });


        return view;
    }

    /**
     * 控件初始化
     * @param view
     */
    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        backTop = (TextView) view.findViewById(R.id.backTop);
    }
}

AdapterRecycler:

package com.example.vulcan.newstudiodemo.fragment.recyclerview;

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 com.example.vulcan.newstudiodemo.R;

import java.util.List;

public class AdapterRecycler extends RecyclerView.Adapter<AdapterRecycler.VH> {
    Context context;
    List<LinkMan> list;

    /**
     * 构造方法,获得上下文和数据集合
     *
     * @param context
     * @param list
     */
    public AdapterRecycler(Context context, List<LinkMan> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * 创建ViewHolder,解析XML
     *
     * @param viewGroup
     * @param i
     * @return
     */
    public VH onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_fg2, null);
        return new VH(view);
    }

    /**
     * 操作列表项
     *
     * @param vh
     * @param i
     */
    @Override
    public void onBindViewHolder(VH vh, int i) {
        vh.name.setText(list.get(i).getName());
        vh.phone.setText(list.get(i).getPhone());
    }

    /**
     * 获得列表项的数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 控件初始化
     */
    public static class VH extends RecyclerView.ViewHolder {
        TextView name;
        TextView phone;

        public VH(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
        }
    }
}

代码中注释的比较清晰了,如果有ListView的使用基础,应该能很容易看懂这个。
如果有什么看不懂的,可以在下方评论,我会尽量回答

注:有什么地方有问题的,希望各位不吝赐教,多谢!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,731评论 0 3
  • 请允许我借鉴前辈们的东西~~~~ 感激不尽~~~~~ 以下为Android 框架排行榜 么么哒~ Android...
    嗯_新阅读 2,160评论 3 32
  • 博客原文链接 Android百大框架排行榜(转) 说明: 无聊写一篇笔记式文章. 精力有限,很多错误之处,受时间与...
    码农朱同学阅读 2,757评论 0 27
  • 这篇文章分三个部分,简单跟大家讲一下 RecyclerView 的常用方法与奇葩用法;工作原理与ListView比...
    LucasAdam阅读 4,436评论 0 27
  • 我不知道我来这儿干嘛 好像只有这没人认识我 这样我说什么发表什么都不会有熟人看到 很自在 非常自在 可能我今天一天...
    阿变Chain阅读 92评论 0 0