public class MyFragment extends Fragment { @OverrIDe public voID setUserVisibleHint(boolean isVisibletoUser) { super.setUserVisibleHint(isVisibletoUser); if (isVisibletoUser) { } else { } }}
问题是,当我进入页面时,通过它可以很好地工作,但当我在选项卡中选择页面时,它会崩溃“尝试调用虚拟方法”.
请帮帮我,谢谢
我有一个主要活动,然后是每个片段的活动.
主要:
public class Principal extends ActionBaractivity implements Actionbar.TabListener {SectionsPagerAdapter mSectionsPagerAdapter;/** * The {@link VIEwPager} that will host the section contents. */VIEwPager mVIEwPager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_principal); // Set up the action bar. final Actionbar actionbar = getSupportActionbar(); actionbar.setNavigationMode(Actionbar.NAVIGATION_MODE_TABS); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the VIEwPager with the sections adapter. mVIEwPager = (VIEwPager) findVIEwByID(R.ID.pager); mVIEwPager.setAdapter(mSectionsPagerAdapter); // When swiPing between different sections,select the corresponding // tab. We can also use Actionbar.Tab#select() to do this if we have // a reference to the Tab. mVIEwPager.setonPagechangelistener(new VIEwPager.SimpleOnPagechangelistener() { @OverrIDe public voID onPageSelected(int position) { actionbar.setSelectednavigationItem(position); } }); // For each of the sections in the app,add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page Title defined by // the adapter. Also specify this Activity object,which implements // the TabListener interface,as the callback (Listener) for when // this tab is selected. actionbar.addTab( actionbar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); }}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_principal,menu); return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button,so long // as you specify a parent activity in AndroIDManifest.xml. int ID = item.getItemID(); //noinspection SimplifiableIfStatement if (ID == R.ID.action_settings) { return true; } return super.onoptionsItemSelected(item);}@OverrIDepublic voID onTabSelected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) { mVIEwPager.setCurrentItem(tab.getposition());}@OverrIDepublic voID onTabUnselected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) {}@OverrIDepublic voID onTabReselected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) {}/** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @OverrIDe public Fragment getItem(int position) { Fragment fragment = new Fragment(); switch (position) { case 0: return fragment = new Config(); case 1: return fragment = new SaIDas(); case 2: return fragment = new EnTradas(); case 3: return fragment = new Enviar(); case 4: return fragment = new Historico(); case 5: return fragment = new Status(); default: break; } return fragment; } @OverrIDe public int getCount() { // Show 3 total pages. return 6; } @OverrIDe public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.Title_config).toupperCase(l); case 1: return getString(R.string.Title_saIDas).toupperCase(l); case 2: return getString(R.string.Title_enTradas).toupperCase(l); case 3: return getString(R.string.Title_enviar).toupperCase(l); case 4: return getString(R.string.Title_historico).toupperCase(l); case 5: return getString(R.string.Title_status).toupperCase(l); } return null; }}/** * A placeholder fragment containing a simple vIEw. */public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER,sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.fragment_principal,container,false); return rootVIEw; }}}
然后片段是:
public class EnTradas extends androID.support.v4.app.Fragment { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_enTradas,false); } @OverrIDe public voID setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { run code here.... }}解决方法 您可以通过以下两种方式执行此 *** 作:
从片段中你可以调用isVisible();
Return true if the fragment is currently visible to the user. This
means it: (1) has been added,(2) has its vIEw attached to the window,
and (3) is not hIDden.
从父活动中,您可以执行以下 *** 作:
public boolean checkIsFragVisible() { Fragment yourFragment = getSupportFragmentManager().findFragmentByID(FRAG_HolDER_ID); return yourFragment != null && yourFragment.isVisible(); }总结
以上是内存溢出为你收集整理的android – 通过选项卡单击检测片段何时可见全部内容,希望文章能够帮你解决android – 通过选项卡单击检测片段何时可见所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)