Skip to content

Commit 4b6d8f5

Browse files
committed
新增BasePagingAdapter
1 parent 8d5e527 commit 4b6d8f5

File tree

17 files changed

+205
-489
lines changed

17 files changed

+205
-489
lines changed

app/src/main/java/com/fuusy/jetpackkt/MainActivity.kt

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,46 @@
11
package com.fuusy.jetpackkt
22

33
import android.os.Bundle
4-
import android.util.Log
54
import android.view.View
6-
import androidx.fragment.app.Fragment
7-
import androidx.fragment.app.FragmentActivity
85
import androidx.lifecycle.LiveData
96
import androidx.lifecycle.Observer
107
import androidx.navigation.NavController
11-
import androidx.viewpager2.adapter.FragmentStateAdapter
128
import com.alibaba.android.arouter.facade.annotation.Route
139
import com.fuusy.common.base.BaseActivity
1410
import com.fuusy.common.ktx.setupWithNavController
1511
import com.fuusy.common.support.Constants
16-
import com.fuusy.home.ui.HomeFragment
1712
import com.fuusy.jetpackkt.databinding.ActivityMainBinding
18-
import com.fuusy.personal.ui.PersonalFragment
19-
import com.fuusy.project.ui.ProjectFragment
13+
2014

2115
private const val TAG = "MainActivity"
2216

2317
@Route(path = Constants.PATH_MAIN)
2418
class MainActivity : BaseActivity<ActivityMainBinding>() {
2519
private var currentNavController: LiveData<NavController>? = null
2620

27-
companion object {
28-
private const val INDEX_HOME = 0
29-
private const val INDEX_PROJECT = 1
30-
private const val INDEX_PERSONAL = 2
31-
}
32-
33-
private val fragments = mapOf<Int, Fragment>(
34-
INDEX_HOME to HomeFragment(),
35-
INDEX_PROJECT to ProjectFragment(),
36-
INDEX_PERSONAL to PersonalFragment()
37-
)
38-
3921
override fun getLayoutId(): Int {
4022
return R.layout.activity_main
4123
}
4224

43-
private lateinit var mFragmentAdapter: VpFragmentAdapter
44-
4525
override fun initData(savedInstanceState: Bundle?) {
4626
if (savedInstanceState == null) {
4727
setupBottomNavigationBar()
4828
}
4929

50-
// mFragmentAdapter = VpFragmentAdapter(this, fragments)
51-
// mBinding?.run {
52-
// BnvMediator(navView,vpFragment){bnv,vp2->
53-
// vp2.isUserInputEnabled = false
54-
// }.attach()
55-
// }
56-
// mBinding?.vpFragment?.adapter = mFragmentAdapter
5730
}
5831

5932
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
6033
super.onRestoreInstanceState(savedInstanceState)
6134
setupBottomNavigationBar()
6235
}
6336

