刷新其子片段时如何更改父项的自定义工具栏标题

时间:2018-09-05 07:06:39

标签: android android-fragments android-toolbar

我在一个片段中有一个viewPager,而vievPager包含2个子片段。

我在具有自定义布局的父片段中设置了一个自定义工具栏。滑动任何子片段时,都会刷新子片段。每次刷新时,我都想更改父片段标题,并在子片段中使用不同的值。

父母片段

public class FragmentDiscoverBase extends Fragment {

    public FragmentDiscoverBase() {
        // Required empty public constructor
    }

    //ViewPager viewPager;
    CustomViewPager viewPager;
    TabLayout tabLayout;

    String title1 = "";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_discover_base, container, false);

        //Find custom view pager
        viewPager = view.findViewById(R.id.customViewPager);
        //Disable horizontal swipe on view pager
        viewPager.setPagingEnabled(false);


                title1 = "   Location   ";


        //Custom action bar with
        showActionBar(title1);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        setHasOptionsMenu(true);

        setupViewPager(viewPager, title1, title2);

        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);


        return view;
    }


    private void setupViewPager(ViewPager viewPager, String title1, String title2) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new FragmentDiscover(), title1);
        adapter.addFragment(new FragmentDiscoverUpcoming(), title2);
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    //Set action bar title with custom font
    private void showActionBar(String mLocation) {

        LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.custom_toolbar, null);
        android.support.v7.app.ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();

        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        final TextView titleDiscover = v.findViewById(R.id.titleDiscover);


        if (isAdded()) {
                titleDiscover.setText(mLocation);
        }

        actionBar.setCustomView(v);
    }
}

儿童片段

public class FragmentDiscover extends Fragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    EditText etLocation;

    public static FragmentDiscover newInstance() {
        FragmentDiscover fragment = new FragmentDiscover();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    //Auto call offer when Internet available
    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_discover, container, false);

        etLocation = view.findViewById(R.id.etLocation);
        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout);

        //How can I set toolbar title on FragmentDiscoverBase, from etLocation input view

        //Swipe to refresh function
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //I Want to chnage 'FragmentDiscoverBase' title to default from here

            }
        });

        return view;
    }
}

请有人可以帮助我吗?提前致谢。

0 个答案:

没有答案