页面滚动后,ViewPager内容无法重置

时间:2015-06-08 12:42:57

标签: android android-viewpager fragment

我有一个视图寻呼机,它有一个textview,最初设置为View.INVISIBLE,当页面聚焦时,它会淡入。

但是当我滚动到下一页并进行上一次查看时,文本视图处于可见状态,但应该处于INVISIBLE状态。

我尝试通过存储以前的索引将textview设置为invisble。但它没有重置textview。

index = indexCalculator(index);
    Log.d("HeadIndex",""+index);
    CustomViewPagerAdapter customViewPagerAdapter = (CustomViewPagerAdapter) viewPagerAdapter;
    ImageSliderFragment fragment = customViewPagerAdapter.getFragment(index);
    fragment.getView().findViewById(R.id.head_data).setVisibility(View.INVISIBLE); 

Canyone帮助我如何将视图设置为INVISIBLE。

更新代码:

private class CustomViewPagerAdapter extends FragmentStatePagerAdapter {
    private Map<Integer, ImageSliderFragment> mPageReferenceMap = new HashMap<>();

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

    @Override
    public Fragment getItem(int position) {
        ImageSliderFragment imageSliderFragment = new ImageSliderFragment();
        Bundle args = new Bundle();
        args.putInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION,position);
        imageSliderFragment.setArguments(args);
        mPageReferenceMap.put(Integer.valueOf(position),imageSliderFragment);
        return imageSliderFragment;
    }

    @Override
    public int getCount() {
        return AppConstants.NUM_PAGES;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;//super.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return  super.instantiateItem(container, position);
    }

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

    public ImageSliderFragment getFragment(int key) {
        return mPageReferenceMap.get(key);
    }
}

PageChangeListener代码:

public class CircularViewPagerHandler implements ViewPager.OnPageChangeListener {
private ViewPager mViewPager;
private int         mCurrentPosition;
private int         mScrollState;

///For determining swipe direction
private static final float thresholdOffset = 0.5f;
private boolean scrollStarted, checkDirection;

private HeadDataListener headDataListener;

public CircularViewPagerHandler(final ViewPager viewPager,HeadDataListener listener) {
    mViewPager = viewPager;
    headDataListener = listener;
}

@Override
public void onPageSelected(final int position) {
    mCurrentPosition = position;
    headDataListener.showHead(position);
    Log.d("OnPageSelected",""+position);
}

@Override
public void onPageScrollStateChanged(final int state) {
    handleScrollState(state);
    mScrollState = state;
    if (!scrollStarted && state == ViewPager.SCROLL_STATE_DRAGGING) {
        scrollStarted = true;
        checkDirection = true;
    } else {
        scrollStarted = false;
    }
}

private void handleScrollState(final int state) {
    if (state == ViewPager.SCROLL_STATE_IDLE) {
        setNextItemIfNeeded();
    }
}

private void setNextItemIfNeeded() {
    if (!isScrollStateSettling()) {
         handleSetNextItem();
    }
}

private boolean isScrollStateSettling() {
    return mScrollState == ViewPager.SCROLL_STATE_SETTLING;
}

private void handleSetNextItem() {
    final int lastPosition = mViewPager.getAdapter().getCount() - 1;
    if(mCurrentPosition == 0) {
        mViewPager.setCurrentItem(lastPosition, false);
    } else if(mCurrentPosition == lastPosition) {
        mViewPager.setCurrentItem(0, false);
    }
}

@Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
    if (checkDirection) {
        if (thresholdOffset > positionOffset) {
            AppConstants.SWIPE_DIRECTION = 1;
        } else {
            AppConstants.SWIPE_DIRECTION = 2;
        }
        checkDirection = false;
    }
}

}

我已经对我的班级实施了回调

1 个答案:

答案 0 :(得分:0)

在XML

处设置视图的可见性
  

机器人:能见度= “水涨船高”

然后在 CustomPagerAdapter

onPageSelected()方法更改视图的可见性