├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── example
│ │ └── administrator
│ │ └── recyclerviewtest
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hz
│ │ │ └── android
│ │ │ └── easyadapter
│ │ │ └── demo
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── item_show_text.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── arrays.xml
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── administrator
│ └── recyclerviewtest
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── bintrayUpload.gradle
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── hz
│ │ └── android
│ │ └── easyadapter
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hz
│ │ │ └── android
│ │ │ └── easyadapter
│ │ │ └── EasyAdapter.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── hz
│ └── android
│ └── easyadapter
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 | # Gradle
11 | .gradle
12 | build
13 | local.properties
14 | bintray.properties
15 | reports
16 |
17 | # Maven
18 | target
19 | pom.xml.*
20 | release.properties
21 | gen-external-apklibs
22 |
23 | # Eclipse
24 | .classpath
25 | .project
26 | .settings
27 | eclipsebin
28 |
29 | # IntelliJ IDEA
30 | .idea
31 | *.iml
32 | *.ipl
33 | *.iws
34 | classes/
35 | idea-classes/
36 | coverage-error.log
37 |
38 | # Android
39 | gen
40 | bin
41 | project.properties
42 | out
43 |
44 | # Finder
45 | .DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # EasyAdapterForRecyclerView
2 |
3 | ##### 介绍
4 | EasyAdapter是用于RecyclerView的适配器,在原有的适配器基础上可支持监听相应的事件并设置点击模式、单选和多选模式。在多选模式下,可设置最大可选数量,以及提供了全选、反选等接口。
5 | ##### 使用
6 | Gradle
7 | ```
8 | compile 'com.hz.androids.easyadapter:library:1.1'
9 | ```
10 | or Maven
11 | ```
12 |
13 | com.hz.androids.easyadapter
14 | library
15 | 1.1
16 | pom
17 |
18 | ```
19 | 1.自定义Adapter继承EasyAdapter
20 |
21 | ```java
22 | private class MyAdapter extends EasyAdapter {
23 | @Override
24 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
25 | ...
26 | }
27 |
28 | /*
29 | whenBindViewHolder方法:相当于原生Adapter.onBindViewHolder
30 | */
31 | @Override
32 | public void whenBindViewHolder(MyViewHolder holder, int position) {
33 | ...
34 | }
35 |
36 | @Override
37 | public int getItemCount() {
38 | return list.size();
39 | }
40 | }
41 | ```
42 | 2.RecycleView设置自定义的适配器
43 |
44 | ```java
45 | MyAdapter myAdapter = new MyAdapter();
46 | recyclerView.setAdapter(myAdapter);
47 | ```
48 | 3.可切换点击、单选、多选模式
49 |
50 | ```java
51 | //点击模式
52 | myAdapter.setSelectMode(EasyAdapter.SelectMode.CLICK);
53 | //单选模式
54 | myAdapter.setSelectMode(EasyAdapter.SelectMode.SINGLE_SELECT);
55 | //多选模式
56 | myAdapter.setSelectMode(EasyAdapter.SelectMode.MULTI_SELECT);
57 | ```
58 |
59 | 4.在自定义适配器中设置相应模式的监听器
60 |
61 | ```java
62 | // 监听点击事件
63 | myAdapter.setOnItemClickListener(new EasyAdapter.OnItemClickListener() {
64 | @Override
65 | public void onSelected(int clickPosition) {
66 | ...
67 | }
68 | });
69 | //监听单选事件
70 | myAdapter.setOnItemSelectListener(new EasyAdapter.OnItemSelectListener() {
71 | @Override
72 | public void onSelected(int selectedPosition) {
73 | ...
74 | }
75 | });
76 | //监听多选事件
77 | myAdapter.setOnItemMultiSelectListener(new EasyAdapter.OnItemMultiSelectListener() {
78 | @Override
79 | public void onMultiSelected(int multiSelectedPosition) {
80 | ...
81 | });
82 | ```
83 |
84 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "26.0.1"
6 | defaultConfig {
7 | applicationId "com.example.administrator.recyclerviewtest"
8 | minSdkVersion 15
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | lintOptions {
21 | abortOnError false
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(include: ['*.jar'], dir: 'libs')
27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 | compile 'com.android.support:appcompat-v7:25.3.1'
31 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
32 | testCompile 'junit:junit:4.12'
33 | compile 'com.android.support:recyclerview-v7:21.0.+'
34 | compile 'com.forward.androids:androids:1.1.7.1'
35 | compile project(':library')
36 | }
37 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/administrator/recyclerviewtest/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.recyclerviewtest;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.example.administrator.recyclerviewtest", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hz/android/easyadapter/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.hz.android.easyadapter.demo;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.support.v7.widget.LinearLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.support.v7.widget.RecyclerView.ViewHolder;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.AdapterView;
11 | import android.widget.Spinner;
12 | import android.widget.TextView;
13 | import android.widget.Toast;
14 |
15 | import com.hz.android.easyadapter.EasyAdapter;
16 |
17 | import java.util.ArrayList;
18 | import java.util.Arrays;
19 | import java.util.List;
20 |
21 | import cn.forward.androids.views.ScrollPickerView;
22 | import cn.forward.androids.views.StringScrollPicker;
23 |
24 | public class MainActivity extends AppCompatActivity {
25 | private RecyclerView recyclerView;
26 | private List list = new ArrayList<>();
27 | private MyAdapter myAdapter;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_main);
33 | recyclerView = (RecyclerView) findViewById(R.id.test_recyclerView);
34 | final View container_multiOptions = findViewById(R.id.container_multSelectMode_options);
35 | final View container_singleOption = findViewById(R.id.container_SingleSelectMode_option);
36 | StringScrollPicker stringScrollPicker = (StringScrollPicker) findViewById(R.id.max_select_count_pv);
37 |
38 | stringScrollPicker.setData(new ArrayList(Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")));
39 | stringScrollPicker.setOnSelectedListener(new ScrollPickerView.OnSelectedListener() {
40 | @Override
41 | public void onSelected(ScrollPickerView scrollPickerView, int position) {
42 | Toast.makeText(MainActivity.this, "stringScrollPicker:" + position, Toast.LENGTH_SHORT).show();
43 | myAdapter.setMaxSelectedCount(position);
44 |
45 | }
46 | });
47 | stringScrollPicker.setSelectedPosition(0);
48 | myAdapter = new MyAdapter();
49 | list.addAll(Arrays.asList("李子", "樱桃", "葡萄", "菠萝", "椰子", "草莓", "苹果", "西瓜", "香蕉", "柚子", "柠檬", "桔子", "芒果", "枣子", "猕猴桃", "水蜜桃", "荔枝", "杨梅"));
50 |
51 | Spinner select_mode_spinner = (Spinner) findViewById(R.id.spinner_mode_selected);
52 | select_mode_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
53 | @Override
54 | public void onItemSelected(AdapterView> parent, View view, int position, long id) {
55 | Toast.makeText(MainActivity.this, "mode position: " + position, Toast.LENGTH_SHORT).show();
56 | if (position == 0) {
57 | myAdapter.setSelectMode(EasyAdapter.SelectMode.CLICK);
58 | container_singleOption.setVisibility(View.GONE);
59 | container_multiOptions.setVisibility(View.GONE);
60 | } else if (position == 1) {
61 | myAdapter.setSelectMode(EasyAdapter.SelectMode.SINGLE_SELECT);
62 | container_singleOption.setVisibility(View.VISIBLE);
63 | container_multiOptions.setVisibility(View.GONE);
64 | } else if (position == 2) {
65 | myAdapter.setSelectMode(EasyAdapter.SelectMode.MULTI_SELECT);
66 | container_singleOption.setVisibility(View.GONE);
67 | container_multiOptions.setVisibility(View.VISIBLE);
68 | }
69 | }
70 |
71 | @Override
72 | public void onNothingSelected(AdapterView> parent) {
73 |
74 | }
75 | });
76 |
77 | //监听单选
78 | myAdapter.setOnItemSingleSelectListener(new EasyAdapter.OnItemSingleSelectListener() {
79 |
80 | @Override
81 | public void onSelected(int itemPosition, boolean isSelected) {
82 | Toast.makeText(MainActivity.this, "selectedPosition:" + itemPosition +" == "+ myAdapter.getSingleSelectedPosition(), Toast.LENGTH_SHORT).show();
83 |
84 | }
85 | });
86 |
87 | /*// 监听点击事件
88 | myAdapter.setOnItemClickListener(new EasyAdapter.OnItemClickListener() {
89 | @Override
90 | public void onSelected(int clickPosition) {
91 | Toast.makeText(MainActivity.this, "clickPosition:" + clickPosition, Toast.LENGTH_SHORT).show();
92 | }
93 | });
94 | //监听单选
95 | myAdapter.setOnItemSingleSelectListener(new EasyAdapter.OnItemSingleSelectListener() {
96 | @Override
97 | public void onSelected(int selectedPosition) {
98 | Toast.makeText(MainActivity.this, "selectedPosition:" + selectedPosition, Toast.LENGTH_SHORT).show();
99 |
100 | }
101 | });
102 | //监听多选
103 | myAdapter.setOnItemMultiSelectListener(new EasyAdapter.OnItemMultiSelectListener() {
104 | @Override
105 | public void onSelected(int multiSelectedPosition) {
106 | Toast.makeText(MainActivity.this, "multiSelectedPosition:" + multiSelectedPosition, Toast.LENGTH_SHORT).show();
107 | }
108 | });
109 | */
110 | //select_mode_spinner.setSelection(0); //点击
111 | //select_mode_spinner.setSelection(1); //单选 此时RecyclerView+Adapter就是一个单选框
112 | select_mode_spinner.setSelection(2); //多选
113 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
114 | linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); //把列表设置成水平滚动
115 | recyclerView.setLayoutManager(linearLayoutManager);
116 |
117 | recyclerView.setAdapter(myAdapter);
118 |
119 | // myAdapter.setSelected(1, 3, 5); // 多选模式下 和单选模式 都一样可以调用
120 |
121 | }
122 |
123 | //监听按钮
124 | public void btnShowSingleSelected(View view) {
125 | int singleSelectedPosition = myAdapter.getSingleSelectedPosition();
126 | Toast.makeText(MainActivity.this, "btnShowSingleSelected:" + singleSelectedPosition, Toast.LENGTH_SHORT).show();
127 |
128 | }
129 |
130 | public void btnSelectedAll(View view) {
131 | myAdapter.selectAll();
132 | }
133 |
134 | public void btnReverseSelected(View view) {
135 | myAdapter.reverseSelected();
136 | }
137 |
138 | public void btnShowMultiSelected(View view) {
139 | List selectedList = myAdapter.getMultiSelectedPosition();
140 | Toast.makeText(MainActivity.this, "btnShowMultiSelected:" + selectedList.toString(), Toast.LENGTH_SHORT).show();
141 | }
142 |
143 | public void btnClearSelected(View view) {
144 | myAdapter.clearSelected();
145 | }
146 |
147 | // 继承EasyAdapter
148 | private class MyAdapter extends EasyAdapter {
149 | @Override
150 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
151 | View view = View.inflate(getApplicationContext(), R.layout.item_show_text, null);
152 | return new MyViewHolder(view);
153 | }
154 |
155 | @Override
156 | public void whenBindViewHolder(MyViewHolder holder, int position) {
157 | holder.textView.setTag(position);//绑定
158 | holder.textView.setText(list.get(position));
159 | }
160 |
161 | @Override
162 | public int getItemCount() {
163 | return list.size();
164 | }
165 | }
166 |
167 | private static class MyViewHolder extends ViewHolder {
168 | private TextView textView;
169 |
170 | public MyViewHolder(View itemView) {
171 | super(itemView);
172 | textView = (TextView) itemView.findViewById(R.id.txt_item);
173 | }
174 | }
175 | }
176 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
16 |
17 |
22 |
23 |
24 |
25 |
31 |
32 |
42 |
43 |
44 |
50 |
51 |
55 |
56 |
63 |
64 |
71 |
72 |
73 |
74 |
75 |
79 |
80 |
89 |
90 |
100 |
101 |
111 |
112 |
113 |
114 |
123 |
124 |
125 |
126 |
127 |
130 |
131 |
132 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_show_text.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/arrays.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | - 点击模式
5 | - 单选模式
6 | - 多选模式
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RecyclerViewTest
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/administrator/recyclerviewtest/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.recyclerviewtest;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | def getLocalProperty(String key) {
2 | if (!localPropertiesFile().exists()) {
3 | return null
4 | }
5 |
6 | Properties properties = new Properties()
7 | properties.load(localPropertiesFile().newDataInputStream())
8 | return properties.getProperty(key)
9 | }
10 |
11 | def localPropertiesFile() {
12 | return project.rootProject.file('local.properties');
13 | }
14 |
15 | // 定义 判断是否上传的方能股份
16 | def isForUpload2Maven() {
17 | // 从本地配置文件(local.properties)获取是否上传的值
18 | return getLocalProperty("for.upload") == "true"
19 | }
20 |
21 | buildscript {
22 |
23 | repositories {
24 | jcenter()
25 | }
26 | dependencies {
27 | classpath 'com.android.tools.build:gradle:2.3.3'
28 |
29 | File file = project.rootProject.file('local.properties')
30 | if (file.exists()) {
31 | Properties properties = new Properties()
32 | properties.load(file.newDataInputStream())
33 | boolean upload2maven = properties.getProperty("for.upload") == "true"
34 | if (upload2maven) {
35 | println 'upload2maven:true'
36 | //用于上传Maven生成的文件到Bintray
37 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
38 | //用于打包Maven所需文件
39 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
40 | } else {
41 | println 'upload2maven:false'
42 | }
43 | }
44 |
45 | // NOTE: Do not place your application dependencies here; they belong
46 | // in the individual module build.gradle files
47 | }
48 | }
49 |
50 | allprojects {
51 | repositories {
52 | jcenter()
53 | }
54 | }
55 |
56 | task clean(type: Delete) {
57 | delete rootProject.buildDir
58 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhouHuang23/EasyAdapter/64b5d1480fe8b23a63c33746b4efa36833af310e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jan 03 21:55:32 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/library/bintrayUpload.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 | apply plugin: 'com.jfrog.bintray'
3 |
4 | //加载属性文件
5 | Properties properties = new Properties()
6 | File localPropertiesFile = project.file("bintray.properties");
7 | if (localPropertiesFile.exists()) {
8 | properties.load(localPropertiesFile.newDataInputStream())
9 | }
10 | File projectPropertiesFile = project.file("bintray.properties");
11 | if (projectPropertiesFile.exists()) {
12 | properties.load(projectPropertiesFile.newDataInputStream())
13 | }
14 |
15 | println android.getBootClasspath()
16 |
17 | //读取属性
18 | def projectRepositoryName = properties.getProperty("project.repositoryName")
19 | def projectName = properties.getProperty("project.name")
20 | def projectGroupId = properties.getProperty("project.groupId")
21 | def projectArtifactId = properties.getProperty("project.artifactId")
22 | def projectVersionName = android.defaultConfig.versionName
23 | def projectPackaging = properties.getProperty("project.packaging")
24 | def projectSiteUrl = properties.getProperty("project.siteUrl")
25 | def projectGitUrl = properties.getProperty("project.gitUrl")
26 |
27 | def developerId = properties.getProperty("developer.id")
28 | def developerName = properties.getProperty("developer.name")
29 | def developerEmail = properties.getProperty("developer.email")
30 |
31 | def bintrayUser = properties.getProperty("bintray.user")
32 | def bintrayApikey = properties.getProperty("bintray.apiKey")
33 | def bintrayOrganizationId = properties.getProperty("bintray.organizationId");
34 |
35 | def javadocName = properties.getProperty("javadoc.name")
36 |
37 | /*
38 | *这句代码一定要加否则会出现如下错误
39 | * Could not upload to 'https://api.bintray.com/content/coolcode/maven/LibUiBase/1.0.0/CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.pom': HTTP/1.1 400 Bad Request [
40 | message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'CommonLibrary/LibUiBase/1.0.0/LibUiBase-1.0.0.p
41 | om']
42 | * */
43 | group = projectGroupId
44 |
45 | // 配置生成POM.xml文件的参数
46 | install {
47 | repositories.mavenInstaller {
48 | pom {
49 | project {
50 | name projectName
51 | groupId projectGroupId
52 | artifactId projectArtifactId
53 | version projectVersionName
54 | packaging projectPackaging
55 | url projectSiteUrl
56 | licenses {
57 | license {
58 | name 'The Apache Software License, Version 2.0'
59 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
60 | }
61 | }
62 | developers {
63 | developer {
64 | id developerId
65 | name developerName
66 | email developerEmail
67 | }
68 | }
69 | scm {
70 | connection projectGitUrl
71 | developerConnection projectGitUrl
72 | url projectSiteUrl
73 | }
74 | }
75 | }
76 | }
77 | }
78 |
79 | //生成sources.jar
80 | task sourcesJar(type: Jar) {
81 | from android.sourceSets.main.java.srcDirs
82 | classifier = 'sources'
83 | }
84 |
85 | task javadoc(type: Javadoc) {
86 | source = android.sourceSets.main.java.srcDirs
87 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
88 | }
89 |
90 | // 在配置结束时调用
91 | afterEvaluate {
92 | // 加入libs中引用的库
93 | javadoc.classpath += files(android.libraryVariants.collect { variant ->
94 | variant.javaCompile.classpath.files
95 | })
96 | }
97 |
98 | // 禁止doclint
99 | if (JavaVersion.current().isJava8Compatible()) {
100 | allprojects {
101 | tasks.withType(Javadoc) {
102 | options.addStringOption('Xdoclint:none', '-quiet')
103 | }
104 | }
105 | }
106 |
107 | //生成javadoc.jar
108 | task javadocJar(type: Jar, dependsOn: javadoc) {
109 | classifier = 'javadoc'
110 | from javadoc.destinationDir
111 | }
112 |
113 | artifacts {
114 | archives javadocJar
115 | archives sourcesJar
116 | }
117 |
118 | //javadoc的配置
119 | javadoc {
120 | options {
121 | encoding "UTF-8"
122 | charSet 'UTF-8'
123 | author true
124 | version projectVersionName
125 | links "http://docs.oracle.com/javase/7/docs/api"
126 | title javadocName
127 | }
128 | }
129 |
130 | /*
131 | *userOrg为bintray账号信息里面的Organization Id
132 | *repo为你创建的仓库名称
133 | * 如果上述两个字段写错了,则会出现下面类似的错误
134 | *Could not create package 'huangxuanheng/maven/fragmentstack': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
135 | *
136 | *
137 | * */
138 | bintray {
139 | user = bintrayUser
140 | key = bintrayApikey
141 | configurations = ['archives']
142 | pkg {
143 | userOrg = bintrayOrganizationId
144 | repo = projectRepositoryName
145 | name = projectName
146 | websiteUrl = projectSiteUrl
147 | vcsUrl = projectGitUrl
148 | licenses = ["Apache-2.0"]
149 | publish = true
150 | }
151 | }
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "26.0.1"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 25
10 | versionCode 4
11 | versionName "1.0.3"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 |
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | lintOptions {
22 | abortOnError false
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | compile fileTree(dir: 'libs', include: ['*.jar'])
29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
30 | exclude group: 'com.android.support', module: 'support-annotations'
31 | })
32 | compile 'com.android.support:appcompat-v7:25.3.1'
33 | testCompile 'junit:junit:4.12'
34 | compile 'com.android.support:recyclerview-v7:21.0.+'
35 | }
36 | if (isForUpload2Maven()) { // 判断是否需要上传到远程仓库,不需要上传就不会执行下面的脚本,加快编译速度
37 | //apply from: "https://raw.githubusercontent.com/1993hzw/common/master/bintrayUpload.gradle"
38 | apply from: "bintrayUpload.gradle"
39 | }
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/hz/android/easyadapter/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hz.android.easyadapter;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.hz.android.easyadapter.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/library/src/main/java/com/hz/android/easyadapter/EasyAdapter.java:
--------------------------------------------------------------------------------
1 | package com.hz.android.easyadapter;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * 仿照原生RecyclerView.Adapter的实现,在原生适配器的基础上 支持监听item单击事件以及支持单选模式、多选模式
11 | * Created by Administrator on 2018/1/4.
12 | */
13 |
14 | public abstract class EasyAdapter extends RecyclerView.Adapter implements View.OnClickListener {
15 | private OnItemClickListener onItemClickListener;
16 | private OnItemSingleSelectListener onItemSingleSelectListener;
17 | private OnItemMultiSelectListener onItemMultiSelectListener;
18 | private SelectMode selectMode;
19 |
20 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
21 | this.onItemClickListener = onItemClickListener;
22 | }
23 |
24 | public void setOnItemSingleSelectListener(OnItemSingleSelectListener onItemSingleSelectListener) {
25 | this.onItemSingleSelectListener = onItemSingleSelectListener;
26 | }
27 |
28 | public void setOnItemMultiSelectListener(OnItemMultiSelectListener onItemMultiSelectListener) {
29 | this.onItemMultiSelectListener = onItemMultiSelectListener;
30 | }
31 |
32 | public abstract void whenBindViewHolder(VH holder, int position);
33 |
34 | private int singleSelected = 0; // 默认为第一个被选中
35 | private List multiSelected = new ArrayList<>();
36 | private int maxSelectedCount = -1;
37 |
38 | @Override
39 | public void onBindViewHolder(VH holder, int position) {
40 | whenBindViewHolder(holder, position);
41 |
42 | holder.itemView.setTag(position);
43 | holder.itemView.setOnClickListener(this);
44 |
45 | if (selectMode == SelectMode.CLICK) { //点击
46 | holder.itemView.setSelected(false);
47 | } else if (selectMode == SelectMode.SINGLE_SELECT) { //单选
48 | if (singleSelected == position) {
49 | holder.itemView.setSelected(true);
50 | } else {
51 | holder.itemView.setSelected(false);
52 | }
53 | } else if (selectMode == SelectMode.MULTI_SELECT) {//多选
54 | if (multiSelected.contains(position)) {
55 | holder.itemView.setSelected(true);
56 | } else {
57 | holder.itemView.setSelected(false);
58 | }
59 | }
60 | }
61 |
62 | @Override
63 | public void onClick(View v) {
64 | int itemPosition = (int) v.getTag();
65 | if (selectMode == SelectMode.CLICK) {//点击模式
66 | if (onItemClickListener != null) {
67 | onItemClickListener.onClicked(itemPosition);
68 | }
69 | } else if (selectMode == SelectMode.SINGLE_SELECT) { //单选模式
70 | if (onItemSingleSelectListener != null) {
71 | if (singleSelected == itemPosition) {
72 | onItemSingleSelectListener.onSelected(itemPosition, false);
73 | } else {
74 | singleSelected = itemPosition;
75 | onItemSingleSelectListener.onSelected(itemPosition, true);
76 | }
77 | }
78 | notifyDataSetChanged();//通知刷新
79 | } else if (selectMode == SelectMode.MULTI_SELECT) {//多选模式
80 | if (maxSelectedCount <= 0 //选择不受限制
81 | || multiSelected.size() < maxSelectedCount) { // 选择个数需要小于最大可选数
82 | if (multiSelected.contains(itemPosition)) {
83 | multiSelected.remove((Integer) itemPosition);
84 | if (onItemMultiSelectListener != null) {
85 | onItemMultiSelectListener.onSelected(Operation.ORDINARY, itemPosition, false);
86 | }
87 | } else {
88 | multiSelected.add(itemPosition);
89 | if (onItemMultiSelectListener != null) {
90 | onItemMultiSelectListener.onSelected(Operation.ORDINARY, itemPosition, true);
91 | }
92 | }
93 |
94 | } else if (multiSelected.size() == maxSelectedCount && multiSelected.contains(itemPosition)) { //当等于最大数量并且点击的item包含在已选中 可清除
95 | multiSelected.remove((Integer) itemPosition);
96 | if (onItemMultiSelectListener != null) {
97 | onItemMultiSelectListener.onSelected(Operation.ORDINARY, itemPosition, false);
98 | }
99 | }
100 | notifyDataSetChanged();
101 | }
102 | }
103 |
104 | //=========API=========
105 |
106 | /**
107 | * 设置选择模式
108 | *
109 | * @param selectMode
110 | */
111 |
112 | public void setSelectMode(SelectMode selectMode) {
113 | this.selectMode = selectMode;
114 | notifyDataSetChanged();
115 | }
116 |
117 | /**
118 | * 获取选择模式
119 | *
120 | * @return
121 | */
122 | public SelectMode getSelectMode() {
123 | return selectMode;
124 | }
125 |
126 | /**
127 | * 设置默认选中项,一个或多个
128 | *
129 | * @param itemPositions
130 | */
131 |
132 | public void setSelected(int... itemPositions) {
133 | multiSelected.clear();
134 | if (selectMode == SelectMode.SINGLE_SELECT) {
135 | singleSelected = itemPositions[0];
136 | if (onItemSingleSelectListener != null) {
137 | onItemSingleSelectListener.onSelected(singleSelected, true);
138 | }
139 | } else {
140 | for (int itemPosition : itemPositions) {
141 | multiSelected.add(itemPosition);
142 | if (onItemMultiSelectListener != null) {
143 | onItemMultiSelectListener.onSelected(Operation.ORDINARY, itemPosition, true);
144 | }
145 | }
146 | }
147 | notifyDataSetChanged();
148 | }
149 |
150 | /**
151 | * 获取单选模式选中Item位置
152 | *
153 | * @return
154 | */
155 | public int getSingleSelected() {
156 | return singleSelected;
157 | }
158 |
159 | /**
160 | * 清除选择项,只有在MULT_SELECT模式下有效
161 | */
162 | public void clearSelected() {
163 | if (selectMode == SelectMode.MULTI_SELECT) {
164 | multiSelected.clear();
165 | if (onItemMultiSelectListener != null) {
166 | onItemMultiSelectListener.onSelected(Operation.ALL_CANCEL, -1, false);
167 | }
168 | notifyDataSetChanged();
169 | }
170 | }
171 |
172 | /**
173 | * 获取单选项位置
174 | */
175 | public int getSingleSelectedPosition() {
176 | return singleSelected;
177 | }
178 |
179 | /**
180 | * 获取多选项位置,元素顺序按照选择顺序排列
181 | */
182 | public List getMultiSelectedPosition() {
183 | return multiSelected;
184 | }
185 |
186 | /**
187 | * 设置最大可选数量
188 | *
189 | * @param maxSelectedCount maxSelectedCount <= 0 表示不限制选择数
190 | */
191 | public void setMaxSelectedCount(int maxSelectedCount) {
192 | if (maxSelectedCount < multiSelected.size()) {
193 | multiSelected.clear();
194 | }
195 | this.maxSelectedCount = maxSelectedCount;
196 | if (onItemMultiSelectListener != null) {
197 | onItemMultiSelectListener.onSelected(Operation.SET_MAX_COUNT, -1, false);
198 | }
199 | notifyDataSetChanged();
200 | }
201 |
202 | /**
203 | * 获取最大可选数目
204 | *
205 | * @return
206 | */
207 | public int getMaxSelectedCount() {
208 | return maxSelectedCount;
209 | }
210 |
211 | /**
212 | * 选择全部,仅在maxSelectedCount <= 0 不限制选择数时有效
213 | */
214 | public void selectAll() {
215 | if (maxSelectedCount <= 0) {
216 | multiSelected.clear();
217 | for (int i = 0; i < getItemCount(); i++) {
218 | multiSelected.add(i);
219 | }
220 | if (onItemMultiSelectListener != null) {
221 | onItemMultiSelectListener.onSelected(Operation.ALL_SELECTED, -1, false);
222 | }
223 | notifyDataSetChanged();
224 | }
225 | }
226 |
227 | /**
228 | * 反选全部,仅在maxSelectedCount <= 0 不限制选择数时有效
229 | */
230 |
231 | public void reverseSelected() {
232 | if (maxSelectedCount <= 0) {
233 | if (onItemMultiSelectListener != null) {
234 | onItemMultiSelectListener.onSelected(Operation.REVERSE_SELECTED, -1, false);
235 | }
236 | for (int i = 0; i < getItemCount(); i++) {
237 | if (multiSelected.contains(i)) {
238 | multiSelected.remove((Integer) i);
239 | } else {
240 | multiSelected.add(i);
241 | }
242 | }
243 | notifyDataSetChanged();
244 | }
245 | }
246 |
247 | /**
248 | * 判断某个item位置是否被选中
249 | *
250 | * @param position
251 | * @return
252 | */
253 | public boolean isSelected(int position) {
254 | if (selectMode == SelectMode.SINGLE_SELECT) {
255 | return position == singleSelected;
256 | } else if (selectMode == SelectMode.MULTI_SELECT) {
257 | return multiSelected.contains(position);
258 | }
259 | return false;
260 | }
261 |
262 | /**
263 | * 点选模式监听接口
264 | */
265 | public interface OnItemClickListener {
266 | /**
267 | * 点选模式下,点击item时回调
268 | *
269 | * @param itemPosition 点击的item位置
270 | */
271 | void onClicked(int itemPosition);
272 | }
273 |
274 | /**
275 | * 单选模式监听接口
276 | */
277 | public interface OnItemSingleSelectListener {
278 | /**
279 | * 单选模式下,点击Item选中时回调
280 | *
281 | * @param itemPosition 点击的item位置
282 | * @param isSelected 是否选中
283 | */
284 | void onSelected(int itemPosition, boolean isSelected);
285 |
286 | }
287 |
288 | /**
289 | * 多选模式监听接口
290 | */
291 | public interface OnItemMultiSelectListener {
292 | /**
293 | * 多选模式下,点击Item选中时回调
294 | *
295 | * @param operation 操作类型,分为普通,全选 反选 取消全部等。
296 | * @param itemPosition 点击的item位置 仅在操作类型为普通时生效
297 | * @param isSelected 是否选中 仅在操作类型为普通时生效
298 | */
299 | void onSelected(Operation operation, int itemPosition, boolean isSelected);
300 |
301 | }
302 |
303 | /**
304 | * 选择模式,分为点击,单选,多选。
305 | */
306 | public enum SelectMode {
307 | CLICK, SINGLE_SELECT, MULTI_SELECT
308 | }
309 |
310 | /**
311 | * 操作类型,分为普通,全选 反选 取消全部等。
312 | */
313 | public enum Operation {
314 | ORDINARY, ALL_SELECTED, REVERSE_SELECTED, ALL_CANCEL, SET_MAX_COUNT
315 | }
316 | }
317 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/com/hz/android/easyadapter/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hz.android.easyadapter;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------