search

Home  >  Q&A  >  body text

android - ViewPager使用FragmentPagerAdapter和Fragment时如何刷新显示的数据

现在是这种情况,我的ViewPager使用FragmentPagerAdapter作为Adapter,在adapter中的getItem中返回一个新的Fragmentfragment显示的数据是从网上拿下来的,但是这个数据我需要刷新,刷新之后显示也要跟着刷新才对。现在问题就来了,这种模式下应该如何刷新fragment中的数据呢?
下面是我设置ViewPager的代码和FragmentPagerAdapter的代码,里面的数据仅仅是个模拟,但真实情况类似:

int[] arg1 = new int[]{1,2,3,4,5,6,7};
int[] arg2 = new int[]{11,22,33,44,55,66,77,88,99};

final FragmentPager adapter = new FragmentPager(getSupportFragmentManager());
adapter.setData(arg1);
viewPager.setAdapter(adapter);
viewPager.setPageTransformer(true, new Transformer());

private class FragmentPager extends FragmentPagerAdapter {

        private int[] data;

        public FragmentPager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return BlankFragment.newInstance(data[position] +"");
        }

        @Override
        public int getCount() {
            return data.length;
        }

        public void setData(int[] arg) {
            data = arg;
        }
    }

下面是我的Fragment的代码(这个是根据Android Studio的模板生成的):

public class BlankFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";

    private String mParam1;

    public BlankFragment() {
    }

    public static BlankFragment newInstance(String param1 ) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        ((TextView)view.findViewById(R.id.fragment_center_text)).setText(mParam1);
        return view;
    }
}

现在,假如说数据改变了,在这个例子中就是adaptersetData传入的数据改变了,我应该怎么属性Fragment的数据呢?
我查过很多解决方法,但是POSITION_NONEnotifyDataSetChanged()在这里并不能发挥作用,而且我也不希望这么做,原因在这个问题里:https://segmentfault.com/q/1010000004572993

PHPzPHPz2772 days ago588

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 16:12:41

    Override getItemPosition to return POSITION_NONE to trigger the destruction and reconstruction of Fragment. However, this will lead to frequent destruction and reconstruction of Fragment, which is not the best method.
    AndgetItemPosition(Object object) the entry parameter object "representing an item" is actually a Fragment. You only need to provide a public method for updating the view for the Fragment, and then force the entry parameter to be converted into a custom Fragment, and call the update method of the Fragment to complete the update. .

    https://github.com/li2/Update_Replace_Fragment_In_ViewPager
    How to update and replace Fragment in ViewPager?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:12:41

    If the Tab may change, it is recommended to use FragmentStatePagerAdapter. FragmentPagerAdapter is designed for fixed Tab. Of course, it can also support refresh through the following solutions:

    private int mChildCount;
    
    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }
    
    @Override
    public int getItemPosition(Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    reply
    0
  • Cancelreply