在FragmentStatePagerAdapter

时间:2016-08-27 10:13:54

标签: android android-fragments fragment fragmentstatepageradapter

我的应用程序就像目录一样,我使用网格显示对象,人们可以选择是否需要该项目。

所以我用图像填充网格片段,其中一些片段是静态的(图像数量不会改变),其他人想要在添加新内容时更新(通缉项目)。可以单击图像以更改对应对象的数据。

这些片段是FragmentStatePagerAdapter的一部分,我无法更新动态片段,我已经尝试了很多解决方案,直到现在我只能使片段在屏幕旋转时自行重绘并且有效细

在我的MainActivity中,我有一个方法可以检查数据是否发生变化以及何时发出请求相应片段更新其视图。

private void updateFragments() {
    mData = ((Database) getApplication()).data;
    int[][] wanted = mData.getWanted();

    mAdapter.startUpdate(mViewPager);

    GridFragment f;
    if (!Arrays.deepEquals(mWanted, wanted)) {// mWanted.length != wantedT.length) {

        f = (GridFragment) mAdapter.getFragment(ArrayImages.NumberOfSeries);

        if (f != null)
            f.updateData(wanted);
    }

    mWanted = wanted;

    mAdapter.notifyDataSetChanged();
    mViewPager.getAdapter().notifyDataSetChanged();
}

在我的GridFragment中,我尝试使用此方法更新视图:

public void updateData(int[][] d) {
    data = d;

    if (this.getView() != null) {
        GridView gridView = (GridView) this.getView().findViewById(R.id.gridview);

        if (data != null)
        if (data.length > 0) {
            customAdapter = new CustomImageAdapter(this.getActivity(), data);

            gridView.setAdapter(customAdapter);

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(getActivity(), FullImageActivity.class);

                    // Passing array index
                    i.putExtra("pos", position);
                    if (series == ArrayImages.NumberOfSeries + 1)
                        i.putExtra("title", "Wanted");

                    Bundle mBundle = new Bundle();
                    mBundle.putSerializable("data", new Integer2D(data));
                    i.putExtras(mBundle);

                    startActivity(i);
                }
            });
        }
        else
        {
            //Clears the GridView
            gridView.setAdapter(null);
        }
    }

    notifyDataChanged();
}

我的ViewPagerAdapter看起来像这样:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
   private Map<Integer, String> mFragmentTags;
   private FragmentManager mFragmentManager;
   private SparseArray<Fragment> map;
   //
   ....
   //

    @Override
    public Fragment getItem(int position)
    {
            return newInstance(position + 1);
    }

    public final Fragment newInstance(int series) {
        Fragment f = new GridFragment();

        Bundle bdl = new Bundle(3);
        bdl.putInt("SERIES", series);

        //Wanted grid
        if(series == ArrayImages.NumberOfSeries+1){
           DBHelper data = ((Database) context.getApplicationContext()).data;
           bdl.putSerializable("DATA", data.getWanted());
           data.close();
        }

        f.setArguments(bdl);

        return f;
    }

   @Override
   public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        if (obj instanceof Fragment) {
            // record the fragment tag here.
            Fragment f = (Fragment) obj;
            String tag = f.getTag();
            mFragmentTags.put(position, tag);
        }

        return obj;
   }

    public Fragment getFragment(int position) {
       String tag = mFragmentTags.get(position);

       if (tag == null)
          return null;

       return mFragmentManager.findFragmentByTag(tag);
    }

    //Redraw on screen rotation
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

由于图像数量会发生变化,图像会自动更新,在返回MainActivity时,如何强制重绘片段或至少更新网格?

0 个答案:

没有答案