简书链接:viewapgertablayout的自动化工具类MyTabLayoutMediator,参照viewpager2tablayout拷
文章字数:230,阅读全文大约需要1分钟

说明

之所以又用回viewpager+tablayout是因为有一个需求 这个东西出现了bug
当修改viewpager2的宽度变化会导致fragment显示不正常,重叠,位置不对,宽度不对等各种问题。
这是我提的问题
https://github.com/android/views-widgets-samples/issues/234
牛逼的你觉得没问题的可以指导一下

viewapger+tablayout的自动化工具类MyTabLayoutMediator,参照viewpager2+tablayout 拷贝的TabLayoutMediator轮子.

实现原理

实现原理很简单,从viewpager2的逻辑可以看出来autoRefresh是根据registerAdapterDataObserver实现的,
viewpager 就改成registerDataObserver
自动生成tab标题的回调还是使用TabLayoutMediator.TabConfigurationStrategy避免创建无意义的类。

用法和TabLayoutMediator一样

用法

1
2
3
4
5
6
7
8
9
myTabLayoutMediator = new MyTabLayoutMediator(getBinding().tabLayout, getBinding().viewpager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

tab.setText(data.get(position).first.getTitle());
}

});
myTabLayoutMediator.attach();

完整源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_DRAGGING;
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_IDLE;
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_SETTLING;

import android.database.DataSetObserver;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.lang.ref.WeakReference;

/**
* Author:Lozn
* Email:[email protected]
* 2021/12/3
* 15:58
*/
public class MyTabLayoutMediator {

private final TabLayout tabLayout;
private final ViewPager viewPager;
private final boolean autoRefresh;
private final TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy;
private final boolean smoothScroll;
private boolean attached;
private ViewPagerOnTabSelectedListener onTabSelectedListener;
private PagerAdapter adapter;
private PagerAdapterObserver pagerAdapterObserver;
private TabLayoutOnPageChangeCallback onPageChangeCallback;

public MyTabLayoutMediator(
@NonNull TabLayout tabLayout,
@NonNull ViewPager viewPager,
@NonNull TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy) {
this(tabLayout, viewPager, /* autoRefresh= */ true, tabConfigurationStrategy);
}

public MyTabLayoutMediator(
@NonNull TabLayout tabLayout,
@NonNull ViewPager viewPager,
boolean autoRefresh,
@NonNull TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy) {
this(tabLayout, viewPager, autoRefresh, /* smoothScroll= */ true, tabConfigurationStrategy);
}
public MyTabLayoutMediator(TabLayout tabLayout, ViewPager viewpager2, boolean autoRefresh, boolean smoothScroll,TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy) {
this.tabLayout = tabLayout;
this.viewPager = viewpager2;
this.smoothScroll=smoothScroll;
this.autoRefresh = autoRefresh;
this.tabConfigurationStrategy = tabConfigurationStrategy;
}
public MyTabLayoutMediator attach() {
if (attached) {
throw new IllegalStateException("TabLayoutMediator is already attached");
}
adapter = viewPager.getAdapter();
if (adapter == null) {
throw new IllegalStateException(
"TabLayoutMediator attached before ViewPager has an " + "adapter");
}
attached = true;
// Add our custom OnPageChangeCallback to the ViewPager
/* viewPager.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
viewPager.removeOnLayoutChangeListener(this);
populateTabsFromPagerAdapter();

}
});*/
onPageChangeCallback = new TabLayoutOnPageChangeCallback(tabLayout);
viewPager.addOnPageChangeListener(onPageChangeCallback);
// Now we'll add a tab selected listener to set ViewPager's current item
onTabSelectedListener = new MyTabLayoutMediator.ViewPagerOnTabSelectedListener(viewPager, smoothScroll);
tabLayout.addOnTabSelectedListener(onTabSelectedListener);
// Now we'll populate ourselves from the pager adapter, adding an observer if
// autoRefresh is enabled
if (autoRefresh) {
// Register our observer on the new adapter
// pagerAdapterObserver = new TabLayoutMediator.PagerAdapterObserver();
// adapter.registerAdapterDataObserver(pagerAdapterObserver);
pagerAdapterObserver = new PagerAdapterObserver();
adapter.registerDataSetObserver(pagerAdapterObserver);
}
populateTabsFromPagerAdapter();
// Now update the scroll position to match the ViewPager's current item
tabLayout.setScrollPosition(viewPager.getCurrentItem(), 0f, true);
return this;
}

public void detach() {
if (autoRefresh && adapter != null) {
adapter.unregisterDataSetObserver(pagerAdapterObserver);
pagerAdapterObserver = null;
}
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
viewPager.removeOnPageChangeListener(onPageChangeCallback);
onTabSelectedListener = null;
onPageChangeCallback = null;
adapter = null;
attached = false;
}
public boolean isAttached() {
return attached;
}

@SuppressWarnings("WeakerAccess")
void populateTabsFromPagerAdapter() {
tabLayout.removeAllTabs();
if (adapter != null) {
int adapterCount = adapter.getCount();
for (int i = 0; i < adapterCount; i++) {
TabLayout.Tab tab = tabLayout.newTab();
tabConfigurationStrategy.onConfigureTab(tab, i);
tabLayout.addTab(tab, false);
}
// Make sure we reflect the currently set ViewPager item
if (adapterCount > 0) {
int lastItem = tabLayout.getTabCount() - 1;
int currItem = Math.min(viewPager.getCurrentItem(), lastItem);
if (currItem != tabLayout.getSelectedTabPosition()) {
tabLayout.selectTab(tabLayout.getTabAt(currItem));
}
}
}
}
private static class TabLayoutOnPageChangeCallback implements ViewPager.OnPageChangeListener {
@NonNull private final WeakReference<TabLayout> tabLayoutRef;
private int previousScrollState;
private int scrollState;

TabLayoutOnPageChangeCallback(TabLayout tabLayout) {
tabLayoutRef = new WeakReference<>(tabLayout);
reset();
}

@Override
public void onPageScrollStateChanged(final int state) {
previousScrollState = scrollState;
scrollState = state;
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
TabLayout tabLayout = tabLayoutRef.get();
if (tabLayout != null) {
// Only update the text selection if we're not settling, or we are settling after
// being dragged
boolean updateText =
scrollState != SCROLL_STATE_SETTLING || previousScrollState == SCROLL_STATE_DRAGGING;
// Update the indicator if we're not settling after being idle. This is caused
// from a setCurrentItem() call and will be handled by an animation from
// onPageSelected() instead.
boolean updateIndicator =
!(scrollState == SCROLL_STATE_SETTLING && previousScrollState == SCROLL_STATE_IDLE);
tabLayout.setScrollPosition(position, positionOffset, updateText, updateIndicator);
}
}

@Override
public void onPageSelected(final int position) {
TabLayout tabLayout = tabLayoutRef.get();
if (tabLayout != null
&& tabLayout.getSelectedTabPosition() != position
&& position < tabLayout.getTabCount()) {
// Select the tab, only updating the indicator if we're not being dragged/settled
// (since onPageScrolled will handle that).
boolean updateIndicator =
scrollState == SCROLL_STATE_IDLE
|| (scrollState == SCROLL_STATE_SETTLING
&& previousScrollState == SCROLL_STATE_IDLE);
tabLayout.selectTab(tabLayout.getTabAt(position), updateIndicator);
}
}

void reset() {
previousScrollState = scrollState = SCROLL_STATE_IDLE;
}
}




/**
* A {@link TabLayout.OnTabSelectedListener} class which contains the necessary calls back to the
* provided {@link ViewPager2} so that the tab position is kept in sync.
*/
public static class ViewPagerOnTabSelectedListener implements TabLayout.OnTabSelectedListener {
private final ViewPager viewPager;
private final boolean smoothScroll;

ViewPagerOnTabSelectedListener(ViewPager viewPager, boolean smoothScroll) {
this.viewPager = viewPager;
this.smoothScroll = smoothScroll;
}

@Override
public void onTabSelected(@NonNull TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), smoothScroll);
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
// No-op
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
// No-op
}
}

private class PagerAdapterObserver extends DataSetObserver{
PagerAdapterObserver() {}

@Override
public void onChanged() {
populateTabsFromPagerAdapter();
}
/*
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
populateTabsFromPagerAdapter();
}

@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}

@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}

@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
populateTabsFromPagerAdapter();
}*/
}
}