Android Fragments 回收程序视图不在片段内部呈现

pb3skfrl  于 2023-08-06  发布在  Android
关注(0)|答案(1)|浏览(142)

我有一个片段,我试图显示一个recyclerview与一些数据。但似乎什么都不起作用,模拟器最终显示一个空白屏幕。
PreferenceMenuFragment通过xml添加到Settings Fragment布局中。我的活动和片段的完整层次结构如下。

NewDemoActivity(parent activity)
    SettingsFragment(Child of NewDemoActivity) 

                   1. PreferenceMenuFragment
                   2. DetailsFragment

字符串

设置片段

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 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";

// TODO: Rename and change types of parameters
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() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment PreferenceMenuFragment.
 */
// TODO: Rename and change types and number of parameters
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) {
    // Inflate the layout for this fragment
    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>

设置菜单适配器

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>

tez616oj

tez616oj1#

您的片段和适配器看起来正常。检查您的fragment_preference.xml一次。从RecyclerView中删除边距和约束并尝试一次。

相关问题