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,在此就不一一展示了,感兴趣的可以看我的对应源码。

四、实现效果

在这里插入图片描述

本次项目对应源码

发布了2 篇原创文章 · 获赞 0 · 访问量 96

猜你喜欢

转载自blog.csdn.net/Louloo/article/details/105129061