安卓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);//適配

        /**
         * 回到頂部監(jiān)聽
         */
        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;
    }

    /**
     * 創(chuàng)建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的使用基礎,應該能很容易看懂這個。
如果有什么看不懂的,可以在下方評論,我會盡量回答

注:有什么地方有問題的,希望各位不吝賜教,多謝!

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,216評論 0 3
  • 請允許我借鑒前輩們的東西~~~~ 感激不盡~~~~~ 以下為Android 框架排行榜 么么噠~ Android...
    嗯_新閱讀 2,544評論 3 32
  • 博客原文鏈接 Android百大框架排行榜(轉) 說明: 無聊寫一篇筆記式文章. 精力有限,很多錯誤之處,受時間與...
    碼農朱同學閱讀 3,120評論 0 27
  • 這篇文章分三個部分,簡單跟大家講一下 RecyclerView 的常用方法與奇葩用法;工作原理與ListView比...
    LucasAdam閱讀 4,709評論 0 27
  • 我不知道我來這兒干嘛 好像只有這沒人認識我 這樣我說什么發(fā)表什么都不會有熟人看到 很自在 非常自在 可能我今天一天...
    阿變Chain閱讀 105評論 0 0

友情鏈接更多精彩內容