0

我对 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。但是,就我而言,这并没有发生。

请帮助我确定我哪里出错了。

谢谢!

4

2 回答 2

0

正如其他帖子中所记录的,当您处理 FragmentStatePageAdapter 时,您需要注意以下事项: 1. 用新片段替换旧片段后,通过调用 FragmentStatePageAdapter 通知适配器底层数据已更改.notifyDataSetChanged()。2. 您需要覆盖 getItemPosition()。在这个方法中,如果位于特定位置的片段没有改变并且如果你返回 POSITION_NONE,你会得到不正确的结果。

修改后的代码如下:

public class PatientPagerActivity extends FragmentActivity implements PatientPagerActivityCallbacks {
    private ViewPager mViewPager;
    private List<Patient> mPatientList;

    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(location, fragment);

                if(mPatientWithMiscFragmentList == null) {
                    mPatientWithMiscFragmentList = 
                            new ArrayList<PatientWithMiscFragment>();
                }
                mPatientWithMiscFragmentList.add(location, miscFragment);

                return fragment;
            }
            @Override
            public int getItemPosition(Object object) {
                PatientFragment fragment = (PatientFragment) object;
                if(mPatientFragmentList.get(mCurrentIndex).getCurrentPatient() != null && 
                    fragment.getCurrentPatient().getID().
                        equals(mPatientFragmentList.get(
                        mCurrentIndex).getCurrentPatient().getID())) {
                    return POSITION_NONE;
                } else if(mPatientFragmentList.get(mCurrentIndex).getCurrentPatient() == null) {
                    return POSITION_NONE;
                } else {
                    return POSITION_UNCHANGED;
                }

            }
        });

        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();
            }


            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();
        }
    }

}
于 2013-10-15T18:06:38.490 回答
-1

你可以使用一个简单的“FrameLayout”来用你的片段膨胀它。所以最新的片段将取代以前的片段。

是一个例子

祝你好运!

于 2013-10-14T17:50:14.003 回答