Android Studio開發(二)使用RecyclerView實現瀑布流展示

Android Studio開發(二)RecyclerView

一、任務需求

  • 根據Android Studio開發(一)模擬微信頁面內容進行recycleview設計開發。
  • 在原有作業一上選擇一個tab添加recycleview顯示。
    顯示內容不做限定,可以是新聞、商品、球隊、明星、快遞時間軸、漫畫等。
  • 可根據課程做最基本的RecyclerView設計,也可以進行較複雜的動態控制。
    綜上,我將第一次中朋友界面進行了改動,模擬實現了基於RecyclerView的瀑布流展示。(假裝自己的微信好友是歐美歌手XD)

二、Recycler View梳理

1. Fragment, Adapter, RecyclerView, MainActivity及Data之間的關係

我們可以將上述主要劃分爲三部分,如下圖所示:
在這裏插入圖片描述

2. 各部分功能簡要說明

  • RecyclerView.xml: 本項目中對應 item.xml,放置所需的控件,實現批量添加控件。
  • Fragment.xml: 本項目中對應 tab02.xml,放置所需的控件,可包含 RecyclerView
  • MainActivity.xml: 本項目中對應 activity_main.xml,放置所需的控件,可包含 Fragment
  • Adapter.java: 本項目中對應 friendAdapter.java,具體實現item.xml中的控件,實現數據加入方法;
  • Fragment.java: 本項目中對應 friendFragment.java,具現tab02.xml,加入並傳遞對象數據至friendFragment.java中,實現加入數據。
  • MainActivity.java: 實現friendFragment.java的加入。

3. 使用RecyclerView的幾種方法:

(1)直接在.xml文件中通過拖拽的方法使用RecyclerView,項目會自動添加依賴關係(注意添加約束);
(2)手動添加v7支持庫,然後將支持庫添加到 dependencies 部分。具體實現可至官網閱覽

三、部分代碼展示

1. Friend.java

package com.example.mywechat2;

public class Friend {

    private int id;
    private int imageid;
    private String name;

    public Friend(int id, int imageid, String name) {
        this.id = id;
        this.imageid = imageid;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public int getImageid() {
        return imageid;
    }

    public String getName() {
        return name;
    }
}

2. frdFragment.java

package com.example.mywechat2;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class frdFragment extends Fragment {

    private List<Friend> friendList = new ArrayList<>();

    public frdFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.tab02, container, false);
        View view = inflater.inflate(R.layout.tab02, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.RecyclerView);
        initFrd();
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        friendAdapter adapter = new friendAdapter(friendList);
        recyclerView.setAdapter(adapter);
        return view;
    }

    private void initFrd() {
        for(int i = 0; i < 10; i++) {
            Friend frdGaga=new Friend(i, R.drawable.lady_gaga, "Lady Gaga");
            friendList.add(frdGaga);
            Friend frdAdam=new Friend(i, R.drawable.adam, "Adam Levin");
            friendList.add(frdAdam);
            Friend frdAdele=new Friend(i, R.drawable.adele, "Adele Adkins");
            friendList.add(frdAdele);
            Friend frdGreyson=new Friend(i, R.drawable.greyson, "Greyson Chance");
            friendList.add(frdGreyson);
            Friend frdRihanna=new Friend(i, R.drawable.myrihanna, "Rihanna Fenty");
            friendList.add(frdRihanna);
            Friend frdLana=new Friend(i, R.drawable.lana, "Lana Del Rey");
            friendList.add(frdLana);
            Friend frdKaty=new Friend(i, R.drawable.katy, "Lady Gaga");
            friendList.add(frdKaty);
            Friend frdLorde=new Friend(i, R.drawable.lorde, "Katy Perry");
            friendList.add(frdLorde);
            Friend frdBruno=new Friend(i, R.drawable.bruno_mars, "Bruno Mars");
            friendList.add(frdBruno);
            Friend frdTroye=new Friend(i, R.drawable.troye, "Troye Sivan");
            friendList.add(frdTroye);

        }
    }
}

比較關鍵的還有friendAdapter.java,在此就不一一展示了,感興趣的可以看我的對應源碼。

四、實現效果

在這裏插入圖片描述

本次項目對應源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章