在选项卡式布局 viewpager 中刷新当前片段
Refresh current fragment in tabbed layout viewpager
我正在尝试刷新 viewpager 选项卡式布局中的当前片段。我有 一个片段 (UserProgressFragment) 创建 ViewPager 并设置 适配器 (UserProgressAdapter) 以及创建选项卡式布局。
在我的 UserProgressAdapter 中,在 getItem() 方法中,我根据我所在的选项卡返回另外两个片段(UserWeightTrackerFragment 和 UserCalorieCounterFragment)。
我的问题是如何在单击按钮时从 UserCalorieCounterFragment 刷新片段并更新其 content/view,因为在 UserProgressFragment 中设置了对 viewpager 和适配器的访问权限? 我考虑过适配器的 notifyDataChange,但我不知道如何从这个 class 调用它,因为它是在 UserProgressFragment class 上设置的。想要刷新此页面的目的是在需要上下文时更新特定视图,即图表。
我附上了下面的代码:
UserProgressFragment
public class UserProgressFragment extends Fragment{
public static UserProgressFragment newInstance() {
return new UserProgressFragment();
}
public UserProgressFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_progress, container, false);
ViewPager viewPager = view.findViewById(R.id.progress_viewpager);
viewPager.setAdapter(new UserProgressTabsAdapter(getChildFragmentManager()));
TabLayout tabLayout = view.findViewById(R.id.progress_sliding_tabs);
tabLayout.setupWithViewPager(viewPager, true);
return view;
}
}
UserProgressTabsAdapter
public class UserProgressTabsAdapter extends FragmentStatePagerAdapter {
private static final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Weight Tracker", "Calorie Counter"};
public UserProgressTabsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return UserWeightTrackerFragment.newInstance(position);
case 1:
return UserCalorieCounterFragment.newInstance(position + 1);
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
UserCalorieCounterFragment(需要刷新这个)
public class UserCalorieCounterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
public static final String ARG_PAGE = "ARG_PAGE";
public static UserCalorieCounterFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
UserCalorieCounterFragment fragment = new UserCalorieCounterFragment();
fragment.setArguments(args);
return fragment;
}
public UserCalorieCounterFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_calorie_counter, container, false);
Button mAddCalories = view.findViewById(R.id.btn_add_calorie);
mAddCalories.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add_calorie:
//REFRESH/UPDATE HERE
break;
}
}
您可以在您的片段中创建一个 public 方法,其中包含将包含 notifyDataSetChanged()
函数的视图分页器。在视图寻呼机 fragment/item 中,您可以调用 getParentFragment()
并将其类型转换为您的 parent/first 片段,然后将 public 方法访问 notifyDataSetChanged()
。
我正在尝试刷新 viewpager 选项卡式布局中的当前片段。我有 一个片段 (UserProgressFragment) 创建 ViewPager 并设置 适配器 (UserProgressAdapter) 以及创建选项卡式布局。
在我的 UserProgressAdapter 中,在 getItem() 方法中,我根据我所在的选项卡返回另外两个片段(UserWeightTrackerFragment 和 UserCalorieCounterFragment)。
我的问题是如何在单击按钮时从 UserCalorieCounterFragment 刷新片段并更新其 content/view,因为在 UserProgressFragment 中设置了对 viewpager 和适配器的访问权限? 我考虑过适配器的 notifyDataChange,但我不知道如何从这个 class 调用它,因为它是在 UserProgressFragment class 上设置的。想要刷新此页面的目的是在需要上下文时更新特定视图,即图表。
我附上了下面的代码:
UserProgressFragment
public class UserProgressFragment extends Fragment{
public static UserProgressFragment newInstance() {
return new UserProgressFragment();
}
public UserProgressFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_progress, container, false);
ViewPager viewPager = view.findViewById(R.id.progress_viewpager);
viewPager.setAdapter(new UserProgressTabsAdapter(getChildFragmentManager()));
TabLayout tabLayout = view.findViewById(R.id.progress_sliding_tabs);
tabLayout.setupWithViewPager(viewPager, true);
return view;
}
}
UserProgressTabsAdapter
public class UserProgressTabsAdapter extends FragmentStatePagerAdapter {
private static final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Weight Tracker", "Calorie Counter"};
public UserProgressTabsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return UserWeightTrackerFragment.newInstance(position);
case 1:
return UserCalorieCounterFragment.newInstance(position + 1);
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
UserCalorieCounterFragment(需要刷新这个)
public class UserCalorieCounterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
public static final String ARG_PAGE = "ARG_PAGE";
public static UserCalorieCounterFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
UserCalorieCounterFragment fragment = new UserCalorieCounterFragment();
fragment.setArguments(args);
return fragment;
}
public UserCalorieCounterFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_calorie_counter, container, false);
Button mAddCalories = view.findViewById(R.id.btn_add_calorie);
mAddCalories.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add_calorie:
//REFRESH/UPDATE HERE
break;
}
}
您可以在您的片段中创建一个 public 方法,其中包含将包含 notifyDataSetChanged()
函数的视图分页器。在视图寻呼机 fragment/item 中,您可以调用 getParentFragment()
并将其类型转换为您的 parent/first 片段,然后将 public 方法访问 notifyDataSetChanged()
。