64-
class VpFragmentAdapter(fragment: FragmentActivity, private val fragments: Map<Int, Fragment>) :
65-
FragmentStateAdapter(fragment) {
66-
override fun getItemCount(): Int {
67-
return fragments.size
68-
}
69-
70-
override fun createFragment(position: Int): Fragment {
71-
return fragments[position] ?: error("ViewPager接收参数index越界")
72-
73-
}
74-
}
75-
76-
7737
/**
7838
* navigation绑定BottomNavigationView
7939
*/
8040
private fun setupBottomNavigationBar() {
8141
val navGraphIds =
8242
listOf(R.navigation.navi_home, R.navigation.navi_project, R.navigation.navi_personal)
8343

84-
mBinding.run {
85-
Log.d(TAG, "setupBottomNavigationBar: ")
86-
}
8744
val controller = mBinding?.navView?.setupWithNavController(
8845
navGraphIds = navGraphIds,
8946
fragmentManager = supportFragmentManager,
@@ -92,7 +49,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
9249
)
9350
controller?.observe(this, Observer { navController ->
9451
//setupActionBarWithNavController(navController)
95-
navController.addOnDestinationChangedListener { controller, destination, arguments ->
52+
navController.addOnDestinationChangedListener { _, destination, _ ->
9653
run {
9754
val id = destination.id
9855
if (id == R.id.projectContentFragment) {

common/src/main/java/com/fuusy/common/base/paging/BasePagingAdapter.kt

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,105 @@
11
package com.fuusy.common.base.paging
22

3+
import android.util.SparseArray
4+
import android.view.LayoutInflater
5+
import android.view.View
36
import android.view.ViewGroup
7+
import android.widget.ImageView
8+
import android.widget.TextView
49
import androidx.paging.PagingDataAdapter
510
import androidx.recyclerview.widget.DiffUtil
611
import androidx.recyclerview.widget.RecyclerView
12+
import com.bumptech.glide.Glide
713

8-
abstract class BasePagingAdapter<t : Any, VH : RecyclerView.ViewHolder>(var diffCallback: DiffUtil.ItemCallback<t>) :
9-
PagingDataAdapter<t, VH>(diffCallback) {
14+
private const val TAG = "BasePagingAdapter"
15+
16+
abstract class BasePagingAdapter<T : Any>(private var diffCallback: DiffUtil.ItemCallback<T>) :
17+
PagingDataAdapter<T, RecyclerView.ViewHolder>(diffCallback) {
1018

1119
companion object {
1220

1321
}
1422

15-
override fun onBindViewHolder(holder: VH, position: Int) {
16-
TODO("Not yet implemented")
23+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
24+
val item = getItem(position)
25+
(holder as BasePagingAdapter<*>.BaseViewHolder).bindNormalData(item)
26+
}
27+
28+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
29+
val holder = BaseViewHolder(parent, viewType)
30+
holder.itemView.setOnClickListener {
31+
onItemClick(getItem(holder.layoutPosition))
32+
}
33+
return holder
34+
}
35+
36+
override fun getItemViewType(position: Int): Int {
37+
return getItemLayout(position)
38+
}
39+
40+
/**
41+
* 子类获取layout
42+
*/
43+
protected abstract fun getItemLayout(position: Int): Int
44+
45+
/**
46+
* itemView的点击事件,子类实现
47+
*/
48+
protected abstract fun onItemClick(data: T?)
49+
50+
/**
51+
* 子类绑定数据
52+
*/
53+
protected abstract fun bindData(helper: ItemHelper, data: T?)
54+
55+
56+
inner class BaseViewHolder(parent: ViewGroup, layout: Int) : RecyclerView.ViewHolder(
57+
LayoutInflater.from(parent.context).inflate(layout, parent, false)
58+
) {
59+
private val helper: ItemHelper = ItemHelper(this)
60+
61+
fun bindNormalData(item: Any?) {
62+
bindData(helper, item as T)
63+
}
1764
}
1865

19-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
20-
TODO("Not yet implemented")
66+
67+
/**
68+
* ItemView的辅助类
69+
*/
70+
class ItemHelper(private val holder: BasePagingAdapter<*>.BaseViewHolder) {
71+
private val itemView = holder.itemView
72+
private val viewCache = SparseArray<View>()
73+
74+
private fun findViewById(viewId: Int): View {
75+
var view = viewCache.get(viewId)
76+
if (view == null) {
77+
view = itemView.findViewById(viewId)
78+
if (view == null) {
79+
throw NullPointerException("$viewId can not find this item!")
80+
}
81+
viewCache.put(viewId, view)
82+
}
83+
return view
84+
}
85+
86+
/**
87+
* TextView设置内容
88+
*/
89+
fun setText(viewId: Int, text: CharSequence?): ItemHelper {
90+
(findViewById(viewId) as TextView).text = text
91+
return this
92+
}
93+
94+
/**
95+
* Glide加载图片
96+
*/
97+
fun bindImgGlide(viewId: Int, url: String) {
98+
val imageView: ImageView = findViewById(viewId) as ImageView
99+
Glide.with(itemView)
100+
.load(url)
101+
.into(imageView)
102+
}
21103
}
22104

23105

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fuusy.common.base.paging
2+
3+
import androidx.paging.PagingSource
4+
import androidx.paging.PagingState
5+
import com.fuusy.common.network.BasePagingResp
6+
import com.fuusy.common.network.BaseResp
7+
8+
class BasePagingSource<T : Any>(private val block: suspend () -> BaseResp<BasePagingResp<List<T>>>,) : PagingSource<Int, T>() {
9+
10+
override fun getRefreshKey(state: PagingState<Int, T>): Int? = null
11+
12+
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, T> {
13+
return try {
14+
val pageNum = params.key ?: 1
15+
val respData = block.invoke()
16+
val preKey = if (pageNum > 1) pageNum - 1 else null
17+
LoadResult.Page(respData.data?.datas!!, prevKey = preKey, nextKey = pageNum + 1)
18+
} catch (e: Exception) {
19+
LoadResult.Error(e)
20+
}
21+
}
22+
}

common/src/main/java/com/fuusy/common/widget/FooterAdapter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fuusy.common.widget
22

3+
import android.util.Log
34
import android.view.LayoutInflater
45
import android.view.View
56
import android.view.ViewGroup
@@ -20,6 +21,7 @@ class FooterAdapter(private val retry: () -> Unit) :
2021
}
2122

2223
override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {
24+
Log.d(TAG, "onBindViewHolder: $loadState ")
2325
when (loadState) {
2426
is LoadState.Loading -> {
2527
holder.pagingBinding.progressBar.visibility = View.VISIBLE

common/src/main/res/layout/paging_footer_item.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
<Button
2424
android:id="@+id/bt_retry"
25-
android:layout_width="wrap_content"
25+
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
2727
app:layout_constraintStart_toStartOf="parent"
2828
app:layout_constraintEnd_toEndOf="parent"
2929
app:layout_constraintTop_toTopOf="parent"
30-
android:textSize="@dimen/text_size_14"
31-
android:text="点击重试"
30+
android:textSize="14sp"
31+
android:text="@string/load_error"
3232
android:visibility="gone"
3333
android:background="@null"
3434
app:layout_constraintBottom_toBottomOf="parent"/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
22
<string name="app_name">common</string>
3+
<string name="load_error">加载失败,请点击重试</string>
34
</resources>

home/src/main/java/com/fuusy/home/adapter/ArticleRvAdapter.kt

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)