简体   繁体   English

ViewPager-无法获取项目位置

[英]ViewPager - can't get the item position

I'm working on a collab project. 我正在做一个合作项目。 My goal here is to display the ViewPager item position. 我的目标是显示ViewPager项的位置。 I have tried several possible solutions such as trying getting from the addOnPageChangeListener and displaying the position, made a function to in IntroView class to get the position but couldn't get what I wanted. 我尝试了几种可能的解决方案,例如尝试从addOnPageChangeListener获取并显示位置,在IntroView类中创建了一个函数来获取位置,但无法获得我想要的。 Here's my code structure. 这是我的代码结构。 Any help would be gladly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。


InroActivity.java InroActivity.java

public class IntroActivity extends DialogFacebookActivity {

    private IntroView mIntroView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ChatterApplication.getApplicationComponent().inject(this);

        mIntroView = new IntroView(this);
        setContentView(mIntroView);
        mIntroView.setAdapter(new IntroPagerAdapter(getSupportFragmentManager()));



    }

Here's my IntroView.java 这是我的IntroView.java

public class IntroView extends CoordinatorLayout {

    @Bind(R.id.view_pager)
    ViewPager mViewPager;


    public IntroView(Context context) {
        super(context);
        init();
    }


    public IntroView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public IntroView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    public void setAdapter(@NonNull PagerAdapter pagerAdapter) {
        mViewPager.setAdapter(pagerAdapter);


    }


    public void goToNextPage() {
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1, true);

    }


    public int displayItem(){

      return mViewPager.getCurrentItem();
    }

    private void init() {
        inflate(getContext(), R.layout.view_intro, this);
        ButterKnife.bind(this);
    }

}

Here's my IntroFragment.java 这是我的IntroFragment.java

public class IntroFragment extends Fragment  {

    private static final String ARG_SECTION_NUMBER = "section_number";

    @Inject
    EventBus mEventBus;


    IntroView iv;

    ViewPager vp;





    public IntroFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ChatterApplication.getApplicationComponent().inject(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
        IntroPageView view;


        if (sectionNumber == IntroPagerAdapter.TUTORIAL_PAGE_COUNT) {
            view = new IntroPageViewLast(getContext());

        } else {
            view = new IntroPageView(getContext(), sectionNumber);

        }



        iv= new IntroView(getContext());
        vp =(ViewPager) iv.findViewById(R.id.view_pager);

        Toast.makeText(getContext(),"You are in: " + vp.getCurrentItem() ,Toast.LENGTH_LONG).show();


        return view;
    }



     public static IntroFragment newInstance(int sectionNumber) {

            IntroFragment fragment = new IntroFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

    }

Here's my IntroPageView which is used by my fragment 这是我的片段使用的IntroPageView

public class IntroPageView extends RelativeLayout {

    @Bind(R.id.intro_skip)
    TextView mSkipButton;

    @Bind(R.id.intro_tos_disclaimer_link)
    TextView disclaimer;

     @Bind(R.id.section_image)
    ImageView mImageView;

    private IntroPageViewListener mEventListener;



    public IntroPageView(Context context, int sectionNumber) {
            super(context);
            init(sectionNumber);
        }


        public void setListener(IntroPageViewListener listener) {
            this.mEventListener = listener;
        }
        protected void init(int sectionNumber) {
            inflate(getContext(), R.layout.view_intro_fragment, this);
            ButterKnife.bind(this);
            mImageView.setImageResource(IntroPagerAdapter.TUTORIAL_IMAGE_IDS[sectionNumber - 1]);
        }

Here's my *IntroPagerAdapter 这是我的* IntroPagerAdapter

public class IntroPagerAdapter extends FragmentPagerAdapter {

    public static final int[] TUTORIAL_IMAGE_IDS = {
      R.drawable.one,
      R.drawable.two,
      R.drawable.third,
      R.drawable.four
    };

    public static final int TUTORIAL_PAGE_COUNT = TUTORIAL_IMAGE_IDS.length;





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


    @Override
    public Fragment getItem(int position) {
        int sectionNumber = position + 1;
        return IntroFragment.newInstance(sectionNumber);
    }


    @Override
    public int getCount() {
        return TUTORIAL_PAGE_COUNT;
    }
}

Turns out I forgot to use the butter ButterKnife.bind(this); 原来我忘了用黄油ButterKnife.bind(this); while initializing my ViewPager. 在初始化我的ViewPager时。 Hence, I was able to use addOnPageChangeListener to get the position. 因此,我能够使用addOnPageChangeListener来获取位置。 Here's the solution: 解决方法如下:

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

        }

        @Override
        public void onPageSelected(int position) {
           // Toast.makeText(getApplicationContext(), "You are in.....: " + (position + 1),Toast.LENGTH_LONG).show();

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM