RecyclerView는 ViewPager 효과를 구현합니다.

3309 단어 Android
코드를 보면 됩니다. RecyclerView는 Viewpager의 효과를 실현하고 인터페이스의 변경 감청을 추가했습니다. 일반적인 RecyclerView와 마찬가지로 한 번에 여러 인터페이스를 미끄러뜨리는 것도 설정할 수 있습니다.
public class VpRecyView extends RecyclerView{

    public VpRecyView(Context context) {
        super(context);
        init();
    }

    public VpRecyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private int position = 0;

    private void init() {
        LinearLayoutManager llm = new LinearLayoutManager(this.getContext());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        this.setLayoutManager(llm);
        SnapHelper snapHelper = new PagerSnapHelper();
//      SnapHelper snapHelper = new LinearSnapHelper(); //       
        snapHelper.attachToRecyclerView(this);//    RecyclerView
        this.addItemDecoration(new DividerItemDecoration(this.getContext(), DividerItemDecoration.HORIZONTAL));
        this.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager){
                    int firs = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                    if (position != firs){
                        position = firs;
                        if (onpagerChageListener != null)
                        onpagerChageListener.onPagerChage(position);
                    }
                }
            }
        });
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        this.requestDisallowInterceptTouchEvent(true); //         
        return super.dispatchTouchEvent(ev);
    }

    public void setOnPagerPosition(int position){
//        this.position = position;
        RecyclerView.LayoutManager layoutManager = this.getLayoutManager();
        layoutManager.scrollToPosition(position);
    }

    public int getOnPagerPosition(){
        RecyclerView.LayoutManager layoutManager = this.getLayoutManager();
        return ((LinearLayoutManager)layoutManager).findFirstVisibleItemPosition();
    }


    interface onPagerChageListener{
        void onPagerChage(int position);
    }

    private onPagerChageListener onpagerChageListener;
    public void setOnPagerChageListener(onPagerChageListener onpagerChageListener){
       this.onpagerChageListener = onpagerChageListener;
    }
    
}

사용:
final List datas1 = Arrays.asList(R.color.color8, R.color.color2, R.color.color3, R.color.color4, R.color.color5, R.color.color6, R.color.color7);
                VpRecyView rv = v.findViewById(R.id.rv);
                Ap ap = new Ap(datas1);
                rv.setAdapter(ap);
                rv.setOnPagerChageListener(new VpRecyView.onPagerChageListener() {
                    @Override
                    public void onPagerChage(int position) {
                        Log.i(TAG, "onPagerChage: "+position);
                    }
                });
                rv.setOnPagerPosition(datas1.size()-1);

좋은 웹페이지 즐겨찾기