布局文件引用
Activity调用
ViewPager bannerViewPager = view.findViewById(R.id.bannerViewPager);
RecyclerView bannerRecycleView = view.findViewById(R.id.bannerRecycleView);
List bannerList = new ArrayList<>();
new BannerView().setBanner(getActivity(), bannerViewPager, bannerRecycleView, bannerList, new BannerView.OnClickListener() {
@Override
public void cickPostion(int position) {
}
});
Banner管理器
package com.xinchengle.xiaokedou.utils.uiutils;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.xinchengle.xiaokedou.R;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* Banner 轮播图
*/
public class BannerView {
public interface OnClickListener {
void cickPostion(int position);
}
private Context context;
private List list;
private OnClickListener onClickListener;
private ViewPager viewPager;
private final int BANNER_NEXT = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case BANNER_NEXT:
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem + 1);
sendMsg();
break;
}
}
};
public void setBanner(Context context, ViewPager vp, RecyclerView recyclerView, List list, BannerView.OnClickListener onClickListener) {
this.context = context;
this.list = list;
this.onClickListener = onClickListener;
this.viewPager = vp;
if (list.size() == 0) {
return;
}
//设置展示View
recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
MyDotAdapter myDotAdaoter = new MyDotAdapter();
recyclerView.setAdapter(myDotAdaoter);
ViewPagerScroller pagerScroller = new ViewPagerScroller(context);
pagerScroller.initViewPagerScroll(viewPager);
MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
//让viewpager的索引默认的指在200
viewPager.setCurrentItem(200);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
myDotAdaoter.setCheckPostion(position);
myDotAdaoter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//及时取消viewPager切换事件 防止OOM
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //按下
handler.removeMessages(BANNER_NEXT);
case MotionEvent.ACTION_MOVE: //移动
handler.removeMessages(BANNER_NEXT);
case MotionEvent.ACTION_UP: //抬起
handler.removeMessages(BANNER_NEXT);
handler.sendEmptyMessageDelayed(BANNER_NEXT, 3000);
}
return false;
}
});
}
//发送轮播请求
private void sendMsg() {
handler.sendEmptyMessageDelayed(BANNER_NEXT, 3000);
}
//ViewPager适配器
private class MyViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View inflate = View.inflate(context, R.layout.item_banner_vp_view, null);
RoundImageView imageView = inflate.findViewById(R.id.roundImage);
if (!isDestroy((Activity) context)) {
Glide.with(context).load(list.get(position % list.size())).into(imageView);
}
container.addView(inflate);
inflate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onClickListener!=null){
onClickListener.cickPostion(position % list.size());
}
}
});
return inflate;
}
}
/**
* 判断Activity是否Destroy
*/
private static boolean isDestroy(Activity mActivity) {
if (mActivity == null || mActivity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
return true;
} else {
return false;
}
}
//小圆点适配器
private class MyDotAdapter extends RecyclerView.Adapter {
private int checkPostion;
public void setCheckPostion(int checkPostion) {
this.checkPostion = checkPostion;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = View.inflate(context, R.layout.item_banner_recycle_view, null);
return new ViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (position == (checkPostion % list.size())) {
holder.bannerDot.setBackgroundResource(R.drawable.shape_banner_dot_check);
} else {
holder.bannerDot.setBackgroundResource(R.drawable.shape_banner_dot_false);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final LinearLayout bannerDot;
public ViewHolder(@NonNull View itemView) {
super(itemView);
bannerDot = itemView.findViewById(R.id.bannerDot);
}
}
}
//配置滑动速度
private class ViewPagerScroller extends Scroller {
private int mScrollDuration = 800;
public ViewPagerScroller(Context context) {
super(context);
}
public ViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
}
public void initViewPagerScroll(ViewPager viewPager) {
try {
Field mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
mScroller.set(viewPager, this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
小圆点选择shape
小圆点未选择shape
ViewPager引用布局(其实就是引用个ImageView,我这是圆角)