Skip to content

Commit cb22d09

Browse files
committed
新增ViewPager适配器以及Demo
1 parent e1b49b2 commit cb22d09

17 files changed

+585
-181
lines changed

library/src/main/java/com/yhw/library/adapter/BaseRecyclerAdapter.kt

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import android.view.ViewGroup
55
import androidx.recyclerview.widget.RecyclerView
66

77
/**
8-
* Recycler基类Adapter
8+
* RecyclerView基类Adapter
99
*/
1010
abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
11-
RecyclerView.Adapter<RecyclerViewHolder>() {
11+
RecyclerView.Adapter<RecyclerViewHolder>(), IAdapter<T> {
1212
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
1313
val view =
1414
LayoutInflater.from(parent.context).inflate(getItemLayoutId(viewType), parent, false)
@@ -23,18 +23,12 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
2323
return mDataList.size
2424
}
2525

26-
/**
27-
* 刷新整个列表数据
28-
*/
29-
fun refreshList(dataList: MutableList<T>) {
26+
override fun refreshAll(dataList: MutableList<T>) {
3027
this.mDataList = dataList
3128
this.notifyDataSetChanged()
3229
}
3330

34-
/**
35-
* 刷新单个条目的数据
36-
*/
37-
fun refreshItem(position: Int, data: T) {
31+
override fun refreshItem(data: T, position: Int) {
3832
if (position >= itemCount) {
3933
return
4034
}
@@ -45,7 +39,7 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
4539
/**
4640
* 插入数据
4741
*/
48-
fun insertItem(data: T, position: Int) {
42+
override fun insertItem(data: T, position: Int) {
4943
if (position > itemCount) {
5044
return
5145
}
@@ -59,7 +53,7 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
5953
/**
6054
* 首部插入数据
6155
*/
62-
fun insertItemToFirst(data: T) {
56+
override fun insertItemToFirst(data: T) {
6357
this.mDataList.add(0, data)
6458
this.notifyItemInserted(0)
6559
if (itemCount > 1) {
@@ -70,7 +64,7 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
7064
/**
7165
* 首部插入多条数据
7266
*/
73-
fun insertItemToFirst(dataList: MutableList<T>) {
67+
override fun insertItemToFirst(dataList: MutableList<T>) {
7468
this.mDataList.addAll(0, dataList)
7569
this.notifyItemRangeInserted(0, dataList.size)
7670
if (itemCount > dataList.size) {
@@ -81,23 +75,20 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
8175
/**
8276
* 尾部追加数据
8377
*/
84-
fun appendItem(data: T) {
78+
override fun appendItem(data: T) {
8579
this.mDataList.add(data)
8680
this.notifyItemInserted(itemCount)
8781
}
8882

8983
/**
9084
* 尾部追加多条数据
9185
*/
92-
fun appendItem(dataList: MutableList<T>) {
86+
override fun appendItem(dataList: MutableList<T>) {
9387
this.mDataList.addAll(dataList)
9488
this.notifyItemRangeInserted(itemCount, dataList.size)
9589
}
9690

97-
/**
98-
* 删除某个条目
99-
*/
100-
fun deleteItem(position: Int) {
91+
override fun removeAt(position: Int) {
10192
if (position >= itemCount) {
10293
return
10394
}
@@ -108,30 +99,25 @@ abstract class BaseRecyclerAdapter<T>(private var mDataList: MutableList<T>) :
10899
}
109100
}
110101

111-
/**
112-
* 批量删除条目
113-
*/
114-
fun deleteItem(positionList: MutableList<Int>) {
102+
override fun removeAt(positionList: MutableList<Int>) {
115103
if (positionList.size >= itemCount) {
116104
return
117105
}
118106
for (i in positionList) {
119107
this.mDataList.removeAt(i)
120-
this.notifyItemRemoved(i)
121-
}
122-
if (itemCount > 0) {
123-
this.notifyDataSetChanged()
124108
}
109+
this.notifyDataSetChanged()
125110
}
126111

127112
/**
128113
* 清除所有数据
129114
*/
130-
fun removeAll() {
115+
override fun removeAll() {
131116
this.mDataList.clear()
132117
this.notifyDataSetChanged()
133118
}
134119

120+
135121
/**
136122
* 布局文件
137123
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.yhw.library.adapter
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentManager
5+
import androidx.fragment.app.FragmentPagerAdapter
6+
7+
/**
8+
* ViewPage的基类adapter
9+
* Fragment
10+
*/
11+
class BaseViewPagerFragmentAdapter(
12+
fm: FragmentManager,
13+
private var fragmentList: MutableList<Fragment>
14+
) : FragmentPagerAdapter(fm), IAdapter<Fragment> {
15+
override fun getCount(): Int {
16+
return fragmentList.size
17+
}
18+
19+
override fun getItem(position: Int): Fragment {
20+
return fragmentList[position]
21+
}
22+
23+
override fun insertItem(data: Fragment, position: Int) {
24+
this.fragmentList.add(position, data)
25+
this.notifyDataSetChanged()
26+
}
27+
28+
override fun appendItem(data: Fragment) {
29+
this.fragmentList.add(data)
30+
this.notifyDataSetChanged()
31+
}
32+
33+
override fun appendItem(dataList: MutableList<Fragment>) {
34+
this.fragmentList.addAll(dataList)
35+
this.notifyDataSetChanged()
36+
}
37+
38+
override fun insertItemToFirst(data: Fragment) {
39+
this.fragmentList.add(0, data)
40+
this.notifyDataSetChanged()
41+
}
42+
43+
override fun insertItemToFirst(dataList: MutableList<Fragment>) {
44+
this.fragmentList.addAll(0, dataList)
45+
this.notifyDataSetChanged()
46+
}
47+
48+
override fun removeAt(position: Int) {
49+
this.fragmentList.removeAt(position)
50+
this.notifyDataSetChanged()
51+
}
52+
53+
override fun removeAt(positionList: MutableList<Int>) {
54+
for (position in positionList) {
55+
this.fragmentList.removeAt(position)
56+
}
57+
this.notifyDataSetChanged()
58+
}
59+
60+
override fun refreshItem(data: Fragment, position: Int) {
61+
this.fragmentList[position] = data
62+
this.notifyDataSetChanged()
63+
}
64+
65+
override fun refreshAll(dataList: MutableList<Fragment>) {
66+
this.fragmentList = dataList
67+
this.notifyDataSetChanged()
68+
}
69+
70+
override fun removeAll() {
71+
this.fragmentList.clear()
72+
this.notifyDataSetChanged()
73+
}
74+
75+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.yhw.library.adapter
2+
3+
import android.view.View
4+
import android.view.ViewGroup
5+
import androidx.viewpager.widget.PagerAdapter
6+
7+
/**
8+
* ViewPager 基类adapter
9+
* view
10+
*/
11+
class BaseViewPagerViewAdapter(private var viewList: MutableList<View>) : PagerAdapter(),
12+
IAdapter<View> {
13+
override fun getCount(): Int {
14+
return viewList.size
15+
}
16+
17+
override fun isViewFromObject(view: View, `object`: Any): Boolean {
18+
return view == `object`
19+
}
20+
21+
override fun instantiateItem(container: ViewGroup, position: Int): Any {
22+
val view = viewList[position]
23+
container.addView(view)
24+
return view
25+
}
26+
27+
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
28+
val view = viewList[position]
29+
container.removeView(view)
30+
}
31+
32+
override fun insertItemToFirst(data: View) {
33+
this.viewList.add(0, data)
34+
this.notifyDataSetChanged()
35+
}
36+
37+
override fun insertItemToFirst(dataList: MutableList<View>) {
38+
this.viewList.addAll(0, dataList)
39+
this.notifyDataSetChanged()
40+
}
41+
42+
override fun insertItem(data: View, position: Int) {
43+
this.viewList.add(position, data)
44+
this.notifyDataSetChanged()
45+
}
46+
47+
override fun appendItem(data: View) {
48+
this.viewList.add(data)
49+
this.notifyDataSetChanged()
50+
}
51+
52+
override fun appendItem(dataList: MutableList<View>) {
53+
this.viewList.addAll(dataList)
54+
this.notifyDataSetChanged()
55+
}
56+
57+
override fun removeAt(position: Int) {
58+
this.viewList.removeAt(position)
59+
this.notifyDataSetChanged()
60+
}
61+
62+
override fun removeAt(positionList: MutableList<Int>) {
63+
for (position in positionList) {
64+
this.viewList.removeAt(position)
65+
}
66+
this.notifyDataSetChanged()
67+
}
68+
69+
override fun refreshItem(data: View, position: Int) {
70+
this.viewList[position] = data
71+
this.notifyDataSetChanged()
72+
}
73+
74+
override fun refreshAll(dataList: MutableList<View>) {
75+
this.viewList = dataList
76+
this.notifyDataSetChanged()
77+
}
78+
79+
override fun removeAll() {
80+
this.viewList.clear()
81+
this.notifyDataSetChanged()
82+
}
83+
84+
85+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.yhw.library.adapter
2+
3+
interface IAdapter<T> {
4+
/**
5+
* 首部插入单条数据
6+
*/
7+
fun insertItemToFirst(data: T)
8+
9+
/**
10+
* 首部插入多条数据
11+
*/
12+
fun insertItemToFirst(dataList: MutableList<T>)
13+
14+
/**
15+
* 任意位置插入一条数据
16+
*/
17+
fun insertItem(data: T, position: Int)
18+
19+
/**
20+
* 尾部追加一条数据
21+
*/
22+
fun appendItem(data: T)
23+
24+
/**
25+
* 尾部追加多条数据
26+
*/
27+
fun appendItem(dataList: MutableList<T>)
28+
29+
/**
30+
* 移除单条数据
31+
* @param position 位置索引
32+
*/
33+
fun removeAt(position: Int)
34+
35+
/**
36+
* 移除多个数据
37+
*/
38+
fun removeAt(positionList: MutableList<Int>)
39+
40+
/**
41+
* 移除全部数据
42+
*/
43+
fun removeAll()
44+
45+
/**
46+
* 刷新单条数据
47+
*/
48+
fun refreshItem(data: T, position: Int)
49+
50+
/**
51+
* 刷新全部数据
52+
*/
53+
fun refreshAll(dataList: MutableList<T>)
54+
}

sample/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ dependencies {
4040
implementation 'com.google.android.material:material:1.1.0'
4141
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
4242
implementation project(path: ':library')
43+
// implementation 'com.yhw.library:fastadapter:0.0.1'
44+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
4345
testImplementation 'junit:junit:4.+'
4446
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
4547
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

sample/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"
1212
android:theme="@style/Theme.FastAdapter">
13-
14-
<activity android:name="com.yhw.demo.MainActivity">
13+
<activity android:name=".ViewPagerViewActivity"></activity>
14+
<activity android:name=".ViewPagerFragmentActivity" />
15+
<activity android:name=".MainActivity">
1516
<intent-filter>
1617
<action android:name="android.intent.action.MAIN" />
1718

1819
<category android:name="android.intent.category.LAUNCHER" />
1920
</intent-filter>
2021
</activity>
22+
<activity android:name=".RecyclerViewActivity" />
2123
</application>
2224

2325
</manifest>

0 commit comments

Comments
 (0)