Skip to content

Commit 81f6a77

Browse files
committed
日常提交 2022-07-18.
1 parent 871043e commit 81f6a77

10 files changed

Lines changed: 97 additions & 76 deletions

File tree

Quick/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ android {
2626
kotlinOptions {
2727
jvmTarget = '11'
2828
}
29+
buildFeatures {
30+
dataBinding true
31+
}
2932
}
3033

3134
dependencies {
3235

3336
implementation 'androidx.core:core-ktx:1.7.0'
3437
implementation 'androidx.appcompat:appcompat:1.4.2'
3538
implementation 'com.google.android.material:material:1.6.1'
36-
implementation 'androidx.databinding:databinding-runtime:7.2.1'
3739
//约束布局
3840
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
3941
//列表

Quick/src/main/java/com/wpf/app/quick/activity/ViewModelActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ abstract class ViewModelActivity<VM : BaseViewModel<H>, H : QuickView>(
4646
mViewModel = ViewModelProvider(
4747
this,
4848
ViewModelProvider.AndroidViewModelFactory(application)
49-
)
50-
.get(vmClass)
49+
).get(vmClass)
5150
bind(this, mViewModel)
5251
mViewModel?.baseView = this as H
5352
mViewModel?.onViewCreated(this as H)

Quick/src/main/java/com/wpf/app/quick/activity/ViewModelFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ open class ViewModelFragment<VM : BaseViewModel<H>, H : QuickView>(
3333
mViewModel = ViewModelProvider(
3434
this,
3535
ViewModelProvider.AndroidViewModelFactory(context!!.applicationContext as Application)
36-
)
37-
.get(vmClass)
36+
).get(vmClass)
3837
bind(this, mViewModel)
3938
mViewModel?.baseView = this as H
4039
mViewModel?.onViewCreated(this as H)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.wpf.app.quick.widgets.recyclerview
2+
3+
/**
4+
* Created by 王朋飞 on 2022/7/18.
5+
*
6+
*/
7+
interface DataChangeAdapter {
8+
9+
fun getAdapter(): QuickAdapter
10+
11+
fun size(): Int {
12+
return getAdapter().itemCount
13+
}
14+
15+
fun setNewData(mDataList: List<QuickItemData>?) {
16+
cleanAll()
17+
appendList(mDataList)
18+
getAdapter().notifyDataSetChanged()
19+
}
20+
21+
fun cleanAll() {
22+
getData()?.clear()
23+
getAdapter().notifyDataSetChanged()
24+
}
25+
26+
fun appendList(mDataList: List<QuickItemData>?) {
27+
if (mDataList == null) return
28+
if (getData() == null) {
29+
setData(arrayListOf())
30+
}
31+
getData()?.addAll(mDataList)
32+
}
33+
34+
fun addData(data: QuickItemData) {
35+
if (getData() == null) {
36+
setData(arrayListOf())
37+
}
38+
getData()?.add(data)
39+
}
40+
41+
fun addData(
42+
pos: Int,
43+
data: QuickItemData
44+
) {
45+
if (getData() == null) {
46+
setData(arrayListOf())
47+
}
48+
getData()?.add(pos, data)
49+
}
50+
51+
fun <T : QuickItemData> getRealTypeData(): MutableList<T>? {
52+
return getData() as? MutableList<T>
53+
}
54+
55+
fun getData(): MutableList<QuickItemData>? {
56+
return getAdapter().mDataList
57+
}
58+
59+
fun setData(newData: MutableList<QuickItemData>) {
60+
getAdapter().mDataList = newData
61+
}
62+
}

Quick/src/main/java/com/wpf/app/quick/widgets/recyclerview/QuickAdapter.kt

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,11 @@ import androidx.recyclerview.widget.RecyclerView
88
* Created by 王朋飞 on 2022/7/13.
99
*
1010
*/
11-
class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>() {
11+
class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>(), DataChangeAdapter {
1212

13-
private var mDataList: MutableList<QuickItemData>? = null
13+
var mDataList: MutableList<QuickItemData>? = null
1414

15-
var mQuickAdapterListener: QuickAdapterListener<out QuickItemData>? = null
16-
17-
fun setNewData(mDataList: List<QuickItemData>?) {
18-
cleanAll()
19-
appendList(mDataList)
20-
notifyDataSetChanged()
21-
}
22-
23-
fun cleanAll() {
24-
if (mDataList != null) {
25-
mDataList!!.clear()
26-
}
27-
notifyDataSetChanged()
28-
}
29-
30-
fun appendList(mDataList: List<QuickItemData>?) {
31-
if (mDataList == null) return
32-
if (this.mDataList == null) {
33-
this.mDataList = mutableListOf()
34-
}
35-
this.mDataList!!.addAll(mDataList)
36-
}
37-
38-
fun addData(data: QuickItemData) {
39-
if (mDataList == null) {
40-
mDataList = ArrayList()
41-
}
42-
mDataList!!.add(data)
43-
}
44-
45-
fun addData(
46-
pos: Int,
47-
data: QuickItemData
48-
) {
49-
if (mDataList == null) {
50-
mDataList = mutableListOf()
51-
}
52-
mDataList?.add(pos, data)
53-
}
54-
55-
fun getData(): MutableList<QuickItemData>? {
56-
return mDataList
57-
}
58-
59-
fun <T : QuickItemData> getRealTypeData(): MutableList<T>? {
60-
return mDataList as? MutableList<T>
61-
}
15+
private var mQuickAdapterListener: QuickAdapterListener<out QuickItemData>? = null
6216

6317
override fun onCreateViewHolder(
6418
viewGroup: ViewGroup,
@@ -68,11 +22,12 @@ class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>() {
6822
it.viewType == viewType
6923
}?.let { findData ->
7024
if (findData is QuickViewDataBinding<*>) {
71-
val holderAnnotationClass = findData::class.java.getAnnotation(BindBindingHolder::class.java)
25+
val holderAnnotationClass =
26+
findData::class.java.getAnnotation(BindBindingHolder::class.java)
7227
holderAnnotationClass?.let {
7328
val bindingHolderCls = it.value.java.getConstructor(ViewGroup::class.java)
74-
val bindingHolder =
75-
bindingHolderCls.newInstance(viewGroup) as QuickViewBindingHolder<QuickViewDataBinding<out ViewDataBinding>, out ViewDataBinding>
29+
val bindingHolder = bindingHolderCls.newInstance(viewGroup)
30+
as QuickViewBindingHolder<QuickViewDataBinding<out ViewDataBinding>, out ViewDataBinding>
7631
bindingHolder.mViewData = findData
7732
bindingHolder.onCreateViewHolder(bindingHolder.itemView)
7833
return bindingHolder as QuickViewHolder<QuickItemData>
@@ -117,4 +72,12 @@ class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>() {
11772
fun getQuickAdapterListener(): QuickAdapterListener<out QuickItemData>? {
11873
return mQuickAdapterListener
11974
}
75+
76+
fun setQuickAdapterListener(listener: QuickAdapterListener<out QuickItemData>?) {
77+
this.mQuickAdapterListener = listener
78+
}
79+
80+
override fun getAdapter(): QuickAdapter {
81+
return this
82+
}
12083
}

Quick/src/main/java/com/wpf/app/quick/widgets/recyclerview/QuickRecyclerView.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class QuickRecyclerView @JvmOverloads constructor(
1414
open val mContext: Context,
1515
open val attrs: AttributeSet? = null,
1616
open val defStyleAttr: Int = 0
17-
) : RecyclerView(mContext, attrs, defStyleAttr) {
17+
) : RecyclerView(mContext, attrs, defStyleAttr), DataChangeAdapter {
1818

1919
private lateinit var mQuickAdapter: QuickAdapter
2020

@@ -29,11 +29,7 @@ open class QuickRecyclerView @JvmOverloads constructor(
2929
adapter = mQuickAdapter
3030
}
3131

32-
fun size(): Int {
33-
return mQuickAdapter.itemCount
34-
}
35-
36-
fun getQuickAdapter() : QuickAdapter {
32+
override fun getAdapter() : QuickAdapter {
3733
return mQuickAdapter
3834
}
3935
}

Quick/src/main/java/com/wpf/app/quick/widgets/recyclerview/QuickRefreshRecyclerView.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.wpf.app.quick.utils.Callback
88
* Created by 王朋飞 on 2022/7/13.
99
*
1010
*/
11-
class QuickRefreshRecyclerView @JvmOverloads constructor(
11+
open class QuickRefreshRecyclerView @JvmOverloads constructor(
1212
override val mContext: Context,
1313
override val attrs: AttributeSet? = null,
1414
override val defStyleAttr: Int = 0
@@ -28,8 +28,8 @@ class QuickRefreshRecyclerView @JvmOverloads constructor(
2828
(mDataChangeListener as? DataChangeListener<RequestData, QuickItemData>)?.onRefresh(
2929
mRequestData, object : Callback<QuickItemData> {
3030
override fun callback(data: List<QuickItemData>?) {
31-
getQuickAdapter().setNewData(data)
32-
getQuickAdapter().notifyDataSetChanged()
31+
setNewData(data)
32+
adapter.notifyDataSetChanged()
3333
}
3434

3535
})
@@ -43,9 +43,9 @@ class QuickRefreshRecyclerView @JvmOverloads constructor(
4343
mRequestData,
4444
object : Callback<QuickItemData> {
4545
override fun callback(data: List<QuickItemData>?) {
46-
getQuickAdapter().appendList(data)
47-
getQuickAdapter().notifyItemRangeInserted(
48-
getQuickAdapter().itemCount - (data?.size ?: 0), (data?.size ?: 0)
46+
appendList(data)
47+
adapter.notifyItemRangeInserted(
48+
size() - (data?.size ?: 0), (data?.size ?: 0)
4949
)
5050
}
5151
})

app/src/main/java/com/wpf/app/quick/demo/SelectListTestActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class SelectListTestActivity : QuickActivity(R.layout.activity_recyclerview_test
4141
}
4242

4343
fun clean(view: View?) {
44-
list?.getQuickAdapter()?.cleanAll()
44+
list?.cleanAll()
4545
}
4646

4747
fun addMessage(view: View?) {
48-
list?.getQuickAdapter()?.addData(SelectItem())
49-
list?.getQuickAdapter()?.notifyItemInserted(list?.getQuickAdapter()?.itemCount ?: 0)
48+
list?.addData(SelectItem())
49+
list?.adapter?.notifyItemInserted(list?.size() ?: 0)
5050
}
5151
}

app/src/main/java/com/wpf/app/quick/demo/viewmodel/RecyclerViewTestViewModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class RecyclerViewTestViewModel : BindingViewModel<ActivityRecyclerviewTestBindi
1515
}
1616

1717
fun clean(view: View?) {
18-
getViewBinding()?.list?.getQuickAdapter()?.cleanAll()
18+
getViewBinding()?.list?.cleanAll()
1919
}
2020

2121
fun addMessage(view: View?) {
22-
getViewBinding()?.list?.getQuickAdapter()?.addData(BindDataTestModel())
23-
getViewBinding()?.list?.getQuickAdapter()?.notifyDataSetChanged()
22+
getViewBinding()?.list?.addData(BindDataTestModel())
23+
getViewBinding()?.list?.adapter?.notifyItemInserted(getViewBinding()?.list?.size() ?: 0)
2424
}
2525
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ android.nonTransitiveRClass=true
2828
IS_REMOTE=true
2929

3030
GROUP=com.wpf.app.quick
31-
VERSION_NAME=0.2.1
31+
VERSION_NAME=0.2.2
3232

3333
POM_DESCRIPTION=Quick Android.
3434

0 commit comments

Comments
 (0)