简体   繁体   English

将drawable添加到CirclePageIndicator

[英]Adding drawable to CirclePageIndicator

How to add image to circle pageindicator instead of adding color.. I have tried to add iconpage indicator but on click method is not working but i can add Image.. In circlepageIndicator onclick method is working but HOW TO ADD DRAWABLE, IMAGE TO CIRCLEPAGE INDICATOR. 如何添加图像到圆形pageindicator而不是添加颜色..我试图添加iconpage指标,但点击方法不起作用,但我可以添加图像..在circlepageIndicator onclick方法工作但如何添加drawable,图像到CIRCLEPAGE指标。 BELOW IS MY CODE. 以下是我的代码。

在此输入图像描述

USED CirclePageIndicator 使用的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 使用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();

        }

    });

You an use CirclePageIndicator object to customize page-indicator. 您使用CirclePageIndicator对象来自定义页面指示器。 you can set image or customize color. 您可以设置图像或自定义颜色。 you can also customize these indicators using XML and you can find example from given link Android-ViewPagerIndicator's GitHub . 您还可以使用XML自定义这些指标,您可以从给定链接Android-ViewPagerIndicator的GitHub中找到示例。 If you want to use icon or image then you can use following class SampleIconsDefault.java . 如果要使用图标或图像,则可以使用以下类SampleIconsDefault.java from given sample 从给定的样本

I had the same problem and the only solution was to create my own library to do that. 我有同样的问题,唯一的解决方案是创建自己的库来做到这一点。

https://github.com/augustopicciani/DrawablePageIndicator https://github.com/augustopicciani/DrawablePageIndicator

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

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