Android Studio 当使用View Pager 2同时创建和加载多个页面时,它们都具有相同的内容

vxqlmq5t  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(222)

在这个例行的应用程序中,我试图加载多个页面的信息,并一次创建所有页面。我成功地使用视图分页器创建像这样的单独页面一次,但当我试图加载信息与一个forloop在一个时间,他们都结束了相同的内容,而不是各自的信息。我应该只是尝试使用加载每个页面之间的超时?
这就是代码:

//Get routine from intent
        userRoutine = (RoutineISO) getIntent().getSerializableExtra("routine");
        ArrayList<String> routineDays = userRoutine.getDays();
        //This foreach loop just goes through each day in the routine
        //and pull the data out of the respective maps
        //This is then used by the day adapter when building out the
        //fragment pages
        Log.i("DEBUG","Printing out days");
        for(String day: routineDays){
            this.dayExercises = userRoutine.getSplitDays().get(day);
            Log.i("DEBUG", dayExercises.toString());
            this.focusGroups = userRoutine.getMuscleGroups().get(day);
            this.dayTitle = day;
            NUM_PAGES++;
            pagerAdapter.notifyDataSetChanged();
            
        }

字符串
这是我正在使用的寻呼机适配器的代码

private class DayAdapter extends FragmentStateAdapter {
        private ArrayList<Fragment> fragments;
        private Context context;
        public DayAdapter(FragmentActivity fa) {
            super(fa);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // Return a NEW fragment instance in createFragment(int).
            Fragment fragment = new DayFragmentISO();

            Bundle args = new Bundle();
            args.putString("title", dayTitle);
            //Tells the fragment this is an existing day
            //so it knows to look for a focusList and dayExercises
            args.putBoolean("existing",true);
            args.putSerializable("focusList",focusGroups);
            args.putSerializable("dayExercises", dayExercises);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getItemCount() {
            return NUM_PAGES;
        }

    }


还请注意,我在Activity中将NUMPages作为private static int NUM_PAGES = 0;。

umuewwlo

umuewwlo1#

您不是在适配器中的数据项列表上进行迭代,而是设置单个数据项并从适配器中阅读该单个数据项。
适配器迭代Adapter::getSize()次。您的重写告诉它遍历NUM_PAGES数据项。当调用notifyDataSetChanged时,它在每次迭代时调用createFragment并执行该代码以创建一个片段。
问题是结构性的--您需要确保适配器观察到的数据集是可迭代的。
顺便说一下--为您添加的每个项目调用notifyDataSetChanged通常是一个糟糕的主意。适配器将在每次插入新页面时重新创建每个片段。
请注意,createFragment提供了一个参数int position。适配器(通常)接受一个List<Object>,并为该列表中的每个索引创建一个Object项。
提供的上下文太少,无法给予严格的解决方案,但理想情况下,您需要执行以下操作:
1.创建一个表示页面的类。它应该包含构造片段示例所需的所有数据的成员。

public class MyPageClass {

    // Maybe init fields here if they are required
    public MyPageClass(){} 

    private String dayTitle;
    private List<FocusGroup> focusGroups;
    private List<DayExercise> dayExercises;

    // Getters + Setters as needed, or any interface you want
    // < ... >

}

字符串
1.修改代码以构造页面对象列表

//Get routine from intent
        userRoutine = (RoutineISO) getIntent().getSerializableExtra("routine");
        ArrayList<String> routineDays = userRoutine.getDays();

        // Define a private data member pages as an empty ArrayList--You probably want to
        // clear it here.
        this.pages = new ArrayList<>();

        for(String day: routineDays){
            // Building our page
            MyPageClass page = new MyPageClass();
            page.setDayExercises(userRoutine.getSplitDays().get(day));
            page.setFocusGroup(userRoutine.getMuscleGroups().get(day));
            page.setDayTitle(day);
            // Adding it to the list that pagerAdapter observes
            this.pages.add(page);
        }

        // Now we notify the adapter, once all of these changes have finished.
        // Consider using more specific methods, like notifyItemRangeInserted() etc
        pagerAdapter.notifyDataSetChanged();


1.修改适配器代码以依赖于此列表。

private class DayAdapter extends FragmentStateAdapter {
       private ArrayList<Fragment> fragments;
       private ArrayList<MyPageClass> pages; // This is our list of MyPageClass instances
       private Context context;

       public DayAdapter(FragmentActivity fa, ArrayList<MyPageClass> pages) {
           super(fa);
           this.pages = pages;
       }

       @NonNull
       @Override
       public Fragment createFragment(int position) {
           // Return a NEW fragment instance in createFragment(int).
           Fragment fragment = new DayFragmentISO();

           Bundle args = new Bundle();
           args.putString("title", pages.get(position).getDayTitle());
           //Tells the fragment this is an existing day
           //so it knows to look for a focusList and dayExercises
           args.putBoolean("existing",true);
           args.putSerializable("focusList", pages.get(position).getFocusGroup()));
           args.putSerializable("dayExercises", pages.get(position).getDayExercises());
           fragment.setArguments(args);
           return fragment;
       }

       @Override
       public int getItemCount() {
           // This tells the adapter how many fragments to create.
           // We want one for each page, so we'll pass it the size of our data.
           return pages.size();
       }

   }

相关问题