How to make a page indicator for horizontal recyclerview

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

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

10条回答
  •  爱一瞬间的悲伤
    2021-01-30 06:01

    First you have to create another RecyclerView for the circles and put this code in the OnScrollListener of the first RecyclerView.

    int[] firstVisibleItemPositions = new int[appList.size()];
    int[] lastVisibleItemPositions = new int[appList.size()];
    
    int position = ((StaggeredGridLayoutManager) listView.getLayoutManager()).findFirstVisibleItemPositions(firstVisibleItemPositions)[0];
    View currentView = circlesList.getChildAt(position);
    int lastPosition = ((StaggeredGridLayoutManager) listView.getLayoutManager()).findLastVisibleItemPositions(lastVisibleItemPositions)[0];
    View lastView = circlesList.getChildAt(lastPosition);
    ImageView circle;
    if (dx>0) {
        if (currentView != null && lastPosition != position) {
            circle = (ImageView) currentView.findViewById(R.id.img);
            circle.setImageResource(R.drawable.empty_circle);
        }
        if (lastView != null && lastPosition != position) {
            circle = (ImageView) lastView.findViewById(R.id.img);
            circle.setImageResource(R.drawable.selected_circle);
        }
    }else if (dx<0){
        if (currentView != null && lastPosition != position) {
            circle = (ImageView) currentView.findViewById(R.id.img);
            circle.setImageResource(R.drawable.selected_circle);
        }
        if (lastView != null && lastPosition != position) {
            circle = (ImageView) lastView.findViewById(R.id.img);
            circle.setImageResource(R.drawable.empty_circle);
        }
    }
    

提交回复
热议问题