youth5201314 / banner

🔥🔥🔥Banner 2.0 来了!Android广告图片轮播控件,内部基于ViewPager2实现,Indicator和UI都可以自定义。
Apache License 2.0
12.92k stars 2.51k forks source link

android 半折叠banner,调整item绘制层级,附demo图 #1132

Closed everyOneNo closed 10 months ago

everyOneNo commented 2 years ago

有更好的方法的话可以告诉我 }QPIYZN@~5 3M$A}W8BT{~L

mBanner1.getViewPager2().addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = -100;
                outRect.right = -100;
            }
        });
        try {
            Class<ViewPager2> viewPager2Class = ViewPager2.class;
            Field[] f = viewPager2Class.getDeclaredFields();
            for (Field item : f) {
                if (item.getName().equals("mRecyclerView")) {
                    item.setAccessible(true);
                    RecyclerView recyclerView = (RecyclerView) item.get(mBanner1.getViewPager2());
                    if (recyclerView != null) {
                        recyclerView.setChildDrawingOrderCallback(new RecyclerView.ChildDrawingOrderCallback() {
                            @Override
                            public int onGetChildDrawingOrder(int childCount, int i) {
                                int r;
                                if (1 == i) {
                                    r = 2;
                                } else if (2 == i) {
                                    r = 1;
                                } else {
                                    r = i;
                                }
                                if (r <= childCount - 1) {
                                    return r;
                                }
                                return i;
                            }
                        });
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }