第一步:重写一下ViewPager
package com.diction.app.android.view.indicator; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; /** * * 这个是自己定义的仿写的viewpger huguomin * * */ public class IndicatorPager extends ViewPager { private boolean scrollable = true; public IndicatorPager(Context context) { super(context); } public IndicatorPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent ev) { if(this.scrollable) { if (getCurrentItem() == 0 && getChildCount() == 0) { return false; } return super.onTouchEvent(ev); } else { return false; } } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(this.scrollable) { if (getCurrentItem() == 0 && getChildCount() == 0) { return false; } return super.onInterceptTouchEvent(ev); } else { return false; } } public void setScrollable(boolean scrollable) { this.scrollable = scrollable; } }
第二步 封装一下
package com.diction.app.android.view.indicator; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.diction.app.android.R; import com.diction.app.android.utils.ImageLoadUtils; import com.diction.app.android.utils.LogUtils; import com.diction.app.android.utils.ScreenUtils; import com.diction.app.android.utils.SizeUtils; import com.facebook.drawee.view.SimpleDraweeView; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; /** * Created by huguomin on 2018/7/24. */ public class CoustomIndicator extends FrameLayout implements ViewPager.OnPageChangeListener { public static final String TAG = "CoustomBanner---> "; private IndicatorPager mCoustomViewPager; private CoustomScroller mScroller; private int count; private List<String> imageViewsList = new ArrayList<>(); private List<String> descLists = new ArrayList<>(); private int currentItem; private BannerPagerAdapter adapter; private Context mContext; private RecyclerView mMRecycler; public CoustomIndicator(@NonNull Context context) { super(context); initView(context); } public CoustomIndicator(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context) { mContext = context; int margrleft = SizeUtils.px2dp((ScreenUtils.getScreenWidth()-dip2px(mContext,10))); LogUtils.e(TAG + " " + margrleft); LayoutInflater.from(context).inflate(R.layout.item_banner_layout, this, true); mCoustomViewPager = (IndicatorPager) findViewById(R.id.banner_view); mMRecycler = findViewById(R.id.sub_recycler); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mMRecycler.setLayoutManager(linearLayoutManager); initViewPagerScroll(); } private void initViewPagerScroll() { try { Field mField = ViewPager.class.getDeclaredField("mScroller"); mField.setAccessible(true); mScroller = new CoustomScroller(mCoustomViewPager.getContext()); mScroller.setDuration(800); mField.set(mCoustomViewPager, mScroller); } catch (Exception e) { // Log.e(tag, e.getMessage()); } } public void setImageListAndText(List<String> imagesUrl, List<String> descList) { if (imagesUrl == null || imagesUrl.size() <= 0) { Log.e(TAG, "The image data set is empty."); return; } count = imagesUrl.size(); for (int i = 0; i <= count + 1; i++) { String url = ""; String desc = ""; if (i == 0) { url = imagesUrl.get(count - 1); desc = descList.get(count - 1); } else if (i == count + 1) { url = imagesUrl.get(0); desc = descList.get(0); } else { url = imagesUrl.get(i - 1); desc = descList.get(i - 1); } imageViewsList.add(url); descLists.add(desc); } setData(); } private void setData(){ currentItem = 1; if (adapter == null) { adapter = new BannerPagerAdapter(); mCoustomViewPager.addOnPageChangeListener(this); } mCoustomViewPager.setPageMargin(dip2px(mContext, 10)); mCoustomViewPager.setOffscreenPageLimit(imageViewsList.size()); mCoustomViewPager.setAdapter(adapter); mCoustomViewPager.setPageTransformer(true,new ZoomOutSlideTransformer()); mCoustomViewPager.setFocusable(true); mCoustomViewPager.setCurrentItem(1); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { currentItem=position; if (mPageSelectedListener != null ){ mPageSelectedListener.onPageSelected(toRealPosition(position) ); } } public int toRealPosition(int position) { int realPosition = (position - 1) % count; if (realPosition < 0) realPosition += count; return realPosition; } @Override public void onPageScrollStateChanged(int state) { switch (state) { case 0://No operation if (currentItem == 0) { mCoustomViewPager.setCurrentItem(count, false); } else if (currentItem == count + 1) { mCoustomViewPager.setCurrentItem(1, false); } break; case 1://start Sliding if (currentItem == count + 1) { mCoustomViewPager.setCurrentItem(1, false); } else if (currentItem == 0) { mCoustomViewPager.setCurrentItem(count, false); } break; case 2://end Sliding break; } } class BannerPagerAdapter extends PagerAdapter { @Override public int getCount() { return imageViewsList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, final int position) { View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_gallery_layout, null); SimpleDraweeView image = inflate.findViewById(R.id.indicator_bg_image); TextView desc = inflate.findViewById(R.id.indicator_bg_text); RelativeLayout containerView = inflate.findViewById(R.id.contianer); ImageLoadUtils.loadImage(image,"http://epdapi2.diction.diexun.com"+imageViewsList.get(position)); desc.setText(descLists.get(position)); container.addView(inflate); return inflate; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } public void setCurrentPosition(int position) { Log.e(TAG, "setCurrentPosition: -----currentItem 》" + currentItem ); Log.e(TAG, "setCurrentPosition: -----currentItem 》" + currentItem ); if (position == toRealPosition(currentItem))return; mCoustomViewPager.setCurrentItem(position,false); } /** * dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { /* return (int) (dpValue * context.getResources().getDisplayMetrics().density + 0.5f);*/ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics()); } public interface OnPageSelectedListenter{ void onPageSelected(int position); } private OnPageSelectedListenter mPageSelectedListener; public void setOnPageSelectedListenter(OnPageSelectedListenter l){ mPageSelectedListener = l; } }
相关的类
import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class CoustomScroller extends Scroller { private int mDuration = 800; public CoustomScroller(Context context) { super(context); } public CoustomScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public CoustomScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, mDuration); } @Override public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, mDuration); } public void setDuration(int time) { mDuration = time; } }
相关的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:clipChildren="false" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.diction.app.android.view.indicator.IndicatorPager android:clipChildren="false" android:id="@+id/banner_view" android:layout_marginLeft="@dimen/x200" android:layout_marginRight="@dimen/x200" android:layout_width="match_parent" android:layout_height="70dp"/> <android.support.v7.widget.RecyclerView android:id="@+id/sub_recycler" android:layout_width="wrap_content" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
搞定!!!!!!