现在是这种情况,我的ViewPager
使用FragmentPagerAdapter
作为Adapter
,在adapter
中的getItem
中返回一个新的Fragment
,fragment
显示的数据是从网上拿下来的,但是这个数据我需要刷新,刷新之后显示也要跟着刷新才对。现在问题就来了,这种模式下应该如何刷新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;
}
}
现在,假如说数据改变了,在这个例子中就是adapter
的setData
传入的数据改变了,我应该怎么属性Fragment
的数据呢?
我查过很多解决方法,但是POSITION_NONE
和notifyDataSetChanged()
在这里并不能发挥作用,而且我也不希望这么做,原因在这个问题里:https://segmentfault.com/q/1010000004572993
天蓬老师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?
巴扎黑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);
}