Fragment内RecyclerView无法渲染,模拟器显示空白屏求助
问题:Fragment中RecyclerView显示空白
我尝试在Fragment里展示带少量数据的RecyclerView,但始终显示空白屏幕。PreferenceMenuFragment通过XML嵌套在SettingsFragment布局中,页面层级如下:
NewDemoActivity(父Activity) SettingsFragment(NewDemoActivity的子Fragment) 1. PreferenceMenuFragment 2. DetailsFragment
相关代码实现
SettingsFragment
public class SettingFragment extends Fragment implements IMqttListener, IPropertyChangeListener, ViewVisibilityChangeListener { private NewDemoActivity mActivity; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = (NewDemoActivity) requireActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.settings_layout, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); }
settings_layout.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black"> <androidx.fragment.app.FragmentContainerView android:id="@+id/pref_fragment_container_view" android:name="com.xx.xx.xx.xx.PreferenceMenuFragment" android:layout_width="318dp" android:layout_height="500dp" android:layout_marginStart="46dp" android:layout_marginEnd="79dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/details_fragment_container_view" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" android:background="@color/white" app:layout_constraintTop_toTopOf="@+id/details_fragment_container_view" app:layout_constraintVertical_bias="0.0" /> <androidx.fragment.app.FragmentContainerView android:id="@+id/details_fragment_container_view" android:layout_width="540dp" android:layout_height="597dp" android:layout_marginTop="95dp" android:layout_marginEnd="41dp" android:layout_marginBottom="76dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/pref_fragment_container_view" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
PreferenceMenuFragment
public class PreferenceMenuFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private NewDemoActivity mActivity; private List<String> menu_titles; private int[] menu_icons=new int[]{R.drawable.brightness_icon_blue,R.drawable.bluetooth_icon_blue,R.drawable.camera_icon_blue,R.drawable.seat_alignment_icon_blue,R.drawable.notification_icon_blue,R.drawable.display_theme_icon_blue ,R.drawable.language_icon_blue,R.drawable.about_icon_blue}; private int[] selected_menu_icons=new int[]{R.drawable.brightness_icon_white,R.drawable.bluetooth_icon_white,R.drawable.camera_icon_white,R.drawable.seat_alignment_icon_white,R.drawable.notification_icon_white,R.drawable.display_theme_icon_white, R.drawable.language_icon_white,R.drawable.about_icon_white}; public PreferenceMenuFragment() { } public static PreferenceMenuFragment newInstance(String param1, String param2) { PreferenceMenuFragment fragment = new PreferenceMenuFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } menu_titles = Arrays.asList(getResources().getStringArray(R.array.setting_option_title)); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_preference, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); RecyclerView recyclerView = view.findViewById(R.id.setting_menu_recycler_view); recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL)); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); SettingsMenuAdapter menuAdapter=new SettingsMenuAdapter(getContext(),menu_titles,menu_icons,selected_menu_icons); recyclerView.setAdapter(menuAdapter); }
fragment_preference.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/setting_menu_recycler_view" android:layout_width="280dp" android:layout_height="500dp" android:layout_marginStart="25dp" android:layout_marginTop="50dp" android:layout_marginEnd="719dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.046" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.369" /> </androidx.constraintlayout.widget.ConstraintLayout>
SettingsMenuAdapter
public class SettingsMenuAdapter extends RecyclerView.Adapter<SettingsMenuViewHolder> { Context context; List<String> label; int[] icons; int[] selectedMenuIcons; int selectedPosition; public SettingsMenuAdapter(Context context, List<String> label, int[] icons, int[] selectedIcons) { this.context = context; this.label = label; this.icons = icons; this.selectedMenuIcons = selectedIcons; } @NonNull @Override public SettingsMenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new SettingsMenuViewHolder(LayoutInflater.from(context).inflate(R.layout.settings_item_layout,parent,false)); } @Override public void onBindViewHolder(@NonNull SettingsMenuViewHolder holder, int position) { holder.textView.setText(label.get(position)); if(position==selectedPosition){ holder.parentItemLayout.setBackground(context.getDrawable(R.drawable.shape_rounded_corner)); holder.textView.setTextColor(context.getResources().getColor(R.color.white,context.getTheme())); holder.imageView.setImageDrawable(context.getResources().getDrawable(selectedMenuIcons[holder.getBindingAdapterPosition()],context.getTheme())); }else{ holder.parentItemLayout.setBackground(null); holder.textView.setTextColor(context.getResources().getColor(R.color.seekbar_blue,context.getTheme())); holder.imageView.setImageDrawable(context.getResources().getDrawable(icons[holder.getBindingAdapterPosition()],context.getTheme())); } holder.parentItemLayout.setOnClickListener(v -> { selectedPosition=holder.getBindingAdapterPosition(); }); } @Override public int getItemCount() { return label.size(); }
settings_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/setting_item_parent_layout" android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal" android:layout_gravity="center" android:gravity="center" android:clickable="true"> <ImageView android:id="@+id/settingsItemImage" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/settingsItemLabel" android:layout_width="0dp" android:layout_weight="5" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:textColor="@color/white"/> </androidx.appcompat.widget.LinearLayoutCompat>
问题排查与修复方案
1. 修复RecyclerView布局约束
fragment_preference.xml中RecyclerView的android:layout_marginEnd="719dp"值过大,直接导致RecyclerView被挤压到屏幕外不可见。修改为合理值或移除该属性:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/setting_menu_recycler_view" android:layout_width="280dp" android:layout_height="500dp" android:layout_marginStart="25dp" android:layout_marginTop="50dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
2. 确保数据正确加载
检查res/values/strings.xml中setting_option_title数组是否存在且有内容:
<string-array name="setting_option_title"> <item>亮度调节</item> <item>蓝牙设置</item> <item>相机配置</item> <item>座椅调整</item> <item>通知管理</item> <item>显示主题</item> <item>语言设置</item> <item>关于我们</item> </string-array>
3. 补全ViewHolder控件绑定
确保SettingsMenuViewHolder正确初始化布局中的控件:
public class SettingsMenuViewHolder extends RecyclerView.ViewHolder { TextView textView; ImageView imageView; LinearLayoutCompat parentItemLayout; public SettingsMenuViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.settingsItemLabel); imageView = itemView.findViewById(R.id.settingsItemImage); parentItemLayout = itemView.findViewById(R.id.setting_item_parent_layout); } }
4. 修复点击事件UI刷新
点击列表项后更新selectedPosition但未触发刷新,导致选中状态不生效,修改Adapter点击事件:
holder.parentItemLayout.setOnClickListener(v -> { int oldPosition = selectedPosition; selectedPosition = holder.getBindingAdapterPosition(); notifyItemChanged(oldPosition); notifyItemChanged(selectedPosition); });
5. 排查颜色对比度问题
PreferenceMenuFragment背景为黑色,未选中状态文字颜色为seekbar_blue,若颜色过暗会看起来像空白。可临时修改未选中状态文字颜色为白色测试:
holder.textView.setTextColor(context.getResources().getColor(R.color.white, context.getTheme()));
内容的提问来源于stack exchange,提问作者Satyasarathi
相关产品推荐
相关产品推荐

