将drawable添加到CirclePageIndicator

时间:2012-12-11 04:24:45

标签: android android-layout android-intent android-emulator android-viewpager

如何将图像添加到圆形pageindicator而不是添加颜色..我试图添加iconpage指标,但点击方法不起作用,但我可以添加图像.. 在circlepageIndicator onclick方法正在工作,但如何添加可绘制,图像到CIRCLEPAGE指标。以下是我的代码。

enter image description here

USED CirclePageIndicator

       indicator  = (CirclePageIndicator)findViewById(R.id.indicatorMenu);
    indicator.setViewPager(pagerMenu);

    final float density1 = getResources().getDisplayMetrics().density;
    indicator.setRadius(5 * density1);
    indicator.setPageColor(0xFF000000);
    indicator.setFillColor(0xFF888888);
  indicator.setStrokeColor(0x880000FF);
    indicator.setStrokeWidth(2 * density1);

    //We set this on the indicator, NOT the pager
    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {             
        @Override
        public void onPageSelected(int position) {
         //   Toast.makeText(HomeSpizzaView.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
        //  pagerMenu.setCurrentItem(position, false);            
        }
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageScrollStateChanged(int state) {               
        }
    });

    indicator.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int currentItem = pagerMenu.getCurrentItem();
               int totalItems = pagerMenu.getAdapter().getCount();
               int nextItem = (currentItem + 1) % totalItems;
               pagerMenu.setCurrentItem(nextItem);
         Log.i("kkkkk", "getCurrentItem"+  nextItem);
         Toast.makeText(HomeSpizzaView.this, "getCurrentItem"+  nextItem, Toast.LENGTH_SHORT).show();
        }
    });

USED IconPageIndicator

      mIndicator  = (IconPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    mIndicator.setLongClickable(true);

    //We set this on the indicator, NOT the pager
    mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {             
        @Override
        public void onPageSelected(int position) {
            Toast.makeText(HomeSpizzaView.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
            mPager.setCurrentItem(position, false);    
        }
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageScrollStateChanged(int state) {               
        }
    });


    mIndicator.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int currentItem = pagerMenu.getCurrentItem();
               int totalItems = pagerMenu.getAdapter().getCount();
               int nextItem = (currentItem + 1) % totalItems;
               pagerMenu.setCurrentItem(nextItem);
         Log.i("mmmmmmmmmmmm", "getCurrentItem"+  nextItem);
         Toast.makeText(HomeSpizzaView.this, "getCurrentItem"+  nextItem, Toast.LENGTH_SHORT).show();

        }

    });

2 个答案:

答案 0 :(得分:0)

您使用 CirclePageIndicator 对象来自定义页面指示符。您可以设置图像或自定义颜色。 您还可以使用XML自定义这些指标,您可以从给定的链接Android-ViewPagerIndicator's GitHub中找到示例。 如果您想使用图标或图片,则可以使用以下课程SampleIconsDefault.java。来自给定的样本

答案 1 :(得分:0)

我遇到了同样的问题,唯一的解决办法就是创建自己的库来实现这一目标。

https://github.com/augustopicciani/DrawablePageIndicator