From cceec3fb5361440b67fd55b99a9c863fb7e76907 Mon Sep 17 00:00:00 2001 From: Ztiany Date: Fri, 18 Oct 2019 17:49:25 +0800 Subject: [PATCH] optimize BannerView --- .../app/activity/ActivityDelegateOwner.java | 2 +- .../android/base/app/activity/BaseActivity.kt | 7 +-- .../base/interfaces/OnItemClickListener.java | 2 +- .../interfaces/OnItemLongClickListener.java | 2 +- .../com/android/base/utils/common/Strings.kt | 6 +-- .../widget/viewpager/BannerPagerAdapter.java | 50 +++++++++---------- .../widget/viewpager/BannerViewPager.java | 37 +++++++------- .../viewpager/ScalableBannerPagerFactory.java | 32 ------------ lib_base/src/main/res/values/base_attrs.xml | 2 - .../sdk/mediaselector/MediaSelector.java | 11 ++-- 10 files changed, 57 insertions(+), 94 deletions(-) delete mode 100644 lib_base/src/main/java/com/android/base/widget/viewpager/ScalableBannerPagerFactory.java diff --git a/lib_base/src/main/java/com/android/base/app/activity/ActivityDelegateOwner.java b/lib_base/src/main/java/com/android/base/app/activity/ActivityDelegateOwner.java index 622b3ef..1d708c2 100644 --- a/lib_base/src/main/java/com/android/base/app/activity/ActivityDelegateOwner.java +++ b/lib_base/src/main/java/com/android/base/app/activity/ActivityDelegateOwner.java @@ -17,4 +17,4 @@ public interface ActivityDelegateOwner { ActivityState getStatus(); -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.kt b/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.kt index 328820e..0e4686b 100644 --- a/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.kt +++ b/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.kt @@ -131,16 +131,17 @@ abstract class BaseActivity : AppCompatActivity(), ActivityDelegateOwner, AutoDi // interface impl /////////////////////////////////////////////////////////////////////////// @UiThread - override fun addDelegate(activityDelegate: ActivityDelegate<*>) { + final override fun addDelegate(activityDelegate: ActivityDelegate<*>) { activityDelegates.addActivityDelegate(activityDelegate) } @UiThread - override fun removeDelegate(activityDelegate: ActivityDelegate<*>): Boolean { + final override fun removeDelegate(activityDelegate: ActivityDelegate<*>): Boolean { return activityDelegates.removeActivityDelegate(activityDelegate) } - override fun findDelegate(predicate: Predicate?>?): ActivityDelegate<*>? { + @UiThread + final override fun findDelegate(predicate: Predicate?>?): ActivityDelegate<*>? { return activityDelegates.findDelegate(predicate) } diff --git a/lib_base/src/main/java/com/android/base/interfaces/OnItemClickListener.java b/lib_base/src/main/java/com/android/base/interfaces/OnItemClickListener.java index 1efc6f2..6ca9518 100644 --- a/lib_base/src/main/java/com/android/base/interfaces/OnItemClickListener.java +++ b/lib_base/src/main/java/com/android/base/interfaces/OnItemClickListener.java @@ -17,6 +17,6 @@ public abstract class OnItemClickListener implements View.OnClickListener { onClick(v, (T) tag); } - public abstract void onClick(@NonNull View view, @NonNull T t); + public abstract void onClick(@NonNull View view, @NonNull T item); } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/interfaces/OnItemLongClickListener.java b/lib_base/src/main/java/com/android/base/interfaces/OnItemLongClickListener.java index bc9bfea..18c7d57 100644 --- a/lib_base/src/main/java/com/android/base/interfaces/OnItemLongClickListener.java +++ b/lib_base/src/main/java/com/android/base/interfaces/OnItemLongClickListener.java @@ -15,6 +15,6 @@ public abstract class OnItemLongClickListener implements View.OnLongClickList return onClick(v, (T) tag); } - public abstract boolean onClick(View view, T t); + public abstract boolean onClick(View view, T item); } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/utils/common/Strings.kt b/lib_base/src/main/java/com/android/base/utils/common/Strings.kt index 924a39e..bf5ca5f 100644 --- a/lib_base/src/main/java/com/android/base/utils/common/Strings.kt +++ b/lib_base/src/main/java/com/android/base/utils/common/Strings.kt @@ -19,15 +19,13 @@ private const val CONTAINS_UPPERCASE_LETTERS_REG = "^.*[A-Z]+.*$" /** * 验证中国的手机号 - * - * @return 验证成功返回 true,验证失败返回 false */ fun isChinaPhoneNumber(mobile: String?): Boolean { return !isEmpty(mobile) && Pattern.matches(CHINA_PHONE_REG, mobile) } /** - * 只包含 [1-9][a-z][A-Z] + * 是否只包含 [1-9][a-z][A-Z] */ fun containsDigitalLetterOnly(text: String?): Boolean { return !isEmpty(text) && Pattern.matches(DIGITAL_LETTER_ONLY_REG, text) @@ -90,7 +88,7 @@ fun isDecimals(decimals: String?): Boolean { /** * 是否为纯中文 */ -fun isChinese(chinese: String?): Boolean { +fun isJustChineseCharacter(chinese: String?): Boolean { return !isEmpty(chinese) && Pattern.matches(CHINESE_REGEX, chinese) } diff --git a/lib_base/src/main/java/com/android/base/widget/viewpager/BannerPagerAdapter.java b/lib_base/src/main/java/com/android/base/widget/viewpager/BannerPagerAdapter.java index 7f62763..72acb19 100644 --- a/lib_base/src/main/java/com/android/base/widget/viewpager/BannerPagerAdapter.java +++ b/lib_base/src/main/java/com/android/base/widget/viewpager/BannerPagerAdapter.java @@ -14,21 +14,24 @@ import androidx.annotation.NonNull; import androidx.core.view.ViewCompat; import androidx.viewpager.widget.PagerAdapter; +public abstract class BannerPagerAdapter extends PagerAdapter { -class BannerPagerAdapter extends PagerAdapter { - - static ScalableBannerPagerFactory sBannerPagerFactory; - - private final String mTransitionName; - private final Context mContext; - private final List mEntities; + private String mTransitionName; + private Context mContext; + private List mEntities; private OnPageClickListener mClickListener; private boolean mIsLooper; - BannerPagerAdapter(Context context, List entities, String transitionName) { - mContext = context; - this.mEntities = entities; + void setTransitionName(String transitionName) { mTransitionName = transitionName; + } + + void setContext(Context context) { + mContext = context; + } + + void setEntities(List entities) { + mEntities = entities; mIsLooper = mEntities.size() > 1; } @@ -49,25 +52,23 @@ class BannerPagerAdapter extends PagerAdapter { @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, final int position) { - if (sBannerPagerFactory == null) { - throw new NullPointerException("You need to provide a ScalableBannerPagerFactory!"); - } - - ImageView imageView = sBannerPagerFactory.createBannerPagerView(mContext); + ImageView imageView = createBannerPagerView(container, mContext, position); setTransitionName(imageView); - - sBannerPagerFactory.setOnClickListener(imageView, v -> { - if (mClickListener != null) { - mClickListener.onClick(imageView, mIsLooper ? position - 1 : position); - } - }); - + callImageClicked(position, imageView); String url = mEntities.get(position); ImageLoaderFactory.getImageLoader().display(imageView, url); container.addView(imageView, 0); return imageView; } + protected final void callImageClicked(int position, ImageView imageView) { + if (mClickListener != null) { + mClickListener.onClick(imageView, mIsLooper ? position - 1 : position); + } + } + + protected abstract ImageView createBannerPagerView(@NonNull ViewGroup container, @NonNull Context context, int position); + private void setTransitionName(ImageView bannerLayout) { if (!TextUtils.isEmpty(mTransitionName)) { ViewCompat.setTransitionName(bannerLayout, mTransitionName); @@ -84,9 +85,4 @@ class BannerPagerAdapter extends PagerAdapter { container.removeView((View) object); } - @Override - public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) { - super.setPrimaryItem(container, position, object); - } - } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/widget/viewpager/BannerViewPager.java b/lib_base/src/main/java/com/android/base/widget/viewpager/BannerViewPager.java index fa7e63a..6f376e6 100644 --- a/lib_base/src/main/java/com/android/base/widget/viewpager/BannerViewPager.java +++ b/lib_base/src/main/java/com/android/base/widget/viewpager/BannerViewPager.java @@ -11,7 +11,8 @@ import com.android.base.R; import java.util.ArrayList; import java.util.List; -import androidx.viewpager.widget.PagerAdapter; +import javax.annotation.Nullable; + import androidx.viewpager.widget.ViewPager; /** @@ -28,7 +29,6 @@ public class BannerViewPager extends FrameLayout { private OnBannerPositionChangedListener mOnBannerPositionChangedListener; - private boolean mScalable; private String mTransitionName; private OnPageClickListener mOnPageClickListener; @@ -45,8 +45,6 @@ public class BannerViewPager extends FrameLayout { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerViewPager); - /*是否可以缩放*/ - mScalable = typedArray.getBoolean(R.styleable.BannerViewPager_zvp_scale_enable, false); /*用于支持5.0的transition动画*/ mTransitionName = typedArray.getString(R.styleable.BannerViewPager_zvp_item_transition_name); int pageId = typedArray.getResourceId(R.styleable.BannerViewPager_zvp_pager_number_id, -1); @@ -74,7 +72,7 @@ public class BannerViewPager extends FrameLayout { mPageNumberView = pageNumberView; } - public void setImages(List entities) { + public void setImages(List entities, @Nullable BannerPagerAdapter adapter) { if (entities == null || entities.isEmpty()) { mImageUrlList.clear(); mViewPager.setAdapter(null); @@ -83,16 +81,21 @@ public class BannerViewPager extends FrameLayout { } mImageUrlList.clear(); setPageSize(entities.size()); + if (entities.size() > 1) { addExtraPage(entities); - showBanner(); + showBanner(adapter); setLooper(); } else { mImageUrlList.addAll(entities); - showBanner(); + showBanner(adapter); } } + public void setImages(List entities) { + setImages(entities, null); + } + private void setPageSize(int pageSize) { if (mPageNumberView != null) { mPageNumberView.setPageSize(pageSize); @@ -169,18 +172,18 @@ public class BannerViewPager extends FrameLayout { mImageUrlList.add(entities.get(0)); } - private void showBanner() { - PagerAdapter adapter; - if (mScalable) { - BannerPagerAdapter bannerPagerAdapter = new BannerPagerAdapter(getContext(), mImageUrlList, mTransitionName); - bannerPagerAdapter.setOnBannerClickListener(mOnPageClickListener); - adapter = bannerPagerAdapter; + private void showBanner(BannerPagerAdapter adapter) { + if (adapter != null) { + adapter.setContext(getContext()); + adapter.setEntities(mImageUrlList); + adapter.setTransitionName(mTransitionName); + adapter.setOnBannerClickListener(mOnPageClickListener); + mViewPager.setAdapter(adapter); } else { OptimizeBannerPagerAdapter optimizeBannerPagerAdapter = new OptimizeBannerPagerAdapter(getContext(), mImageUrlList, mTransitionName); optimizeBannerPagerAdapter.setOnBannerClickListener(mOnPageClickListener); - adapter = optimizeBannerPagerAdapter; + mViewPager.setAdapter(optimizeBannerPagerAdapter); } - mViewPager.setAdapter(adapter); } public void setOnPageClickListener(OnPageClickListener onPageClickListener) { @@ -199,8 +202,4 @@ public class BannerViewPager extends FrameLayout { void onPagePositionChanged(int position); } - public static void setScalableBannerPagerFactory(ScalableBannerPagerFactory scalableBannerPagerFactory) { - BannerPagerAdapter.sBannerPagerFactory = scalableBannerPagerFactory; - } - } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/widget/viewpager/ScalableBannerPagerFactory.java b/lib_base/src/main/java/com/android/base/widget/viewpager/ScalableBannerPagerFactory.java deleted file mode 100644 index 0ada5ac..0000000 --- a/lib_base/src/main/java/com/android/base/widget/viewpager/ScalableBannerPagerFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.android.base.widget.viewpager; - -import android.content.Context; -import android.view.View; -import android.widget.ImageView; - -/** - * @author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-07-01 17:43 - */ -public interface ScalableBannerPagerFactory { - - /** - * return a scalable ImageView - */ - ImageView createBannerPagerView(Context context); - - /** - * Set a click listener for image view which is created by {@link #createBannerPagerView(Context)}, like PhotoView: - * - *
{@code
-     *         imageView.setOnPhotoTapListener((view, x, y) -> {
-     *             if (mClickListener != null) {
-     *                 mClickListener.onClick(imageView, mIsLooper ? position - 1 : position);
-     *             }
-     *         });
-     * }
- */ - void setOnClickListener(ImageView view, View.OnClickListener onClickListener); - -} diff --git a/lib_base/src/main/res/values/base_attrs.xml b/lib_base/src/main/res/values/base_attrs.xml index 6be0056..5c4a4fc 100644 --- a/lib_base/src/main/res/values/base_attrs.xml +++ b/lib_base/src/main/res/values/base_attrs.xml @@ -2,8 +2,6 @@ - - diff --git a/lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelector.java b/lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelector.java index 8c44422..4e464ee 100644 --- a/lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelector.java +++ b/lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelector.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; /** @@ -131,7 +132,7 @@ public class MediaSelector { private void processSingle(Intent data) { final ArrayList medias = Boxing.getResult(data); - if (medias != null) { + if (medias != null && !medias.isEmpty()) { mCallback.onTakePictureSuccess(medias.get(0).getPath()); } } @@ -143,15 +144,17 @@ public class MediaSelector { for (BaseMedia media : medias) { strings.add(media.getPath()); } - mCallback.onTakeMultiPictureSuccess(strings); + if (!strings.isEmpty()) { + mCallback.onTakeMultiPictureSuccess(strings); + } } } public interface Callback { - default void onTakeMultiPictureSuccess(List pictures) { + default void onTakeMultiPictureSuccess(@NonNull List pictures) { } - default void onTakePictureSuccess(String picture) { + default void onTakePictureSuccess(@NonNull String picture) { } }