android - 如何在 IconPageIndicator 中实现图标点击监听器

标签 android viewpagerindicator

我正在使用 viewPagerIndicator 库中的 IconPageIndicator。当在指示器中单击图标时,我需要查看寻呼机移动到该页面。单击图标时如何获取事件单击? 如何知道在 IconPageIndicator 中单击了哪个图标?

最佳答案

我已经阅读了你的问题和测试示例。你是对的,当我们点击图标时它不能移动。

所以你需要编辑库项目---> IconPageIndicator.java

只需替换下面的方法

public void notifyDataSetChanged() 
{
    mIconsLayout.removeAllViews();
    IconPagerAdapter iconAdapter = (IconPagerAdapter) mViewPager.getAdapter();
    int count = iconAdapter.getCount();
    for (int i = 0; i < count; i++) {
        ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);
        view.setImageResource(iconAdapter.getIconResId(i));
        view.setTag(""+i);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int viewPosition = Integer.parseInt(v.getTag().toString());

                mViewPager.setCurrentItem(viewPosition);
            }
        });
        mIconsLayout.addView(view);
    }
    if (mSelectedIndex > count) {
        mSelectedIndex = count - 1;
    }
    setCurrentItem(mSelectedIndex);
    requestLayout();
}

清理库项目并重建它。也不要忘记清理并重建您的项目。我希望它能工作。

关于android - 如何在 IconPageIndicator 中实现图标点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19629012/

相关文章:

java - 共享偏好 VS 上下文?

android - 如何使用类似 UI materializecss 框架的 android 设计?

android - 我可以在 android 2d 中用条纹或点填充形状吗

java - Mac OS 10.9 上的 Bluecove

Android:无法实例化 Activity ComponentInfo

android - 如何在 viewpagerindicator 中为 IconPageIndicator 设置分隔符填充

android - AppBarLayout fitsSystemWindows true attr 使 CircularIndicator 不可见

android - BroadcastReceiver peekService() 返回 null

android - ScrollView 中的 ViewPagerIndicator

android - ViewPagerIndicator 选项卡 : Icons above Text