我对 Android 编程完全陌生,我很难让它工作(我相当接近,但我错过了一些重要的东西)。
我有一个名为 PatientPagerActivity 的活动,它通过患者信息的片段进行分页。在每个 PatientFragment 中,我都有一个按钮,单击时,除了提供患者信息外,还会提供一些杂项信息。所以我打算在 PatientPagerActivity 中做的是将 PatientFragment 替换为 PatientWithMiscFragment (其中包含患者信息和其他部分)。
activity_pager.xml 文件:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PatientPagerActivity 文件:
public class PatientPagerActivity extends FragmentActivity
implements PatientPagerActivityCallbacks {
private ViewPager mViewPager;
private List<Patient> mQuizProblemList;
private int mCurrentIndex;
private List<PatientFragment> mPatientFragmentList;
private List<PatientWithMiscFragment> mPatientWithMiscFragmentList;
private static final String EMPTY_TEXT = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pager);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mPatientList =
PatientManager.getInstance(this).getPatients();
FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public int getCount() {
return mPatientList.size();
}
@Override
public Fragment getItem(int location) {
Patient patient = mPatientList.get(location);
PatientFragment fragment =
PatientFragment.newInstance(patient);
PatientWithMiscFragment miscFragment =
PatientWithMiscFragment.newInstance(patient);
if(mPatientFragmentList== null) {
mPatientFragmentList =
new ArrayList<PatientFragment>();
}
mPatientFragmentist.add(fragment);
if(mPatientWithMiscFragmentList == null) {
mPatientWithMiscFragmentList =
new ArrayList<PatientWithMiscFragment>();
}
mPatientWithMiscFragmentList.add(miscFragment);
return fragment;
}
});
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int counter) {
setTitle("Patient: " + (counter + 1));
mCurrentIndex = counter;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
hideMiscInformation();
}
});
long patientID =
getIntent().getLongExtra(PatientFragment.EXTRA_PATIENT_ID, -1);
for(int counter = 0; counter < mPatientList.size(); counter++) {
if(mPatientList.get(counter).getPatient().getID() == patientID) {
mViewPager.setCurrentItem(counter);
setTitle("Patient: " + (counter + 1));
mCurrentIndex = counter;
break;
}
}
}
@Override
public void onMiscSelected() {
if(findViewById(R.id.viewPager) != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//Find the current PatientInformation
Fragment oldFragment = mPatientFragmentList.get(mCurrentIndex);
//And Remove it
if(oldFragment != null) {
ft.remove(oldFragment);
ft.commit();
}
//Notify the adapter that data set has changed.
notifyAdapterDataSetChanged();
ft = fm.beginTransaction();
//Replace it with the corresponding fragment that also has misc
ft.add(R.id.viewPager,
mPatientWithMiscFragmentList.get(mCurrentIndex));
ft.commit();
notifyAdapterDataSetChanged();
}
}
public void hideMiscInformation() {
if(findViewById(R.id.viewPager) != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//Remove the misc information
Fragment oldMiscFragment =
fm.findFragmentById(R.id.viewPager);
if(oldMiscFragment != null) {
ft.remove(oldMiscFragment);
ft.commit();
}
//Replace it with ONLY the patient information.
ft = fm.beginTransaction();
ft.add(R.id.viewPager, mPatientFragmentList.get(mCurrentIndex));
ft.commit();
notifyAdapterDataSetChanged();
}
}
:
:
}
当用户对杂项信息感兴趣时,PagerActivity 会发生回调,onMiscSelected() 会将 PatientFragment 替换为 PatientWithMiscFragment。但是,就我而言,这并没有发生。
请帮助我确定我哪里出错了。
谢谢!