我在一个片段中有一个viewPager
,并且vievPager
包含2个子片段。
我在父片段中设置了一个自定义工具栏,它具有自定义布局。2当我滑动任何子片段时,我会刷新子片段。3当我每次刷新时,我希望用子片段中可用不同值来更改父片段标题。
父片段
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;
}
}
请问有谁能帮我吗?先谢谢了。
1条答案
按热度按时间ux6nzvsh1#
几乎4岁的“常见”问题,没有回应?我只是通过调用我的应用程序的任何地方实现了它。