我实现mTabs.addOnTabSelectedListener,并在onTabSelected上检查editext。如果字段不正确,我想保留在Tab1上。
我使用Viewpager,其中使用片段。
你是怎样做的?
主要活动:
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home_activity);
TabLayout tabs = findViewById(R.id.tabs);
ViewPager viewPager = findViewById(R.id.view_pager);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(HomeActivity.this, getSupportFragmentManager(), tabs, viewPager);
viewPager.setAdapter(sectionsPagerAdapter);
tabs.setupWithViewPager(viewPager);
}
}
FragmentAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@StringRes
private static final int[] TAB_TITLES = new int[] {
"TAB1",
"TAB2"
};
private final Context mContext;
private TabLayout mTabs;
private ViewPager mViewPager;
public SectionsPagerAdapter(Context context, FragmentManager fm, TabLayout tabs, ViewPager viewPager) {
super(fm);
mContext = context;
mTabs = tabs;
mViewPager = viewPager;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1, mContext, mTabs, mViewPager);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
谢谢。
最佳答案
片段类:
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private static int indexTab;
private static Context mContext;
private static TabLayout mTabs;
private static ViewPager mViewPager;
private PageViewModel pageViewModel;
public static PlaceholderFragment newInstance(int index, Context context, TabLayout tabs, ViewPager viewPager) {
mContext=context;
mTabs=tabs;
mViewPager=viewPager;
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
Log.v("index","index:"+index);
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("onCreate","onCreate");
pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
indexTab=index;
}
pageViewModel.setIndex(index);
}
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("onCreateView","onCreateView "+indexTab);
View root;
if(indexTab==2){
indexTab=1; //FORCE
root = inflater.inflate(R.layout.fragment_one, container, false);
mTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}else{
root = inflater.inflate(R.layout.fragment_two, container, false);
}
return root;
}
}