每次滑动清除 ViewPager 中的片段数据
Clear data of fragments in ViewPager with each swipe
@Override
public void onViewDetachedFromWindow(@NonNull FragmentViewHolder holder) {
super.onViewDetachedFromWindow(holder);
}
我有很多独立的 EditText
在每个 viewpager 片段中都有不同的 id。对于每次滑动,我想清除这些 EditText
并将它们设为默认值。
我可以使用以下方法清除文本:
holder.itemview.findViewById(R.id.first).setText("something");
但它不会是明文。
我该怎么办?
编辑:整个测试代码:
我准备了一个测试片段。当我单击按钮时,它会更改当前片段中的文本。当我滑动到第二个片段时,它应该被清除或更改为默认值 xml。
测试片段
public class TestFragment extends Fragment {
private TestViewModel mViewModel;
private ViewPager2 mViewPager;
private Button btnTest;
public static TestFragment newInstance() {
return new TestFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.test_fragment, container, false);
btnTest=(Button)view.findViewById(R.id.etTest);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(TestViewModel.class);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = view.findViewById(R.id.myPager);
mViewPager.setSaveFromParentEnabled(false);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(),getLifecycle()));
TabLayout tabLayout = view.findViewById(R.id.tabs);
new TabLayoutMediator(tabLayout, mViewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(((ViewPagerAdapter)(mViewPager.getAdapter())).mFragmentNames[position]);//Sets tabs names as mentioned in ViewPagerAdapter fragmentNames array, this can be implemented in many different ways.
}
}
).attach();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Fragment current=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(tab.getPosition());//clear data here or below
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}});
btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int cur=((ViewPagerAdapter)(mViewPager.getAdapter())).getCurrentPos();
switch (cur){
case 0:
Fragment current=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(0);
TextView tF= current.getView().findViewById(R.id.first);
tF.setText("Clicked while 1");
break;
case 1:
Fragment current2=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(1);
TextView tF2= current2.getView().findViewById(R.id.second);
tF2.setText("Clicked while 2");
break;
}
}
});
}
public class ViewPagerAdapter extends FragmentStateAdapter {
private int currentPos;
private final Fragment[] mFragments = new Fragment[] {
new FirstFragment(),
new SecondFragment(),
};
public final String[] mFragmentNames = new String[] {
"First",
"Second"
};
public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@Override
public void onViewDetachedFromWindow(@NonNull FragmentViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if(currentPos!=0){
TextView textView =(TextView)holder.itemView.findViewById(R.id.first);//clear data here or up
}
}
@Override
public int getItemCount() {
return mFragments.length;
}
@Override
public long getItemId(int position) {
this.setCurrentPos(position);
return super.getItemId(position);
}
public int getCurrentPos() {
return currentPos;
}
public void setCurrentPos(int currentPos) {
this.currentPos = currentPos;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragments[position];
}
}}
我可以处理 onViewDetachedFromWindow 或 onTabUnselected。两者都可以改变视图。
第一个片段
public class FirstFragment extends Fragment {
private View mLeak;
private TextView tvTest;
public FirstFragment() {
}
public static FirstFragment newInstance(String param1, String param2) {
FirstFragment fragment = new FirstFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLeak = inflater.inflate(R.layout.fragment_first, container, false);
tvTest=mLeak.findViewById(R.id.first);
return mLeak;
}
@Override
public void onDetach() {
super.onDetach();
mLeak=null;
}}
方法一:
在您的 viewpager 上设置这一行:
viewPager.setOffscreenPageLimit(1);
这将在加载时再次刷新每个片段。
方法二:
创建一个接口并在每个片段上实现它。 In your activity/fragment where viewpager is defined, use "addOnPageChangeListener" on viewpager, and when the different page is selected, simply use the interface to get back the fragment instance and call the required methods in the fragment, to clear/change正文。
代码:
//Your Activity
class HomeActivity extends Activity{
...Methods...
public void setUpViewpager(){
//setup your viewpager
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
ChangeText fragment = (ChangeText) viewpager.getFragment(position);
fragment.changeText();
}
@Override
public void onPageScrollStateChanged(int state) {}
});
}
interface ChangeText{
void changeText();
}
}
//Your fragment
class FirstFragment extends Fragment implements HomeActivity.ChangeText{
...Methods...
public void changeText(){
//change the textview text here
}
}
class SecondFragment extends Fragment implements HomeActivity.ChangeText{
...Methods...
public void changeText(){
//change the textview text here
}
}
@Override
public void onViewDetachedFromWindow(@NonNull FragmentViewHolder holder) {
super.onViewDetachedFromWindow(holder);
}
我有很多独立的 EditText
在每个 viewpager 片段中都有不同的 id。对于每次滑动,我想清除这些 EditText
并将它们设为默认值。
我可以使用以下方法清除文本:
holder.itemview.findViewById(R.id.first).setText("something");
但它不会是明文。
我该怎么办?
编辑:整个测试代码: 我准备了一个测试片段。当我单击按钮时,它会更改当前片段中的文本。当我滑动到第二个片段时,它应该被清除或更改为默认值 xml。
测试片段
public class TestFragment extends Fragment {
private TestViewModel mViewModel;
private ViewPager2 mViewPager;
private Button btnTest;
public static TestFragment newInstance() {
return new TestFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.test_fragment, container, false);
btnTest=(Button)view.findViewById(R.id.etTest);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(TestViewModel.class);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = view.findViewById(R.id.myPager);
mViewPager.setSaveFromParentEnabled(false);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(),getLifecycle()));
TabLayout tabLayout = view.findViewById(R.id.tabs);
new TabLayoutMediator(tabLayout, mViewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(((ViewPagerAdapter)(mViewPager.getAdapter())).mFragmentNames[position]);//Sets tabs names as mentioned in ViewPagerAdapter fragmentNames array, this can be implemented in many different ways.
}
}
).attach();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Fragment current=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(tab.getPosition());//clear data here or below
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}});
btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int cur=((ViewPagerAdapter)(mViewPager.getAdapter())).getCurrentPos();
switch (cur){
case 0:
Fragment current=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(0);
TextView tF= current.getView().findViewById(R.id.first);
tF.setText("Clicked while 1");
break;
case 1:
Fragment current2=((ViewPagerAdapter)(mViewPager.getAdapter())).createFragment(1);
TextView tF2= current2.getView().findViewById(R.id.second);
tF2.setText("Clicked while 2");
break;
}
}
});
}
public class ViewPagerAdapter extends FragmentStateAdapter {
private int currentPos;
private final Fragment[] mFragments = new Fragment[] {
new FirstFragment(),
new SecondFragment(),
};
public final String[] mFragmentNames = new String[] {
"First",
"Second"
};
public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@Override
public void onViewDetachedFromWindow(@NonNull FragmentViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if(currentPos!=0){
TextView textView =(TextView)holder.itemView.findViewById(R.id.first);//clear data here or up
}
}
@Override
public int getItemCount() {
return mFragments.length;
}
@Override
public long getItemId(int position) {
this.setCurrentPos(position);
return super.getItemId(position);
}
public int getCurrentPos() {
return currentPos;
}
public void setCurrentPos(int currentPos) {
this.currentPos = currentPos;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragments[position];
}
}}
我可以处理 onViewDetachedFromWindow 或 onTabUnselected。两者都可以改变视图。
第一个片段
public class FirstFragment extends Fragment {
private View mLeak;
private TextView tvTest;
public FirstFragment() {
}
public static FirstFragment newInstance(String param1, String param2) {
FirstFragment fragment = new FirstFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLeak = inflater.inflate(R.layout.fragment_first, container, false);
tvTest=mLeak.findViewById(R.id.first);
return mLeak;
}
@Override
public void onDetach() {
super.onDetach();
mLeak=null;
}}
方法一:
在您的 viewpager 上设置这一行:
viewPager.setOffscreenPageLimit(1);
这将在加载时再次刷新每个片段。
方法二:
创建一个接口并在每个片段上实现它。 In your activity/fragment where viewpager is defined, use "addOnPageChangeListener" on viewpager, and when the different page is selected, simply use the interface to get back the fragment instance and call the required methods in the fragment, to clear/change正文。
代码:
//Your Activity
class HomeActivity extends Activity{
...Methods...
public void setUpViewpager(){
//setup your viewpager
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
ChangeText fragment = (ChangeText) viewpager.getFragment(position);
fragment.changeText();
}
@Override
public void onPageScrollStateChanged(int state) {}
});
}
interface ChangeText{
void changeText();
}
}
//Your fragment
class FirstFragment extends Fragment implements HomeActivity.ChangeText{
...Methods...
public void changeText(){
//change the textview text here
}
}
class SecondFragment extends Fragment implements HomeActivity.ChangeText{
...Methods...
public void changeText(){
//change the textview text here
}
}