如何在嵌套片段中加载回收者视图

时间:2019-07-05 19:20:56

标签: android android-fragments android-recyclerview

当我在日历视图中单击某天时,我正在尝试加载回收器视图。但是,当我单击它时,它不会将嵌套片段设置为任何内容,但是当我将其设置为更改父片段时,它将起作用。我阅读了有关使用子管理器和框架布局作为子片段视图的嵌套片段的信息。 https://guides.codepath.com/android/Creating-and-Using-Fragments#nesting-fragments-within-fragments

更新:已发布的父片段布局。

更新二:由于某种原因,recyclerview布局文件没有正确显示,但是我得到它来显示我想要的项目。我使用了片段小部件,并将其设置为其他片段,我猜不需要框架布局。

主要活动代码

    manager = getChildFragmentManager();
    calView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, dayOfMonth);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            dayFiltered = mViewModel.filterByDay(eventList,dayOfWeek);

            Log.d("fragment","events on day" + dayFiltered.get(0).getName());
            for (Event event : dayFiltered)
            {
                dayEventsHash.add(event.getName());
                dayTimeEventHash.add(event.getTime());
            }

            editor.putStringSet("dayNameFilteredEvents",dayEventsHash);
            editor.putStringSet("dayTimeFilteredEvents",dayTimeEventHash);
            editor.apply();

            // Fragment test;
            test = new CalendarEventListing();

            manager.beginTransaction().replace(R.id.homeFragSub,test,"calEventL").commit();


        }
    });

儿童片段代码

public static CalendarEventListing newInstance() {

    return new CalendarEventListing();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    view =  inflater.inflate(R.layout.calendar_event_listing_fragment, container, false);

    initRecyclerView(view);

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mViewModel = ViewModelProviders.of(this).get(CalendarEventListingViewModel.class);
    // TODO: Use the ViewModel
}

public void initRecyclerView(View view)
{

    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    hashNames = new HashSet<>();
    hashTimes = new HashSet<>();
    nullHash = new HashSet<>();

    hashNames.addAll(prefs.getStringSet("dayNameFilteredEvents",nullHash));
    hashTimes.addAll(prefs.getStringSet("dayTimeFilteredEvents",nullHash));


    eventNames = new ArrayList(hashNames);
    eventTimes = new ArrayList(hashTimes);

    Log.d("fragment","eventname size " + eventNames.size());

    RecyclerView recyclerView = view.findViewById(R.id.dayFilteredEventsRV);
    AdapterFive adapter = new AdapterFive(eventNames,eventTimes);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

}

父片段布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.HomeFrag">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="317dp"
        android:layout_height="317dp"
        android:layout_marginStart="47dp"
        android:layout_marginLeft="47dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="47dp"
        android:layout_marginRight="47dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/homeFragSub"
        android:layout_width="395dp"
        android:layout_height="368dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/calendarView">

    </FrameLayout>


</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案