Skip to content

Commit

Permalink
【2017.7.7】重要更新:增加按住Banner 停止轮播,松开开始自动轮播的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei committed Jul 7, 2017
1 parent ebe7f89 commit 9fa5660
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
//compile project(':mzbanner')
compile 'com.github.pinguo-zhouwei:MZBannerView:v1.1.0'
compile project(':mzbanner')
//compile 'com.github.pinguo-zhouwei:MZBannerView:v1.1.0'

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BannerViewHolder createViewHolder() {
});

mNormalBanner = (MZBannerView) view.findViewById(R.id.banner_normal);

mNormalBanner.setIndicatorRes(R.color.colorAccent,R.color.colorPrimary);
mNormalBanner.setPages(list, new MZHolderCreator<BannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
Expand Down
39 changes: 37 additions & 2 deletions mzbanner/src/main/java/com/zhouwei/mzbanner/MZBannerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import android.os.Build;
import android.os.Handler;
import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
Expand All @@ -36,6 +39,7 @@
*/

public class MZBannerView<T> extends RelativeLayout {
private static final String TAG = "MZBannerView";
private ViewPager mViewPager;
private MZPagerAdapter mAdapter;
private List<T> mDatas;
Expand Down Expand Up @@ -214,6 +218,37 @@ private void initIndicator(){
}
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(!mIsCanLoop){
return super.dispatchTouchEvent(ev);
}
switch (ev.getAction()){
// 按住Banner的时候,停止自动轮播
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_DOWN:
int paddingLeft = mViewPager.getLeft();
float touchX = ev.getRawX();
// 如果是魅族模式,去除两边的区域
if(touchX >= paddingLeft && touchX < getScreenWidth(getContext()) - paddingLeft){
mIsAutoPlay = false;
}
break;
case MotionEvent.ACTION_UP:
mIsAutoPlay = true;
break;
}
return super.dispatchTouchEvent(ev);
}

public static int getScreenWidth(Context context){
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
int width = dm.widthPixels;
return width;
}

/******************************************************************************************************/
/** 对外API **/
Expand Down Expand Up @@ -286,7 +321,7 @@ public ViewPager getViewPager() {
* @param unSelectRes 未选中状态资源图片
* @param selectRes 选中状态资源图片
*/
public void setIndicatorRes(int unSelectRes,int selectRes){
public void setIndicatorRes(@DrawableRes int unSelectRes, @DrawableRes int selectRes){
mIndicatorRes[0]= unSelectRes;
mIndicatorRes[1] = selectRes;
}
Expand Down Expand Up @@ -540,7 +575,7 @@ private View getView(int position,ViewGroup container){
}

// 添加点击事件
view.setOnClickListener(new OnClickListener() {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mPageClickListener!=null){
Expand Down

0 comments on commit 9fa5660

Please sign in to comment.