0

我对这个很棒的图书馆有疑问。我有一个带有 4 个选项卡的 MainActivity:

public class MainActivity extends SherlockFragmentActivity {

// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml

    SharedPreferences pref= getSharedPreferences("wizard", 0);
    SharedPreferences.Editor editor = pref.edit();
    String isOkWizard= pref.getString("isOkWizard", null); //prelevo lo stato dello wizard
    if(isOkWizard!=null){
        //abilita tutto
    }else{
        Toast.makeText(this, "" 
    }
    setContentView(R.layout.activity_main);
    //toggleFullscreen(true);
   // setTheme(R.style.Theme_Sherlock_NoActionBar);
    // Activate Navigation Mode Tabs
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in activity_main.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Activate Fragment Manager
    FragmentManager fm = getSupportFragmentManager();

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            mActionBar.setSelectedNavigationItem(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

    // Capture tab button clicks
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Pass the position on tab click to ViewPager
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    };

    // Create first Tab
    tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create second Tab
    tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create third Tab
    tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
    mActionBar.addTab(tab);

 // Create 4 Tab
    tab = mActionBar.newTab().setText("Tab4").setTabListener(tabListener);
    mActionBar.addTab(tab);

}

现在在第一个选项卡中,我有一个带有 5 个按钮的 Fragment1,它们可以打开一个新片段。我的问题是用按钮监听器调用的新片段替换这个片段。这是我的第一个选项卡的 fragment1:

public class FragmentTab1 extends SherlockFragment {

    static Context context;

        @Override
        public SherlockFragmentActivity getSherlockActivity() {
            return super.getSherlockActivity();
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Get the view from fragmenttab1.xml


         //     View view = inflater.inflate(R.layout.fragmenttab1, container, false);
            RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.fragmenttab1,
                    container, false);
            context=getActivity();
            Button guasti = (Button) view.findViewById(R.id.bGuasti);
            Button segnala = (Button) view.findViewById(R.id.segnalaDisservizio);
            guasti.setOnClickListener(new OnClickListener() {

                    @Override
                public void onClick(View arg0) {
                    Fragment newFragment = new FragmentTab2();
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();

                    // Replace whatever is in the fragment_container view with this fragment,
                    // and add the transaction to the back stack
                    transaction.replace(R.id.fragment1, newFragment);
                    transaction.addToBackStack(null);

                    // Commit the transaction
                    transaction.commit();

                }
            });

            segnala.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(hasConnection()){
                    Intent i= new Intent(getActivity(), SegnalaDisservizioActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(i);
                    }else{
                        Toast.makeText(context, "Devi avere una connessione attiva per usurfruire del servizio", 3000).show();
                    }
                }
            });
            return view;
        }

        public static boolean hasConnection() {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo wifiNetwork = cm
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifiNetwork != null && wifiNetwork.isConnected()) {
                return true;
            }

            NetworkInfo mobileNetwork = cm
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mobileNetwork != null && mobileNetwork.isConnected()) {
                return true;
            }

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (activeNetwork != null && activeNetwork.isConnected()) {
                return true;
            }

            return false;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            setUserVisibleHint(true);
        }
4

1 回答 1

0

我相信您的所有 5 个片段都是从ViewPagerAdapter.

在您的活动中定义一个公共方法,例如

//tabNumber from 0 to 4
public void nextPage(int tabNumber)
{
   mPager.setCurrentItem(tabNumber);
}

并从您的片段中调用

int tab =1; //or any number
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.nextPage(tab);   

您的片段将自动移动到指定的编号。

我建议您在此处从 JakeWharton 下载查看寻呼机演示应用程序,并在此处下载相同的源代码。下次遇到困难时,您可以简单地检查应用程序的所需功能并向大师本人学习。

编辑!!!

如果你想完全替换片段,你应该在适配器中管理它,或者你不应该使用视图寻呼机。

对于片段演示,请在此处查看此应用程序源代码

检查此问题是否符合您的要求。无论如何,尽量让你的问题尽可能清楚。

于 2013-08-08T16:13:03.050 回答