Skip to content

Commit c144a36

Browse files
committed
增加ListView GridView 适配器
1 parent a504124 commit c144a36

File tree

11 files changed

+397
-90
lines changed

11 files changed

+397
-90
lines changed

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,76 @@
11
# FastAdapter
2-
一个快速进行适配器编写的库,适用于RecyclerView,ListView,GridView,ViewPager等
2+
一个快速进行适配器编写的库,适用于RecyclerView,ListView,GridView,ViewPager等,
3+
可大大减少样板代码,简化Adapter的编写。
34

4-
引入依赖
5+
### 引入依赖
56

6-
```java
7-
implementation 'com.yhw.library:fastadapter:0.0.2'
7+
```kotlin
8+
implementation 'com.yhw.library:fastadapter:1.0.0'
89
```
910

10-
使用方法:
11-
请查看Demo中的代码示例
11+
### 使用方法
12+
13+
#### RecyclerView Adapter用法
14+
```kotlin
15+
class MyRecyclerAdapter(dataList: MutableList<String>) : BaseRecyclerAdapter<String>(dataList) {
16+
17+
override fun getItemLayoutId(viewType: Int): Int {
18+
return R.layout.sample_item_layout
19+
}
20+
21+
override fun onBindViewItem(holder: RecyclerViewHolder, position: Int, data: String) {
22+
// 使用
23+
// val textView = holder.getView<TextView>(R.id.tv_text)
24+
// textView.text = data
25+
//
26+
holder.setText(R.id.tv_text, "$data $position") //推荐写法
27+
28+
holder.setImageResource(R.id.iv_image, R.mipmap.ic_launcher)
29+
}
30+
}
31+
```
32+
#### 首部插入一条数据
33+
```kotlin
34+
adapter.insertItemToFirst("新插入的首部数据")
35+
```
36+
#### 首部插入多条数据
37+
```kotlin
38+
adapter.insertItemToFirst(mutableListOf("批量插入的首部数据A","批量插入的首部数据B",
39+
"批量插入的首部数据C", "批量插入的首部数据D" ))
40+
```
41+
#### 任意位置插入数据
42+
```kotlin
43+
adapter.insertItem("我是新插入的数据", 4)
44+
```
45+
#### 尾部插入一条数据
46+
```kotlin
47+
adapter.appendItem("我是尾部新追加的数据")
48+
```
49+
#### 尾部插入多条数据
50+
```kotlin
51+
adapter.appendItem(mutableListOf("我是尾部新追加的数据A","我是尾部新追加的数据B",
52+
"我是尾部新追加的数据C", "我是尾部新追加的数据D"))
53+
```
54+
#### 移除一条数据
55+
```kotlin
56+
adapter.removeAt(4)
57+
```
58+
#### 移除多条数据
59+
```kotlin
60+
adapter.removeAt(checkedList)
61+
```
62+
#### 移除全部数据
63+
```kotlin
64+
adapter.removeAll()
65+
```
66+
#### 更新单条数据
67+
```kotlin
68+
adapter.refreshItem("这是更新过的数据", 4)
69+
```
70+
#### 更新全部数据
71+
```kotlin
72+
adapter.refreshAll(mutableListOf("新数据A", "新数据B", "新数据C", "新数据D", "新数据E", "新数据F", "新数据G", "新数据H"))
73+
```
74+
75+
**其它Adapter具体用法请参考Demo中的代码示例**
76+
**示例代码和说明采用Kotlin语言编写,如果你的项目是Java,用法与Kotlin基本类似**

library/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish {
77
userOrg = 'yanghongwei'
88
groupId = 'com.yhw.library'
99
artifactId = 'fastadapter'
10-
publishVersion = '0.0.2'
10+
publishVersion = '1.0.0'
1111
repoName="FastAdapter"
1212
desc = 'A fast adapter code library, suitable for RecyclerView, ListView, GridView, ViewPager, etc'
1313
website = 'https://github.com/LuckyCodeer/FastAdapter'
@@ -43,7 +43,6 @@ android {
4343
}
4444

4545
dependencies {
46-
4746
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
4847
implementation 'androidx.core:core-ktx:1.2.0'
4948
implementation 'androidx.appcompat:appcompat:1.1.0'
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.yhw.library.adapter
2+
3+
import android.text.TextUtils
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.BaseAdapter
8+
import android.widget.CheckBox
9+
import android.widget.ImageView
10+
import android.widget.TextView
11+
import androidx.annotation.DrawableRes
12+
import androidx.annotation.IdRes
13+
import androidx.annotation.StringRes
14+
15+
/**
16+
* ListView & GridView 基类Adapter
17+
* @param T 数据类型
18+
*/
19+
abstract class BaseListGridAdapter<T>(private var dataList: MutableList<T>) :
20+
BaseAdapter(), IAdapter<T> {
21+
private var layoutInflater: LayoutInflater? = null
22+
23+
override fun getCount(): Int {
24+
return dataList.size
25+
}
26+
27+
override fun getItem(position: Int): T {
28+
return dataList[position]
29+
}
30+
31+
override fun getItemId(position: Int): Long {
32+
return position.toLong()
33+
}
34+
35+
override fun insertItemToFirst(data: T) {
36+
this.dataList.add(0, data)
37+
this.notifyDataSetChanged()
38+
}
39+
40+
override fun insertItemToFirst(dataList: MutableList<T>) {
41+
this.dataList.addAll(0, dataList)
42+
this.notifyDataSetChanged()
43+
}
44+
45+
override fun insertItem(data: T, position: Int) {
46+
this.dataList.add(position, data)
47+
this.notifyDataSetChanged()
48+
}
49+
50+
override fun appendItem(data: T) {
51+
this.dataList.add(data)
52+
this.notifyDataSetChanged()
53+
}
54+
55+
override fun appendItem(dataList: MutableList<T>) {
56+
this.dataList.addAll(dataList)
57+
this.notifyDataSetChanged()
58+
}
59+
60+
override fun removeAt(position: Int) {
61+
this.dataList.removeAt(position)
62+
this.notifyDataSetChanged()
63+
}
64+
65+
override fun removeAt(positionList: MutableList<Int>) {
66+
for (position in positionList) {
67+
this.dataList.removeAt(position)
68+
}
69+
this.notifyDataSetChanged()
70+
}
71+
72+
override fun removeAll() {
73+
this.dataList.clear()
74+
this.notifyDataSetChanged()
75+
}
76+
77+
override fun refreshItem(data: T, position: Int) {
78+
this.dataList[position] = data
79+
this.notifyDataSetChanged()
80+
}
81+
82+
override fun refreshAll(dataList: MutableList<T>) {
83+
this.dataList = dataList
84+
this.notifyDataSetChanged()
85+
}
86+
87+
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
88+
if (layoutInflater == null) {
89+
layoutInflater = LayoutInflater.from(parent?.context)
90+
}
91+
val viewHolder: ViewHolder
92+
val view: View
93+
if (convertView == null) {
94+
view = layoutInflater?.inflate(getItemLayoutId(), parent, false)!!
95+
viewHolder = ViewHolder(view)
96+
viewHolder.itemViewType = getItemViewType(position)
97+
view.tag = viewHolder
98+
} else {
99+
view = convertView
100+
viewHolder = view.tag as ViewHolder
101+
}
102+
onBindViewItem(
103+
position,
104+
dataList[position],
105+
parent,
106+
viewHolder,
107+
)
108+
return view
109+
}
110+
111+
abstract fun getItemLayoutId(): Int
112+
113+
/**
114+
* @param position 索引位置
115+
* @param data item数据
116+
* @param holder 获取控件
117+
*/
118+
abstract fun onBindViewItem(
119+
position: Int,
120+
data: T,
121+
parent: ViewGroup?,
122+
holder: ViewHolder,
123+
)
124+
125+
class ViewHolder(var itemView: View) {
126+
private var viewMap: MutableMap<Int, View> = mutableMapOf()
127+
var itemViewType = 0
128+
129+
fun <T : View> getView(@IdRes viewId: Int): T {
130+
var view = viewMap[viewId]
131+
if (view == null) {
132+
view = itemView.findViewById(viewId)
133+
viewMap[viewId] = view
134+
}
135+
return view as T
136+
}
137+
138+
fun setText(@IdRes viewId: Int, text: String) {
139+
val textView: TextView = getView(viewId)
140+
textView.text = text
141+
}
142+
143+
fun setText(@IdRes viewId: Int, @StringRes textId: Int) {
144+
val textView: TextView = getView(viewId)
145+
textView.setText(textId)
146+
}
147+
148+
/**
149+
* 设置文本
150+
* 如果文本为null或者空字符串则隐藏控件
151+
*/
152+
fun setTextIfEmptyHide(@IdRes viewId: Int, text: String) {
153+
val textView: TextView = getView(viewId)
154+
if (TextUtils.isEmpty(text)) {
155+
textView.visibility = View.GONE
156+
} else {
157+
textView.visibility = View.VISIBLE
158+
textView.text = text
159+
}
160+
}
161+
162+
fun setImageResource(@IdRes viewId: Int, @DrawableRes resId: Int) {
163+
val imageView: ImageView = getView(viewId)
164+
imageView.setImageResource(resId)
165+
}
166+
167+
fun setChecked(@IdRes viewId: Int, isChecked: Boolean) {
168+
val checkBox: CheckBox = getView(viewId)
169+
checkBox.isChecked = isChecked
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)