Android开发----RecyclerView的使用(创建网格布局)

  1. 引入RecyclerView
    在当前模块的build.gradle中引入RecyclerView的包,路径如下:app/build.gradle,
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
	//版本号尽量与已有的appcompat包相同
    implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'

  1. 在xml定义RecyclerView
    在你需要加入的RecyclerView的布局文件中添加RecyclerView,我的例子代码如下:
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <!--推荐歌单-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="@dimen/marginSize"
                android:layout_marginRight="@dimen/marginSize"
                android:layout_marginLeft="@dimen/marginSize"
                android:layout_marginBottom="@dimen/marginSize">
                
                <View
                    android:layout_width="4dp"
                    android:layout_height="22dp"
                    android:background="@color/mainColor"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/recommend"
                    android:textColor="@color/recommendColor"
                    android:textSize="@dimen/textSize"
                    android:textStyle="bold"
                    android:layout_marginLeft="@dimen/recommendMarginSize" />

            </LinearLayout>


            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_grid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>
        </LinearLayout>
    </ScrollView>
  1. 在Activity中声明RecyclerView对象
    为了对我们的RecyclerView进行操作,我们需要在该布局文件对应的xml中创建RecyclerView对象,并通过Activity的findViewById方法来找到对应的RecyclerView
private RecyclerView mRvGrid;
mRvGrid = this.findViewById(R.id.rv_grid);
//由于在此我要设置的是网格的RecyclerView,所以还需为RecyclerView设置布局
mRvGrid.setLayoutManager(new GridLayoutManager(this,3));
  1. 创建Adapter
    (1)继承自RecyclerView.Adapter
    (2)自定义ViewHolder继承自RecyclerView.ViewHolder
    (3)创建item的布局文件
    (4)实现RecyclerView.Adapter的接口方法
package com.musicplaer.eminemmusic.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.musicplaer.eminemmusic.R;

public class MusicGridAdapter extends RecyclerView.Adapter<MusicGridAdapter.ViewHolder> {
    private Context context;

    public MusicGridAdapter(Context context){
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //LayoutInflater.from(context).inflate()将Layout映射为View: 即设置每一个item的布局
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_grid_music,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        //6在此表示有6个item,仅为演示
        return 6;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

每一个接item的布局文件可以参考如下

<?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"
    android:orientation="vertical"
    android:background="@android:color/white">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <!--底层是专辑图片-->
        <com.musicplaer.eminemmusic.views.WEqualsHImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/album1"/>

        <!--上层有播放量显示-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right|center_vertical"
            android:layout_margin="@dimen/albumMarginSize">

            <ImageView
                android:layout_width="@dimen/playNumImgSize"
                android:layout_height="@dimen/playNumImgSize"
                android:src="@mipmap/play_num"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10.00万"
                android:textColor="@android:color/white"
                android:textSize="@dimen/textSize"
                android:textStyle="bold"
                android:layout_marginLeft="@dimen/albumMarginSize"/>

        </LinearLayout>

    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="歌单名称"
        android:textColor="@color/recommendColor"
        android:textStyle="bold"
        android:textSize="@dimen/textSize"
        android:padding="@dimen/albumMarginSize"/>

</LinearLayout>
  1. 为RecyclerView设置Adapter
    setAdapter(SelfDefAdapter);
    在原先创建RecyclerView对象的Activity中创建自定义的Adapter,代码示例如下
private MusicGridAdapter musicGridAdapter;
mRvGrid.setAdapter(musicGridAdapter);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值