在viewpager适配器notifyDataSetChanged之后,pagetransformer不起作用

时间:2018-09-04 03:20:45

标签: android android-viewpager android-pagetransformer

我使用ViewPagerPageAdapter来显示我的数据,并且ViewPager具有其PageTransformer,它工作正常,现在当我滑动到新项目时需要更新数据和UI,但是问题是当我滑动到新项目并调用`adapter.notifyDataSetChanged()时,我的数据和UI确实更新了,但是我的PageTransformer却不起作用。

这是我的代码:

mAdapter = new DynamicsCardAdapter(getContext(), mList);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer2());
mViewPager.setPageMargin(-(int) (width * 0.30));
mViewPager.setAdapter(mAdapter);

适配器:

public class DynamicsCardAdapter extends PagerAdapter {

    private Context mContext;
    private List<ChildDynamicsInfo> mList;
    private int mPosition = -1;
    private int mChildCount = 0;


    public DynamicsCardAdapter(Context context, List<ChildDynamicsInfo> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup viewGroup, int position) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.item_parent_dynamics_card, viewGroup, false);
        textName = (TextView) convertView.findViewById(R.id.student_name);
        textName.setText(mList.get(position).name);
        viewGroup.addView(convertView);
        return convertView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        //return POSITION_NONE;
        if ( mChildCount > 0) {
            mChildCount --;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }
}

PageTransformer:

public class ZoomOutPageTransformer2 implements ViewPager.PageTransformer {

    private static final float MAX_ROTATE = 30.0f;


    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        float currentRotate = position * MAX_ROTATE;

        if (position < 0) {
            view.setPivotX(pageWidth / 2);
            view.setPivotY(view.getHeight());
            view.setRotation(currentRotate);
        } else if (position <= 1) {
            view.setPivotX(pageWidth / 2);
            view.setPivotY(view.getHeight());
            view.setRotation(currentRotate);
        } else if (position == 0) {
            view.setRotation(0);

        }
    }
}

更新数据:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mList.get(position).isRead = true;
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
});

我只是想在滑到新项目时更新UI,并确保我的PageTransformer正常工作,我曾经尝试过:

mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(position);

但是由于我在onPageSelected()上更新了UI,所以它将导致StackOverFlow

0 个答案:

没有答案