我对这个很棒的图书馆有疑问。我有一个带有 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);
}