版权声明:本文为HaiyuKing原创文章,转载请注明出处!

前言

记录封装单选、多选、切换选中状态的BaseSelectableAdapter基类,配合Recyclerview使用。

注意:此Demo只是一个简单的使用,那么实际项目中需要灵活处理!

效果图

 RecyclerViewSelectableAdapterDemo【封装BaseSelectableAdapter用于多选、单选,以及切换选中状态等功能】    RecyclerViewSelectableAdapterDemo【封装BaseSelectableAdapter用于多选、单选,以及切换选中状态等功能】

代码分析

BaseSelectableAdapter:基类;

BaseSelectable:接口;

OnItemCheckListener:单选图片的监听事件

使用步骤

一、项目组织结构图

RecyclerViewSelectableAdapterDemo【封装BaseSelectableAdapter用于多选、单选,以及切换选中状态等功能】

RecyclerViewSelectableAdapterDemo【封装BaseSelectableAdapter用于多选、单选,以及切换选中状态等功能】

注意事项:

1、  导入类文件后需要change包名以及重新import R文件路径

2、  Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖

二、导入步骤

(1)在build.gradle中引用recyclerview【版本号和appcompat保持一致】

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.why.project.recyclerviewselectableadapterdemo"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //RecyclerView
    compile "com.android.support:recyclerview-v7:27.1.1"
}

(2)在项目中实现Recyclerview基本数据展现

1、创建Bean类

package com.why.project.recyclerviewselectableadapterdemo.bean;

/**
 * Created by HaiyuKing
 * Used 列表项的bean类
 */

public class Photo {
    private String id;
    private String path;

    public Photo(String id, String path) {
        this.id = id;
        this.path = path;
    }

    public Photo() {
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
Photo.java

相关文章: