当前位置:网站首页>Bottomsheetdialogfragment + viewpager + fragment + recyclerview sliding problem
Bottomsheetdialogfragment + viewpager + fragment + recyclerview sliding problem
2022-04-23 13:21:00 【Zhang Hailong_ China】
Recently, a pop-up window needs several Tab Switch , Every Tab Is a list that can slide up and down , Based on this design , It's easy to think of using BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView, The old driver came up to do , It is found that after writing, only the first page list can slide, and other pages cannot slide ,what? What happened? , First eliminate the problem of your own code , Take questions and go Google, The main reason is because BottomSheetBehavior Of findScrollingChild The method has nothing to do with ViewPager Update to find child elements view Things that are , So it can only get one page to slide , Then we need to be right BottomSheetBehavior Make changes , In this case, you need to define yourself BottomSheetDialog:
Method 1:
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
After modification :
@VisibleForTesting
View findScrollingChild(View view) {
if (ViewCompat.isNestedScrollingEnabled(view)) {
return view;
}
if (view instanceof ViewPager) {
ViewPager viewPager = (ViewPager) view;
View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem());
// View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager);
if (currentViewPagerChild == null) {
return null;
}
View scrollingChild = findScrollingChild(currentViewPagerChild);
if (scrollingChild != null) {
return scrollingChild;
}
} else if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
Method 2. Be careful : This method is troublesome , Change your mind , Now that it's located to be ViewPager Sliding problem , Now? Google Not having ViewPager2 Yes , and ViewPager2 The bottom layer is made of RecycleView Realization , It should be able to solve this problem , It is as expected ,ViewPager2 It has indeed solved this problem Code sharing :
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="372dp"
android:background="@drawable/shape_gift_bg"
android:clickable="true"
android:orientation="vertical">
<View
android:layout_width="30dp"
android:layout_height="4dp"
android:layout_gravity="center_horizontal"
android:layout_marginVertical="10dp"
android:background="@drawable/shape_gray_525367_2dp" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tb_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:background="@color/transparent"
app:tabIndicatorColor="@android:color/transparent"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabRippleColor="@android:color/transparent" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_select_seat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" />
</LinearLayout>
</RelativeLayout>
Dialog Java Code :
public class RowWheatDialog extends BottomSheetDialogFragment {
private final static FunbitLog logger = new FunbitLog(RowWheatDialog.class.getSimpleName());
private RelativeLayout mRootView;
private TabLayout mTitleTabLayout;
private ViewPager2 mViewPager;
private OnClickedListener mListener;
private List<Fragment> mFragmentList = new ArrayList<>();
private List<VoiceRoomInfo> mBossSeatList = new ArrayList<>();
private List<VoiceRoomInfo> mRegularSeatList = new ArrayList<>();
private BossSeatFragment mBossSeatFragment;
private RegularSeatFragment mRegularSeatFragment;
private FragmentAdapter mFragmentAdapter;
private TabLayoutMediator mTabLayoutMediator;
private boolean isPPNeedPick = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_row_wheat, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
logger.d("onViewCreated()");
mRootView = view.findViewById(R.id.rl_root_view);
mTitleTabLayout = view.findViewById(R.id.tb_layout);
mViewPager = view.findViewById(R.id.vp_select_seat);
initTab();
mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
public void setOnClickedListener(OnClickedListener listener) {
this.mListener = listener;
}
public void setIsPPNeedPick(boolean ppNeedPick) {
this.isPPNeedPick = ppNeedPick;
}
public void setData(List<VoiceRoomInfo> bossSeatList, List<VoiceRoomInfo> regularSeatList) {
logger.d("setData bossSeatList : " + bossSeatList + " regularSeatList : " + regularSeatList);
this.mBossSeatList = bossSeatList;
this.mRegularSeatList = regularSeatList;
if (mBossSeatFragment != null) {
mBossSeatFragment.setData(bossSeatList);
}
if (mRegularSeatFragment != null) {
mRegularSeatFragment.setData(regularSeatList);
}
updateTitleTabLayoutTitle();
}
public void updateTitleTabLayoutTitle() {
if (mTitleTabLayout == null) {
return;
}
int tabCount = mTitleTabLayout.getTabCount();
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = mTitleTabLayout.getTabAt(i);
if (tab == null) {
continue;
}
if (tab.getPosition() == 0) {
View view01 = tab.getCustomView();
if (view01 != null) {
TextView textView01 = view01.findViewById(R.id.subtab_tv);
if (textView01 != null) {
textView01.setText(ResourceUtil.getString(R.string.boss_seat_label_count, (mBossSeatList == null ? 0 : mBossSeatList.size())));
}
}
} else {
View view02 = tab.getCustomView();
if (view02 != null) {
TextView textView02 = view02.findViewById(R.id.subtab_tv);
if (textView02 != null) {
textView02.setText(ResourceUtil.getString(R.string.regular_seat_label_count, (mRegularSeatList == null ? 0 : mRegularSeatList.size())));
}
}
}
}
}
public interface OnClickedListener {
void onPickClicked(VoiceRoomInfo voiceRoomInfo, boolean isPP, int position);
}
@Override
public void onResume() {
super.onResume();
logger.d("onResume() mBossSeatList " + mBossSeatList);
if (mBossSeatFragment != null) {
mBossSeatFragment.setData(mBossSeatList);
}
if (mRegularSeatFragment != null) {
mRegularSeatFragment.setData(mRegularSeatList);
}
updateTitleTabLayoutTitle();
}
public void initTab() {
logger.d("initTab()");
mBossSeatFragment = new BossSeatFragment();
mBossSeatFragment.setData(mBossSeatList);
mFragmentList.add(mBossSeatFragment);
if (isPPNeedPick) {
mRegularSeatFragment = new RegularSeatFragment();
mRegularSeatFragment.setData(mRegularSeatList);
mFragmentList.add(mRegularSeatFragment);
mTitleTabLayout.setVisibility(View.VISIBLE);
mRegularSeatFragment.setOnClickedListener(new RegularSeatFragment.OnClickedListener() {
@Override
public void onPickClicked(VoiceRoomInfo voiceRoomInfo, boolean isPP, int position) {
if (mListener != null) {
mListener.onPickClicked(voiceRoomInfo, true, position);
}
}
});
} else {
mTitleTabLayout.setVisibility(View.GONE);
}
mFragmentAdapter = new FragmentAdapter(getActivity());
mViewPager.setAdapter(mFragmentAdapter);
mTabLayoutMediator = new TabLayoutMediator(mTitleTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
logger.d("TabLayoutMediator onConfigureTab () " + tab + " position : " + position);
switch (position) {
case 0:
tab.setCustomView(R.layout.item_seat_subtab);
TextView textView0 = tab.getCustomView().findViewById(R.id.subtab_tv);
textView0.setText(ResourceUtil.getString(R.string.boss_seat_label_count, (mBossSeatList == null ? 0 : mBossSeatList.size())));
break;
case 1:
if (isPPNeedPick) {
tab.setCustomView(R.layout.item_seat_subtab);
TextView textView1 = tab.getCustomView().findViewById(R.id.subtab_tv);
textView1.setText(ResourceUtil.getString(R.string.regular_seat_label_count, (mRegularSeatList == null ? 0 : mRegularSeatList.size())));
}
break;
default:
break;
}
}
});
mTabLayoutMediator.attach();
mViewPager.registerOnPageChangeCallback(changeCallback);
mBossSeatFragment.setOnClickedListener(new BossSeatFragment.OnClickedListener() {
@Override
public void onPickClicked(VoiceRoomInfo voiceRoomInfo, boolean isPP, int position) {
if (mListener != null) {
mListener.onPickClicked(voiceRoomInfo, false, position);
}
}
});
}
private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
int tabCount = mTitleTabLayout.getTabCount();
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = mTitleTabLayout.getTabAt(i);
if (tab.getPosition() == position) {
updateTab(tab, true);
} else {
updateTab(tab, false);
}
}
}
};
private void updateTab(TabLayout.Tab tab, boolean isSelected) {
if (tab == null || tab.getCustomView() == null) {
return;
}
TextView textView = tab.getCustomView().findViewById(R.id.subtab_tv);
TextView textViewLine = tab.getCustomView().findViewById(R.id.subtab_line_view);
if (isSelected) {
textView.setTextColor(ResourceUtil.getColor(R.color.white));
textViewLine.setVisibility(View.VISIBLE);
textView.setTextAppearance(getContext(), R.style.TextStyleRobotoBold);
} else {
textView.setTextColor(ResourceUtil.getColor(R.color.AmongCancelColor));
textView.setTextAppearance(getContext(), R.style.TextStyleRobotoMedium);
textViewLine.setVisibility(View.GONE);
}
}
public class FragmentAdapter extends FragmentStateAdapter {
public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemCount() {
return mFragmentList.size();
}
}
@Override
public void onDestroyView() {
logger.d("onDestroyView()");
if (mTabLayoutMediator != null) {
mTabLayoutMediator.detach();
}
if (mViewPager != null) {
mViewPager.unregisterOnPageChangeCallback(changeCallback);
}
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
logger.d("onDestroy()");
}
}
版权声明
本文为[Zhang Hailong_ China]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230607201639.html
边栏推荐
- nodejs + mysql 实现简单注册功能(小demo)
- How to build a line of code with M4 qprotex
- AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
- How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
- CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
- Feature Engineering of interview summary
- [point cloud series] learning representations and generative models for 3D point clouds
- Loading and using image classification dataset fashion MNIST in pytorch
- PyTorch 21. NN in pytorch Embedding module
- Data warehouse - what is OLAP
猜你喜欢
filter()遍历Array异常友好
[point cloud series] neural opportunity point cloud (NOPC)
9419 page analysis of the latest first-line Internet Android interview questions
[point cloud series] relationship based point cloud completion
Common interview questions and detailed analysis of the latest Android developers in 2020
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
MySQL5.5安装教程
FatFs FAT32 learning notes
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
Riscv MMU overview
随机推荐
100000 college students have become ape powder. What are you waiting for?
web三大组件之Servlet
SHA512 / 384 principle and C language implementation (with source code)
Armv8m (cortex M33) MPU actual combat
Filter and listener of three web components
[point cloud series] unsupervised multi task feature learning on point clouds
vscode小技巧
MySQL 8.0.11下载、安装和使用可视化工具连接教程
GIS practical tips (III) - how to add legend in CASS?
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
[point cloud series] full revolutionary geometric features
100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
Pyqt5 store opencv pictures into the built-in sqllite database and query
Machine learning -- naive Bayes
FatFs FAT32 learning notes
2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
mui picker和下拉刷新冲突问题
[point cloud series] Introduction to scene recognition
Super 40W bonus pool waiting for you to fight! The second "Changsha bank Cup" Tencent yunqi innovation competition is hot!