第三方开源库之 BaseRecyclerViewAdapterHelper

BRVAH 是一个强大的 RecyclerAdapter 框架,它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。

添加依赖

  1. 在 build.gradle(Project:XXXX) 的 repositories 添加:
allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io"}
    }
}
  1. 在 build.gradle(Module:app) 的 dependencies 添加:
dependencies {
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
}

使用

  1. 效果图
    6.jpg

  2. HomeBean.java

/**
 * Created on 2019/11/28 9:30
 *
 * @author Gong Youqiang
 */
public class HomeBean {
    private String name;
    private int iconId;

    public HomeBean(String name, int iconId) {
        this.name = name;
        this.iconId = iconId;
    }

    public String getName() {
        return name;
    }

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

    public int getIconId() {
        return iconId;
    }

    public void setIconId(int iconId) {
        this.iconId = iconId;
    }
}

  1. HomeAdapter.java
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.epro.test.R;
import com.epro.test.bean.HomeBean;

import java.util.List;

/**
 * Created on 2019/11/28 9:31
 *
 * @author Gong Youqiang
 */
public class HomeAdapter extends BaseQuickAdapter<HomeBean, BaseViewHolder> {

    public HomeAdapter(int layoutResId, @Nullable List<HomeBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeBean item) {
        helper.setText(R.id.tv_name,item.getName());
        helper.setImageResource(R.id.iv_img,item.getIconId());

        CardView cardView = helper.getView(R.id.card_view);

        helper.addOnClickListener(R.id.card_view);
    }
}

  1. item_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_gravity="center"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="@color/white"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="android"
            android:textColor="@color/black"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>
  1. activity_layout.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">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
  1. top_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_home_bg" />
</LinearLayout>
  1. Activity
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.epro.test.R;
import com.epro.test.adapter.HomeAdapter;
import com.epro.test.bean.HomeBean;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LayoutActivity extends AppCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    HomeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        ButterKnife.bind(this);

        initAdapter();

    }

    private void initAdapter() {
        mAdapter = new HomeAdapter(R.layout.item_home,getHomeItem());
        mAdapter.openLoadAnimation();
        View top = getLayoutInflater().inflate(R.layout.top_view, (ViewGroup) mRecyclerView.getParent(), false);
        mAdapter.addHeaderView(top);

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(LayoutActivity.this,"click"+position,Toast.LENGTH_SHORT).show();
            }
        });

        mRecyclerView.setLayoutManager(new GridLayoutManager(LayoutActivity.this,3));
        mRecyclerView.setAdapter(mAdapter);
    }

    private List<HomeBean> getHomeItem() {
        return Arrays.asList(
                new HomeBean("android",R.mipmap.ic_launcher),
                new HomeBean("ios",R.mipmap.ic_launcher),
                new HomeBean("haha",R.mipmap.ic_launcher),
                new HomeBean("andnnroid",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher)
        );
    }

}

发布了225 篇原创文章 · 获赞 64 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/duoduo_11011/article/details/103417073