我创建了带有视图寻呼机的3个标签页。有一个问题是在所有3个选项卡中我都有保存和退出按钮,但是当我向第一页滑动两次并按下保存或退出按钮时它将无法正常工作当移动到第二个片段时如果调用第三个片段视图。
public class TestActivity extends ActionBarActivity implements TabListener, OnPageChangeListener {
private String TAG = "FrmLsAdd: ";
private static String[] TestAr = new String[3];
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
private ViewPager mViewPager;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.farmer_add);
TestAr[0] = "Create 1";
TestAr[1] = "Create 2";
TestAr[2] = "Create 3";
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if (Utils.hasHoneycomb()) {
actionBar.setTitle("Farmer");
}
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(this);
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
String TAG = "Apdtr: ";
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment mFragment = new Fragment();
switch (i) {
case 0:
AppLog.logString(TAG + "case 0");
mFragment = Fragment1.newInstance(i);
break;
case 1:
AppLog.logString(TAG + "case 1");
mFragment = Fragment2.newInstance(i);
break;
case 2:
AppLog.logString(TAG + "case 2");
mFragment = Fragment3.newInstance(i);
break;
default:
AppLog.logString(TAG + "Default");
throw new IllegalArgumentException("not this many fragments: " + i);
}
return mFragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return TestAr[(position)];
}
}
public static class Fragment1 extends Fragment {
private String TAG = "Frg1: ";
private DatabaseHandler db;
private static Fragment1 f;
static Fragment1 newInstance(int num) {
f = new Fragment1();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add1, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AppLog.logString(TAG + "onbtnSave");
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog Alert_Dialog = new Dialog(getActivity());
getActivity().finish();
}
});
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}
}
public static class Fragment2 extends Fragment {
private String TAG = "Frg2: ";
private DatabaseHandler db;
private static Fragment2 f;
static Fragment2 newInstance(int num) {
f = new Fragment2();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add2, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AppLog.logString(TAG + "onbtnSave");
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public static class Fragment3 extends Fragment {
private String TAG = "Frg3: ";
private DatabaseHandler db;
private static Fragment3 f;
static Fragment3 newInstance(int num) {
f = new Fragment3();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add3, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}
}
}
@Override
public void onTabReselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
}
@Override
public void onTabSelected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
// mViewPager.setCurrentItem(arg0.getPosition());
mViewPager.setCurrentItem(this.getSupportActionBar().getSelectedNavigationIndex());
}
@Override
public void onTabUnselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(arg0);
}
}