Android中Dialog Fragment内RecyclerView滚动条不显示问题排查
RecyclerView垂直滚动条在Dialog Fragment中不显示的问题排查与解决
最近我遇到了一个头疼的小问题——在Dialog Fragment里嵌入的RecyclerView,明明已经设置了android:scrollbars="vertical",但垂直滚动条就是死活不显示。折腾了好一阵才找到根源,分享给大家:
我的场景与尝试的方案
我在Dialog Fragment的布局中嵌入了RecyclerView,布局代码如下:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:padding="16dp" tools:context="com.airxos.app.common.fragments.LayerSwitcherFragment"> <TextView android:id="@+id/layer_list_title" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:fontFamily="sans-serif" android:textSize="20sp" android:textStyle="bold" android:textColor="@color/black_87" android:text="@string/layers" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/layer_list" android:layout_marginTop="@dimen/margin_16" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/layer_list_title" app:layout_constraintEnd_toEndOf="parent" android:layout_height="0dp" android:maxHeight="@dimen/info_window_height" android:layout_width="match_parent" android:nestedScrollingEnabled="true" android:scrollbars="vertical" android:background="@color/white" android:scrollbarThumbVertical="@drawable/scrollbar" android:scrollbarSize="5dp" android:padding="5dp" android:scrollbarStyle="insideInset"/> </android.support.constraint.ConstraintLayout>
同时在代码中配置了Dialog Fragment的显示逻辑和RecyclerView的基础设置:
@Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Objects.requireNonNull(inflater, "LayoutInflater"); if(getDialog().getWindow() != null) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().setCanceledOnTouchOutside(true); } return inflater.inflate(R.layout.fragment_layer_switcher, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { Objects.requireNonNull(view, "View"); super.onViewCreated(view, savedInstanceState); layerList = view.findViewById(R.id.layer_list); layerList.setAdapter(new LayerListAdapter(context, layers, layerSelectionListener)); layerList.setLayoutManager(new LinearLayoutManager(context)); }
显示Dialog Fragment的代码:
LayerSwitcherFragment switcherFragment = LayerSwitcherFragment.newInstance(layers); FragmentTransaction ft = getChildFragmentManager().beginTransaction(); Fragment prev = getChildFragmentManager().findFragmentByTag("layer_dialog"); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); switcherFragment.show(ft, "layer_dialog");
一开始我怀疑是主题冲突导致滚动条不显示,于是尝试了各种常见方案:
- 自定义
scrollbarThumbVertical的drawable样式 - 修改外层ConstraintLayout的背景色
- 确认
nestedScrollingEnabled设置为true - 翻遍Stack Overflow上的相关解决方案并逐一尝试
但所有操作都没能让滚动条显示出来。
最终找到的问题根源
最后才发现,我把这个DialogFragment迁移到aar库之后,主工程里居然还保留着同名的fragment_layer_switcher.xml布局文件!也就是说,应用运行时加载的是主工程里没有滚动条设置的旧布局,而非aar库中我已经更新过的新布局。
把主工程里的旧布局文件删除后,RecyclerView的垂直滚动条立刻就正常显示了!
内容的提问来源于stack exchange,提问作者hushed_voice
相关产品推荐
相关产品推荐

