Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
luohaolun committed Jul 3, 2019
2 parents 0384a0c + 9117c98 commit 8c057c2
Showing 1 changed file with 91 additions and 15 deletions.
106 changes: 91 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# AbsListView通用适配器
# AbsListView RecyclerView 通用适配器


### 说明

超简化代码,高阶函数参数简化为1,使用非常方便。多类型注意数据源的构造 data:List<Pair<Int,T>>


### 添加依赖
Expand All @@ -15,11 +20,11 @@
> Module build.gradle
dependencies {
implementation 'com.github.luohaolun:adapter:1.3'
implementation 'com.github.luohaolun:adapter:1.4'
}


### 使用
### BaseAdapter使用

> 通常
Expand All @@ -28,10 +33,9 @@
data.add(i.toString())
}

lvList.adapter = Adapter(this, data, R.layout.item_test) { view, item, position ->
view.tvNum.text = item
view.btn.setOnClickListener { Toast.makeText(this, "点击$position", Toast.LENGTH_SHORT).show() }
}
lvList.adapter = Adapter(data, R.layout.item_test) {
tvNum.text = it
}



Expand All @@ -47,16 +51,88 @@
data.add(Pair(1, i.toString()))
}

lvList.adapter = MultiAdapter(this, data, SparseArray<Int>().apply { put(0, R.layout.item_test);put(1, R.layout.item_test_2) }) { view, item, type, position ->
when (type) {
0 -> {
view.tvNum.text = "$item 类型0"
view.btn.setOnClickListener { Toast.makeText(this, "点击$position 类型0", Toast.LENGTH_SHORT).show() }
>> MultiAdapter
lvList.adapter = MultiAdapter(data, SparseArray<Int>().apply { put(0, R.layout.item_test);put(1, R.layout.item_test_2) }) {
when (type) {
0 -> {
tvNum.text = "$it 类型0"
}
1 -> {
view.tvNum.text = "$item 类型1"
view.btn.setOnClickListener { Toast.makeText(this, "点击$position 类型1", Toast.LENGTH_SHORT).show() }
tvNum.text = "$it 类型1"
}
}
}

>> MulAdapter
lvList.adapter = MulAdapter(data, 0, R.layout.item_test) {
tvNum.text = "$it 类型1"
}.addItemType(1, R.layout.item_test_2) {
tvNum.text = "$it 类型2"
}





### RecyclerView.Adapter使用


> 通常
val data = mutableListOf<String>()
for (i in 0..10) {
data.add(i.toString())
}

recyList.adapter = RecyclerAdapter(data, R.layout.item_test) {
tvNum.text = it
}.setOnItemClickListener {
Toast.makeText(this@MainActivity, "点击 $position $it", Toast.LENGTH_SHORT).show()
}.setOnItemLongClickListener {
Toast.makeText(this@MainActivity, "长按 $position $it", Toast.LENGTH_SHORT).show()
}


> 多类型Item
val data = mutableListOf<Pair<Int, String>>()

for (i in 0..20) {
if (i < 4)
data.add(Pair(0, i.toString()))
else
data.add(Pair(1, i.toString()))
}

>> MultiRecyclerAdapter
recyList.adapter = MultiRecyclerAdapter(data, SparseArray<Int>().apply { put(0, R.layout.item_test);put(1, R.layout.item_test_2) }) {
when (type) {
0 -> {
tvNum.text = it
}
1 -> {
tvNum.text = it
}
}
}.setOnItemClickListener {
Toast.makeText(this@MainActivity, "点击 $position $it", Toast.LENGTH_SHORT).show()
}.setOnItemLongClickListener {
Toast.makeText(this@MainActivity, "长按 $position $it", Toast.LENGTH_SHORT).show()
}


>> MulRecyclerAdapter

recyList.adapter = MulRecyclerAdapter(data, 0, R.layout.item_test) {
tvNum.text = it
}.addItemType(1, R.layout.item_test_2) {
tvNum.text = it
}.setOnItemClickListener {
Toast.makeText(this@MainActivity, "点击 $position $it", Toast.LENGTH_SHORT).show()
}.setOnItemLongClickListener {
Toast.makeText(this@MainActivity, "长按 $position $it", Toast.LENGTH_SHORT).show()
}

0 comments on commit 8c057c2

Please sign in to comment.