Skip to content

Commit

Permalink
增加多类型
Browse files Browse the repository at this point in the history
  • Loading branch information
luohaolun committed Jun 28, 2019
1 parent b1270b7 commit 9b91dd2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/k/lhl/adapter/Adapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.widget.BaseAdapter
class Adapter<T>(private val context: Context?, private val data: List<T>, private val layoutId: Int, private val bindView: (View, T, Int) -> Unit) : BaseAdapter() {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var view = convertView ?: LayoutInflater.from(context).inflate(layoutId, parent, false)
val view = convertView ?: LayoutInflater.from(context).inflate(layoutId, parent, false)
bindView(view, data[position], position)
return view
}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/k/lhl/adapter/MultiAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package k.lhl.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter

class MultiAdapter<T>(private val context: Context?, private val data: List<Pair<Int, T>>, private val layoutId: SparseArray<Int>, private val bindView: (View, T, Int, Int) -> Unit) : BaseAdapter() {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView ?: LayoutInflater.from(context).inflate(layoutId[data[position].first], parent, false)
bindView(view, data[position].second, data[position].first, position)
return view
}

override fun getItemViewType(position: Int): Int = data[position].first

override fun getViewTypeCount(): Int = layoutId.size()

override fun getItem(position: Int): T = data[position].second

override fun getItemId(position: Int) = position.toLong()

override fun getCount(): Int = data.size
}
38 changes: 32 additions & 6 deletions test/src/main/java/k/lhl/test/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package k.lhl.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.SparseArray
import android.widget.Toast
import k.lhl.adapter.Adapter
import k.lhl.adapter.MultiAdapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_test.view.*

Expand All @@ -12,14 +14,38 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val data = mutableListOf<String>()
for (i in 0..10) {
data.add(i.toString())
// val data = mutableListOf<String>()
// for (i in 0..10) {
// data.add(i.toString())
// }
//
// lvList.adapter = Adapter(this, data, R.layout.item_test) { v, item, position ->
// v.tvNum.text = item
// v.btn.setOnClickListener { Toast.makeText(this, "点击" + position, Toast.LENGTH_SHORT).show() }
// }

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()))
}

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

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() }
}
1 -> {
view.tvNum.text = "$item 类型1"
view.btn.setOnClickListener { Toast.makeText(this, "点击$position 类型1", Toast.LENGTH_SHORT).show() }
}
}
}


}
}
22 changes: 22 additions & 0 deletions test/src/main/res/layout/item_test_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">


<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="测试2" />

<TextView
android:id="@+id/tvNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

0 comments on commit 9b91dd2

Please sign in to comment.