我对这个很棒的库有疑问。我有一个带有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);
}
答案 0 :(得分:0)
我相信您已经从ViewPagerAdapter
管理了所有5个片段。
在您的活动中定义一个公共方法,如
//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 here下载视图寻呼机演示应用程序以及相同here的源代码。下次遇到困难时,您只需检查应用程序是否有所需的功能,并向主人学习。
修改!!! 强>
如果您想完全替换片段,您应该在适配器中管理它,或者您不应该使用视图寻呼机。
对于片段演示,请检查this app和source code here
检查this question是否符合您的要求。无论如何,尽量让你的问题尽可能清楚。