1

当用户借助在 FragmentActivity 上实现的 ViewPager 更改页面时,我正在寻找更新片段内容的绝佳解决方案。我已经实现了 FragmentActivity,如下所示: InformationActivity extends FragmentACtivity

 List<Fragment> fragments = new Vector<Fragment>();
  private void initialisePaging() {
         for(int i=0;i<getCount;i++){
                fragments.add(Fragment.instantiate(this, DetailInfoFragment.class.getName()));
         }          
        this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) super.findViewById(R.id.viewPager);
        pager.setAdapter(this.mPagerAdapter);
        pager.setCurrentItem(itemNumber);

        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                Log.v("onPageSelected", "arg0 "+arg0);

                MyAdapter getVal = new MyAdapter(InformationActivity.this);
                Log.v("InformationActivity ", "getTitle : == > "+getVal.titles[arg0]);
                Log.v("InformationActivity ", "getSubTitle : == > "+getVal.subTitles[arg0]);

                getSharedPreferences("Values", 0).edit().putString("Title", getVal.titles[arg0]).commit();
                getSharedPreferences("Values", 0).edit().putString("SubTitle", getVal.subTitles[arg0]).commit();
                getSharedPreferences("Values", 0).edit().putInt("Image", getVal.images[arg0]).commit();

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });


    }

我使用共享偏好在 Activity 和 Fragments 之间共享数据。

我已经实现了 DetailInfoFragment extends Fragments 如下:

        @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View f1Vew = inflater.inflate(R.layout.details, container, false);
    imageIcon = ((ImageView)f1Vew.findViewById(R.id.imagLrg));
    titleTxt = ((TextView)f1Vew.findViewById(R.id.titleText));
    subTxt = ((TextView)f1Vew.findViewById(R.id.subTitleText));
            String titleMain = getActivity().getSharedPreferences("Values", 0).getString("Title", "");
    String subMain = getActivity().getSharedPreferences("Values", 0).getString("SubTitle", "");

    Log.v("DetailInfoFragment : ", "tilte : "+titleMain);
    Log.v("DetailInfoFragment : ", "subMain : "+subMain);

    titleTxt.setText(""+titleMain);
    subTxt.setText(""+subMain);
    imageIcon.setImageResource(getActivity().getSharedPreferences("Values", 0).getInt("Image", 0));
    return f1Vew;
}  

从上面的实现中,当用户在查看寻呼机的帮助下在页面上滑动时,我从 InformationActivity 到 DetailsFragment 获得了相同的标题和子标题。但是我的问题是片段内容没有更新(titleTxt,subTxt,imageIcon)的标题,sub标题,图像图标。请在我的情况下如何更新片段内容

4

0 回答 0