0

我试图弄清楚如何确定显示的片段。我无法弄清楚如何开始编写这段代码。下面是我的来源。

分段

public class Fragment1 extends SherlockFragment {
    WebView web;
    static ProgressBar progressBar;

    @Override
    public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
        View v = inf.inflate(R.layout.activity_main, grp, false);
        web = (WebView) v.findViewById(R.id.webView);
        progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        setHasOptionsMenu(true);

        return v;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        web.setWebViewClient(new SpecialWebView(progressBar));
        web.setWebChromeClient(new SpecialWebChromeClient(progressBar));
        // Progress Bar - End

        WebSettings websettings = web.getSettings();
        websettings.setBuiltInZoomControls(false);
        web.setBackgroundColor(Color.parseColor("#f2f2f2"));
        websettings.setUseWideViewPort(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setAllowFileAccess(true);
        websettings.setDomStorageEnabled(true);
        websettings.setLoadWithOverviewMode(true);
        websettings.setSavePassword(true);
        web.loadUrl("http://www.google.com");   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater Inflater = getSupportMenuInflater();
        Inflater.inflate(R.menu.menu, menu);
        return true;

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem Item) {
        final int current = ((ViewPager) mFragments).getCurrentItem();
        String currentId = current == 0 ? FRAGMENT1: (current == 1 ? FRAGMENT2: FRAGMENT3);
        //WebView web = (WebView) findViewById(currentId);
        switch (Item.getItemId()) {
        case R.id.menu_back:
            web.goBack();
            return true;
        case R.id.menu_forward:
            web.goForward();
            return true;
        case R.id.menu_refresh:
            web.reload();
            return true;
        }
        return super.onOptionsItemSelected(Item);
    }

主要活动

  public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List<Fragment> mFragments;

private static final String FRAGMENT1 = Fragment1.class.getName();
private static final String FRAGMENT2 = Fragment2.class.getName();
private static final String FRAGMENT3 = SupportFragment.class.getName();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_main);

    // add fragments
    mFragments = new ArrayList<Fragment>();
    mFragments.add(Fragment.instantiate(this, FRAGMENT1));
    mFragments.add(Fragment.instantiate(this, FRAGMENT2));
    mFragments.add(Fragment.instantiate(this, FRAGMENT3));

    // adapter
    mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);

    // pager
    mPager = (ViewPager) findViewById(R.id.view_pager);
    mPager.setOffscreenPageLimit(3);
    mPager.setAdapter(mAdapter);

    // indicator
    mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
    mIndicator.setViewPager(mPager);
}
4

2 回答 2

0

我不确定你要继承什么适配器,但为什么不重写getView()回调来为你的活动设置一个类变量呢?然后,您可以在每次想知道显示哪个片段时检查变量的值。

@Override
public Fragment getItem(int position) {
    MainActivity.setCurrentlyShownFragment(position);
    return SomeFragment.newInstance(position);
}

这是我知道的最简单的方法。

于 2013-06-05T18:21:36.747 回答
0

弄清楚是否Fragment显示 a 并不难。在您的Fragment代码中,只需覆盖setUserVisibleHint(). 这需要一个布尔值,如果片段 UI 可见则为 true,否则为 false。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) { 
   //Do something
} else {  
   //Do something else
}

}

然后,您只需在条件语句的适当部分中包含您想要在可见案例和不可见案例中执行的操作。根据您尝试执行的操作,这可能涉及使用Interface将数据传递给包含片段的活动,以便您可以访问其他片段或执行某些基于活动的功能。

于 2013-06-05T18:38:36.917 回答