RecyclerBanner
RecyclerBanner copied to clipboard
Image Selected Not Rounded
if you can see image selected not rounded.
this is my code : xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="250dp" android:layout_height="250dp" android:background="@color/bgwhite" app:cardCornerRadius="10dp"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> </android.support.v7.widget.CardView>
this is java code:
`private BannerLayout bannerLayout; private HomeBannerAdapter homeBannerAdapter;
bannerLayout = (BannerLayout)v.findViewById(R.id.recycler);
List<Integer>list = new ArrayList<>();
list.add(R.mipmap.pic1);
list.add(R.mipmap.pic2);
list.add(R.mipmap.pic3);
list.add(R.mipmap.pic4);
homeBannerAdapter = new HomeBannerAdapter(getActivity(), list);
homeBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
bannerLayout.setShowIndicator(false);
bannerLayout.setAdapter(homeBannerAdapter);`
my adapter :
`public class HomeBannerAdapter extends RecyclerView.Adapter<HomeBannerAdapter.MzViewHolder> {
private Context context;
private List<Integer> urlList;
private BannerLayout.OnBannerItemClickListener onBannerItemClickListener;
public HomeBannerAdapter(Context context, List<Integer> urlList) {
this.context = context;
this.urlList = urlList;
}
public void setOnBannerItemClickListener(BannerLayout.OnBannerItemClickListener onBannerItemClickListener) {
this.onBannerItemClickListener = onBannerItemClickListener;
}
@Override
public HomeBannerAdapter.MzViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MzViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_image, parent, false));
}
@Override
public void onBindViewHolder(HomeBannerAdapter.MzViewHolder holder, final int position) {
if (urlList == null || urlList.isEmpty())
return;
final int P = position % urlList.size();
//String url = urlList.get(P);
ImageView img = (ImageView) holder.imageView;
Picasso.get().load(urlList.get(P)).into(img);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onBannerItemClickListener != null) {
onBannerItemClickListener.onItemClick(P);
}
}
});
}
@Override
public int getItemCount() {
if (urlList != null) {
return urlList.size();
}
return 0;
}
class MzViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
MzViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image);
}
}
}`
this is bannerlayout code:
`public class BannerLayout extends FrameLayout {
private int autoPlayDuration; // refresh interval
private boolean showIndicator; // Whether indicator is displayed
private RecyclerView indicatorContainer;
private Drawable mSelectedDrawable;
private Drawable mUnselectedDrawable;
private IndicatorAdapter indicatorAdapter;
private int indicatorMargin; // indicator spacing
private RecyclerView mRecyclerView;
private BannerLayoutManager mLayoutManager;
private int WHAT_AUTO_PLAY = 1000;
private boolean hasInit;
private int bannerSize = 1;
private int currentIndex;
private boolean isPlaying = false;
private boolean isAutoPlaying = true;
int itemSpace;
float centerScale;
float moveSpeed;
protected Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == WHAT_AUTO_PLAY) {
if (currentIndex == mLayoutManager.getCurrentPosition()) {
++currentIndex;
mRecyclerView.smoothScrollToPosition(currentIndex);
mHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration);
refreshIndicator();
}
}
return false;
}
});
public BannerLayout(Context context) {
this(context, null);
}
public BannerLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BannerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
protected void initView(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BannerLayout);
showIndicator = a.getBoolean(R.styleable.BannerLayout_showIndicator, true);
autoPlayDuration = a . getInt ( R . styleable . BannerLayout_interval , 4000 );
isAutoPlaying = a.getBoolean(R.styleable.BannerLayout_autoPlaying, true);
itemSpace = a.getInt(R.styleable.BannerLayout_itemSpace, 20);
centerScale = a.getFloat(R.styleable.BannerLayout_centerScale, 1.2f);
moveSpeed = a.getFloat(R.styleable.BannerLayout_moveSpeed, 1.0f);
if (mSelectedDrawable == null) {
// draw the default selected state graphic
GradientDrawable selectedGradientDrawable = new GradientDrawable();
selectedGradientDrawable.setShape(GradientDrawable.OVAL);
selectedGradientDrawable.setColor(Color.RED);
selectedGradientDrawable.setSize(dp2px(5), dp2px(5));
selectedGradientDrawable.setCornerRadius(dp2px(5) / 2);
mSelectedDrawable = new LayerDrawable(new Drawable[]{selectedGradientDrawable});
}
if (mUnselectedDrawable == null) {
// draw the default unselected state graphics
GradientDrawable unSelectedGradientDrawable = new GradientDrawable();
unSelectedGradientDrawable.setShape(GradientDrawable.OVAL);
unSelectedGradientDrawable.setColor(Color.GRAY);
unSelectedGradientDrawable.setSize(dp2px(5), dp2px(5));
unSelectedGradientDrawable.setCornerRadius(dp2px(5) / 2);
mUnselectedDrawable = new LayerDrawable(new Drawable[]{unSelectedGradientDrawable});
}
indicatorMargin = dp2px(4);
int marginLeft = dp2px(16);
int marginRight = dp2px(0);
int marginBottom = dp2px(11);
int gravity = GravityCompat.START;
int o = a.getInt(R.styleable.BannerLayout_orientation, 0);
int orientation = 0;
if (o == 0) {
orientation = OrientationHelper.HORIZONTAL;
} else if (o == 1 ) {
orientation = OrientationHelper.VERTICAL;
}
a.recycle();
// Carousel section
mRecyclerView = new RecyclerView(context);
LayoutParams vpLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
addView(mRecyclerView, vpLayoutParams);
mLayoutManager = new BannerLayoutManager(getContext(), orientation);
mLayoutManager.setItemSpace(itemSpace);
mLayoutManager.setCenterScale(centerScale);
mLayoutManager.setMoveSpeed(moveSpeed);
mRecyclerView . setLayoutManager (mLayoutManager);
new CenterSnapHelper().attachToRecyclerView(mRecyclerView);
// indicator section
indicatorContainer = new RecyclerView(context);
LinearLayoutManager indicatorLayoutManager = new LinearLayoutManager(context, orientation, false);
indicatorContainer . setLayoutManager (indicatorLayoutManager);
indicatorAdapter = new IndicatorAdapter();
indicatorContainer . setAdapter (indicatorAdapter);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | gravity;
params.setMargins(marginLeft, 0, marginRight, marginBottom);
addView(indicatorContainer, params);
if (!showIndicator) {
indicatorContainer.setVisibility(GONE);
}
}
// Set whether to prohibit scrolling
public void setAutoPlaying(boolean isAutoPlaying) {
this.isAutoPlaying = isAutoPlaying;
setPlaying(this.isAutoPlaying);
}
public boolean isPlaying() {
return isPlaying;
}
// Set whether to display the indicator
public void setShowIndicator(boolean showIndicator) {
this.showIndicator = showIndicator;
indicatorContainer.setVisibility(showIndicator ? VISIBLE : GONE);
}
// Set the current image scaling factor
public void setCenterScale(float centerScale) {
this.centerScale = centerScale;
mLayoutManager.setCenterScale(centerScale);
}
// Set the speed of following the finger
public void setMoveSpeed(float moveSpeed) {
this.moveSpeed = moveSpeed;
mLayoutManager.setMoveSpeed(moveSpeed);
}
// Set the picture spacing
public void setItemSpace ( int itemSpace ) {
this.itemSpace = itemSpace;
mLayoutManager.setItemSpace(itemSpace);
}
/**
* Set the rotation interval
*
* @param autoPlayDuration time milliseconds
*/
public void setAutoPlayDuration ( int autoPlayDuration ) {
this.autoPlayDuration = autoPlayDuration;
}
public void setOrientation(int orientation) {
mLayoutManager.setOrientation(orientation);
}
/**
* Set whether to play automatically (locked)
*
* @param playing Start playing
*/
protected synchronized void setPlaying(boolean playing) {
if (isAutoPlaying && hasInit) {
if (!isPlaying && playing) {
mHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration);
isPlaying = true;
} else if (isPlaying && !playing) {
mHandler.removeMessages(WHAT_AUTO_PLAY);
isPlaying = false;
}
}
}
/**
* Set carousel data set
*/
public void setAdapter(RecyclerView.Adapter adapter) {
hasInit = false;
mRecyclerView.setAdapter(adapter);
bannerSize = adapter . getItemCount ();
mLayoutManager.setInfinite(bannerSize >= 3);
setPlaying(true);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dx != 0) {
setPlaying(false);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
int first = mLayoutManager.getCurrentPosition();
Log.d("xxx", "onScrollStateChanged");
if (currentIndex != first) {
currentIndex = first;
}
if (newState == SCROLL_STATE_IDLE) {
setPlaying(true);
}
refreshIndicator();
}
});
hasInit = true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
setPlaying(false);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setPlaying(true);
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setPlaying(true);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
setPlaying(false);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
if (visibility == VISIBLE) {
setPlaying(true);
} else {
setPlaying(false);
}
}
/**
* Marking point adapter
*/
protected class IndicatorAdapter extends RecyclerView.Adapter {
int currentPosition = 0;
public void setPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ImageView bannerPoint = new ImageView(getContext());
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(indicatorMargin, indicatorMargin, indicatorMargin, indicatorMargin);
bannerPoint . setLayoutParams (lp);
return new RecyclerView.ViewHolder(bannerPoint) {
};
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ImageView bannerPoint = (ImageView) holder.itemView;
bannerPoint.setImageDrawable(currentPosition == position ? mSelectedDrawable : mUnselectedDrawable);
}
@Override
public int getItemCount() {
return bannerSize;
}
}
protected int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
Resources.getSystem().getDisplayMetrics());
}
/**
* Change instruction points for navigation
*/
protected synchronized void refreshIndicator() {
if (showIndicator && bannerSize > 1) {
indicatorAdapter.setPosition(currentIndex % bannerSize);
indicatorAdapter.notifyDataSetChanged();
}
}
public interface OnBannerItemClickListener {
void onItemClick ( int position );
}
} `