Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 通过选项卡单击检测片段何时可​​见大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图检测片段在滑动视图上何时可见,以便在其可见时更新其内容.我是这样做的.

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;

@Override
protected 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));
    }







}


@Override
public 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;
}

@Override
public 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);
}

@Override
public void onTabSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction) {


        mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction) {
}

@Override
public 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 @R_260_10586@l 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();

从父活动中,您可以执行以下操作:

public Boolean checkIsFragVisible() {
        Fragment yourFragment = getSupportFragmentManager().findFragmentById(FRAG_HOLDER_ID);
        return yourFragment != null && yourFragment.isVisible();
    }

大佬总结

以上是大佬教程为你收集整理的android – 通过选项卡单击检测片段何时可​​见全部内容,希望文章能够帮你解决android – 通过选项卡单击检测片段何时可​​见所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。