How to make a page indicator for horizontal recyclerview

前端 未结 10 1146
说谎
说谎 2021-01-30 05:32

Any idea how to create a page indicator for recyclerview list ?

10条回答
  •  长发绾君心
    2021-01-30 05:47

    You can add PageIndicator.java class to your .xml below recycler view widget

    
    
        
    
        
    
    
    

    The PageIndicator.java class like below

    public class PageIndicator extends LinearLayout {
      private ImageView[] imageIndications;
    
      public PageIndicator(Context context) {
        super(context);
      }
    
      public PageIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      /**
       * method to create the pageIndicator
       */
      public void createPageIndicator(int pageCount, int focusedPageDrawable,
          int unFocusedPageDrawable) {
        imageIndications = new ImageView[pageCount];
        ImageView indicatorImageView;
        for (int i = 0; i < pageCount; i++) {
          indicatorImageView = new ImageView(getContext());
          int size = BaseUtils.INSTANCE.getDensityPixelValue(getContext(), 8);
          final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size, size);
          params.setMargins(8, 0, 4, 0);
          indicatorImageView.setLayoutParams(params);
          // method to change the page icon
          changePageIcon(i, 0, indicatorImageView, focusedPageDrawable, unFocusedPageDrawable);
          imageIndications[i] = indicatorImageView;
          this.addView(indicatorImageView);
        }
      }
    
      /**
       * method to handle the PageChangeListener for ViewPager
       *
       * @param size the total number of images available for product
       * @param position the current position of ViewPager
       * @param focusedPageDrawable
       * @param unFocusedPageDrawable
       */
      public void handleViewPagerScroll(int size, int position, int focusedPageDrawable,
          int unFocusedPageDrawable) {
        for (int i = 0; i < size && i < imageIndications.length; i++) {
          changePageIcon(position, i, imageIndications[i], focusedPageDrawable, unFocusedPageDrawable);
          imageIndications[i].getLayoutParams().width = imageIndications[i].getDrawable().getIntrinsicWidth();
        }
      }
    
      /**
       * method to change the page icon
       *
       * @param position
       * @param indicatorImageView
       * @param focusedPageDrawable
       * @param unFocusedPageDrawable
       */
      private void changePageIcon(int position, int pageIndex, ImageView indicatorImageView,
          int focusedPageDrawable, int unFocusedPageDrawable) {
        if (pageIndex == position) {
          if (focusedPageDrawable != 0) {
            indicatorImageView.setImageResource(focusedPageDrawable);
          } else {
            indicatorImageView.setImageResource(R.drawable.rounded_style_blue);
          }
        } else {
          if (unFocusedPageDrawable != 0) {
            indicatorImageView.setImageResource(unFocusedPageDrawable);
          } else {
            indicatorImageView.setImageResource(R.drawable.rounded_style2);
          }
        }
      }
    }
    

    In your recycler view adapter, you can add override below with interface

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager
        if (manager is LinearLayoutManager && itemCount > 0) {
          recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
              super.onScrolled(recyclerView, dx, dy)
              val visiblePosition: Int = manager.findFirstCompletelyVisibleItemPosition()
              if (visiblePosition > -1) {
                iFragmentCommunicator.updateCount(visiblePosition)
              }
            }
          })
        }
      }
    
      interface IFragmentCommunicator {
        fun updateCount(count: Int)
      }
    

    and last in your Activity or Fragment you can add below code initially to call Page Indicator method.

    private fun initCircularIndicator() {
        val margin =
          (screenWidth - (0.8F * screenWidth).toInt()) / 2 - 8)
        (mBinder?.llImageIndicator?.layoutParams as? FrameLayout.LayoutParams)?.apply {
          setMargins(margin, 0, 0, 32))
        }
        mBinder?.llImageIndicator?.requestLayout()
        mBinder?.llImageIndicator?.run {
          removeAllViews()
          createPageIndicator(
            8,
            R.drawable.selected_item_indicator,
            0
          )
          handleViewPagerScroll(8, 0, R.drawable.selected_blue_item_indicator, 0)
        }
      }
    

    In above code you can add your drawable for selected_blue_item_indicator like below

    
    
    
        
    
        
    
        
    
    

    and once you override the updateCount() method in Activity or Fragment call handleViewPagerScroll() method of Page Indicator

    override fun updateCount(count: Int) {
        mBinder?.llImageIndicator?.handleViewPagerScroll(
          8,
          count,
          R.drawable.selected_blue_item_indicator,
          0
        )
      }
    

    that's all you have to do.

提交回复
热议问题