Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make CBLoopViewPager public and add a RecyclerViewCornerRadius to mak… #298

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import android.widget.ListView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bigkoo.convenientbanner.view.CBLoopViewPager;
import com.bigkoo.convenientbanner.view.RecyclerViewCornerRadius;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,7 +53,7 @@ private void init(){
// initImageLoader();
loadTestDatas();
//本地图片例子
convenientBanner.setPages(
CBLoopViewPager cbLoopViewPager = convenientBanner.setPages(
new CBViewHolderCreator() {
@Override
public LocalImageHolderView createHolder(View itemView) {
Expand All @@ -65,7 +67,11 @@ public int getLayoutId() {
}, localImages)
//设置两个点图片作为翻页指示器,不设置则没有指示器,可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
// .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
.setOnItemClickListener(this);
.setOnItemClickListener(this)
.getViewPager();
RecyclerViewCornerRadius recyclerViewCornerRadius = new RecyclerViewCornerRadius(cbLoopViewPager);
recyclerViewCornerRadius.setCornerRadius(30);
cbLoopViewPager.addItemDecoration(recyclerViewCornerRadius);
//设置指示器的方向
// .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
// .setOnPageChangeListener(this)//监听翻页事件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class ConvenientBanner<T> extends RelativeLayout {
private AdSwitchTask adSwitchTask;
private boolean isVertical = false;

public CBLoopViewPager getViewPager() {
return viewPager;
}

public enum PageIndicatorAlign {
ALIGN_PARENT_LEFT, ALIGN_PARENT_RIGHT, CENTER_HORIZONTAL
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.bigkoo.convenientbanner.view;

import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.v7.widget.RecyclerView;
import android.view.ViewTreeObserver;

/**
* from https://github.com/wordplat/RecyclerViewCornerRadius
* Created by tanyi
* on 2018/9/6.
*/
public class RecyclerViewCornerRadius extends RecyclerView.ItemDecoration{
public static final String TAG = "RecyclerViewCornerRadius";

private RectF rectF;
private Path path;

private int topLeftRadius = 0;
private int topRightRadius = 0;
private int bottomLeftRadius = 0;
private int bottomRightRadius = 0;

public RecyclerViewCornerRadius(final RecyclerView recyclerView) {
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rectF = new RectF(0, 0, recyclerView.getMeasuredWidth(), recyclerView.getMeasuredHeight());

path = new Path();
path.reset();
path.addRoundRect(rectF, new float[]{
topLeftRadius, topLeftRadius,
topRightRadius, topRightRadius,
bottomLeftRadius, bottomLeftRadius,
bottomRightRadius, bottomRightRadius
}, Path.Direction.CCW);
recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}

public void setCornerRadius(int radius) {
this.topLeftRadius = radius;
this.topRightRadius = radius;
this.bottomLeftRadius = radius;
this.bottomRightRadius = radius;
}

public void setCornerRadius(int topLeftRadius, int topRightRadius, int bottomLeftRadius, int bottomRightRadius) {
this.topLeftRadius = topLeftRadius;
this.topRightRadius = topRightRadius;
this.bottomLeftRadius = bottomLeftRadius;
this.bottomRightRadius = bottomRightRadius;
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
c.clipRect(rectF);
c.clipPath(path, Region.Op.REPLACE);
}
}