4

如何將圖像添加到圓形頁面指示符,而不是添加顏色..我試圖添加iconpage指示器,但點擊方法不起作用,但我可以添加圖像.. 在circlepageIndicator onclick方法正在工作,但如何要添加可拖動圖像到圓形指示器。下面是我的代碼。向CirclePageIndicator添加繪圖

enter image description here

二手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(); 
     } 
    }); 

二手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(); 

     } 

    }); 

回答

0

您的使用CirclePageIndicator對象來定製頁面指示符。您可以設置圖像或自定義顏色。 您還可以使用XML自定義這些指標,並可以從給定鏈接Android-ViewPagerIndicator's GitHub中找到示例。 如果你想使用圖標或圖像,那麼你可以使用以下類SampleIconsDefault.java。從給定的樣本