当使用带有SectionsPagerAdapter的viewpager时,如何从当前片段更新ui

时间:2015-11-19 19:37:57

标签: android android-fragments android-viewpager

我正在使用viewpager和tabs,我添加了4个片段,每个片段都有一个recyclerveiw,我想让它更新,而用户正在创建新项目以存储在recycler视图中我的问题是oncreateview()我只调用一次,当我在这些视图之间滑动时,onresume()也没有被调用,所以我应该覆盖哪种方法,每次滑动都会调用,以便能够更新ui?我已经坐了2天了,如果有人可以帮助我,我找不到答案是我的代码。

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        tabCount = mFragments.size();
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {

       return mFragmentTitles.get(position);
    }
}
private void setupViewPager(ViewPager viewPager) {
    SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());

    Bundle args = new Bundle();
    PlaceholderFragment todayLayout = new PlaceholderFragment();
    args.putInt(PlaceholderFragment.ARG_LAYOUT_TYPE, PlaceholderFragment.LAYOUT_TYPE_TODAY);
    todayLayout.setArguments(args);

    Bundle args1 = new Bundle();
    PlaceholderFragment archiveLayout = new PlaceholderFragment();
    args1.putInt(PlaceholderFragment.ARG_LAYOUT_TYPE, PlaceholderFragment.LAYOUT_TYPE_ARCHIVE);
    archiveLayout.setArguments(args1);

    Bundle args2 = new Bundle();
    PlaceholderFragment helpLayout = new PlaceholderFragment();
    args2.putInt(PlaceholderFragment.ARG_LAYOUT_TYPE, PlaceholderFragment.LAYOUT_TYPE_HELP);
    helpLayout.setArguments(args2);

    Bundle args3 = new Bundle();
    PlaceholderFragment notDoneLayout = new PlaceholderFragment();
    args3.putInt(PlaceholderFragment.ARG_LAYOUT_TYPE, PlaceholderFragment.LAYOUT_TYPE_NOT_DONE);
    notDoneLayout.setArguments(args3);

    adapter.addFragment(todayLayout, "Do Today");
    adapter.addFragment(archiveLayout, "Archive");
    adapter.addFragment(helpLayout, "Help");
    adapter.addFragment(notDoneLayout, "Not Done");
    viewPager.setAdapter(adapter);
}

public class PlaceholderFragment extends Fragment {
    public static final int LAYOUT_TYPE_TODAY = 1;
    public static final int LAYOUT_TYPE_ARCHIVE = 2;
    public static final int LAYOUT_TYPE_HELP = 3;
    public static final int LAYOUT_TYPE_NOT_DONE = 4;

    public static final String ARG_LAYOUT_TYPE = "layout_arg_type";

    private int thisLayoutType = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.recycler_for_layouts, container, false);

        recyclerView =(RecyclerView) view.findViewById(R.id.recycler_view);
        setUpAdepter();


        thisLayoutType = getArguments().getInt(ARG_LAYOUT_TYPE, 0);


        setUpRecycler(recyclerView);

            return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        // Make sure that we are currently visible
        if (this.isVisible()) {
           helpAdepter.addItems(saveHelp);
            if (!isVisibleToUser) {

            }
        }
    }

    private void setUpAdepter() {
        todayAdepter= new ViewListAdepters(getContext(),new ViewListAdepters.ItemClickListener() {
            @Override
            public void onItemClicked(TodoDaily daily, int icon) {
                switch (icon){
                    case 0:
                        break;
                    case 1:
                        setPopUpForEdit(daily);
                        //here i want to update the ui 
                        break;
                    case 2:
                        setPopUpForDone(daily);

                        break;
                    case 3:
                        setPopUpForHelp();
                        break;
                    case 4:
                        setPopUpForNotDone();
                        break;
                    case 5:
                        setPopUpForShift();
                        break;
                    default:
                        break;
                }
            }
        });
        archiveAdepter= new ArchiveAdepter(getContext(),new ArchiveAdepter.ItemClickListener() {
            @Override
            public void onItemClicked(TodoDaily daily, int icon) {
                switch (icon){
                    case 0:
                        break;
                    case 1:
                        setPopUpForEdit(daily);
                        break;
                    case 2:
                        setPopUpForDone(daily);
                        break;
                    case 3:
                        setPopUpForHelp();
                        break;
                    case 4:
                        setPopUpForNotDone();
                        break;
                    case 5:
                        setPopUpForShift();
                        break;
                    default:
                        break;
                }
            }
        });
        helpAdepter= new HelpAdepter(getContext(),new HelpAdepter.ItemClickListener() {
            @Override
            public void onItemClicked(TodoDaily daily, int icon) {
                switch (icon){
                    case 0:
                        break;
                    case 1:
                        setPopUpForDone(daily);
                        break;
                    case 2:
                        setPopUpForNotDone();
                        break;
                    case 3:
                        setPopUpForShift();
                        break;
                    default:
                        break;
                }
            }
        });
        notDoneAdepter= new NotDoneAdepter(getContext(),new NotDoneAdepter.ItemClickListener() {
            @Override
            public void onItemClicked(TodoDaily daily, int icon) {
                switch (icon){
                    case 0:
                        break;
                    case 1:
                        setPopUpForDone(daily);
                        break;
                    case 2:
                        setPopUpForHelp();
                        break;
                    case 3:
                        setPopUpForShift();
                        break;
                    default:
                        break;
                }
            }
        });
        todayAdepter= new ViewListAdepters(getContext(),new ViewListAdepters.ItemClickListener() {
            @Override
            public void onItemClicked(TodoDaily daily, int icon) {
                switch (icon){
                    case 0:
                        break;
                    case 1:
                        setPopUpForEdit(daily);
                        break;
                    case 2:
                        setPopUpForDone(daily);
                        break;
                    case 3:
                        setPopUpForHelp();
                        break;
                    case 4:
                        setPopUpForNotDone();
                        break;
                    case 5:
                        setPopUpForShift();
                        break;
                    default:
                        break;
                }
            }
        });
    }
    private void setUpRecycler(RecyclerView recyclerView) {
        switch (thisLayoutType){
            case (LAYOUT_TYPE_TODAY):
                recyclerView.setAdapter(todayAdepter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                break;
            case (LAYOUT_TYPE_ARCHIVE):
                recyclerView.setAdapter(archiveAdepter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                break;
            case (LAYOUT_TYPE_HELP):
                recyclerView.setAdapter(helpAdepter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                break;
            case (LAYOUT_TYPE_NOT_DONE):
                recyclerView.setAdapter(notDoneAdepter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                break;
        }
    }

    private void showEmptyView(boolean isEmpty) {
        if(isEmpty){
            emptyText.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }else {
            emptyText.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我创建了一个简单的项目,演示了如何在ViewPager中更新RecycleView

您可以找到View Pager update with RecycleView

您可以在日志项目中看到FragmentStatePagerAdapter中的Lifecyrcle片段。

您可以使用ViewPager.OnPageChangeListener来了解现在选择的页面。

我希望这有帮助。