解决ViewPager + Fragment 数据刷新问题, 刷新无效, notifyDataSetChanged()无效

重写两个方法:getItemId()  getItemPosition()

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final String TAG = ViewPagerAdapter.class.getSimpleName();

    private List mList;

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

    public void setData(List mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int i) {
        return mList == null ? null : mList.get(i);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public long getItemId(int position) {
        return mList.get(position).hashCode();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if (mList.contains(object)) {
            // 如果当前 item 未被 remove,则返回 item 的真实 position
            Logger.d(TAG, "包含");
            return mList.indexOf(object);
        } else {
            // 否则返回状态值 POSITION_NONE
            Logger.d(TAG, "不包含");
            return POSITION_NONE;
        }
    }

}

你可能感兴趣的:(Android)