我正在开展一个合作项目。我的目标是显示ViewPager项目位置。我尝试了几种可能的解决方案,例如尝试从addOnPageChangeListener获取并显示位置,在 IntroView 类中创建一个函数来获取位置但是无法获得我的位置通缉。这是我的代码结构。任何帮助都将很高兴。提前致谢。
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()));
}
这是我的 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);
}
}
这是我的 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;
}
}
这是我的片段
使用的 IntroPageViewpublic 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]);
}
这是我的 * 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;
}
}
答案 0 :(得分:0)
原来我忘了使用黄油ButterKnife.bind(这个);在初始化我的ViewPager时。因此,我能够使用addOnPageChangeListener来获取位置。这是解决方案:
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) {
}
});