optimize BannerView

androidx
Ztiany 5 years ago
parent 7095135a4d
commit cceec3fb53
  1. 7
      lib_base/src/main/java/com/android/base/app/activity/BaseActivity.kt
  2. 2
      lib_base/src/main/java/com/android/base/interfaces/OnItemClickListener.java
  3. 2
      lib_base/src/main/java/com/android/base/interfaces/OnItemLongClickListener.java
  4. 6
      lib_base/src/main/java/com/android/base/utils/common/Strings.kt
  5. 50
      lib_base/src/main/java/com/android/base/widget/viewpager/BannerPagerAdapter.java
  6. 37
      lib_base/src/main/java/com/android/base/widget/viewpager/BannerViewPager.java
  7. 32
      lib_base/src/main/java/com/android/base/widget/viewpager/ScalableBannerPagerFactory.java
  8. 2
      lib_base/src/main/res/values/base_attrs.xml
  9. 9
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelector.java

@ -131,16 +131,17 @@ abstract class BaseActivity : AppCompatActivity(), ActivityDelegateOwner, AutoDi
// interface impl // interface impl
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@UiThread @UiThread
override fun addDelegate(activityDelegate: ActivityDelegate<*>) { final override fun addDelegate(activityDelegate: ActivityDelegate<*>) {
activityDelegates.addActivityDelegate(activityDelegate) activityDelegates.addActivityDelegate(activityDelegate)
} }
@UiThread @UiThread
override fun removeDelegate(activityDelegate: ActivityDelegate<*>): Boolean { final override fun removeDelegate(activityDelegate: ActivityDelegate<*>): Boolean {
return activityDelegates.removeActivityDelegate(activityDelegate) return activityDelegates.removeActivityDelegate(activityDelegate)
} }
override fun findDelegate(predicate: Predicate<ActivityDelegate<*>?>?): ActivityDelegate<*>? { @UiThread
final override fun findDelegate(predicate: Predicate<ActivityDelegate<*>?>?): ActivityDelegate<*>? {
return activityDelegates.findDelegate(predicate) return activityDelegates.findDelegate(predicate)
} }

@ -17,6 +17,6 @@ public abstract class OnItemClickListener<T> implements View.OnClickListener {
onClick(v, (T) tag); onClick(v, (T) tag);
} }
public abstract void onClick(@NonNull View view, @NonNull T t); public abstract void onClick(@NonNull View view, @NonNull T item);
} }

@ -15,6 +15,6 @@ public abstract class OnItemLongClickListener<T> implements View.OnLongClickList
return onClick(v, (T) tag); return onClick(v, (T) tag);
} }
public abstract boolean onClick(View view, T t); public abstract boolean onClick(View view, T item);
} }

@ -19,15 +19,13 @@ private const val CONTAINS_UPPERCASE_LETTERS_REG = "^.*[A-Z]+.*$"
/** /**
* 验证中国的手机号 * 验证中国的手机号
*
* @return 验证成功返回 true验证失败返回 false
*/ */
fun isChinaPhoneNumber(mobile: String?): Boolean { fun isChinaPhoneNumber(mobile: String?): Boolean {
return !isEmpty(mobile) && Pattern.matches(CHINA_PHONE_REG, mobile) 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 { fun containsDigitalLetterOnly(text: String?): Boolean {
return !isEmpty(text) && Pattern.matches(DIGITAL_LETTER_ONLY_REG, text) 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) return !isEmpty(chinese) && Pattern.matches(CHINESE_REGEX, chinese)
} }

@ -14,21 +14,24 @@ import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.PagerAdapter;
public abstract class BannerPagerAdapter extends PagerAdapter {
class BannerPagerAdapter extends PagerAdapter { private String mTransitionName;
private Context mContext;
static ScalableBannerPagerFactory sBannerPagerFactory; private List<String> mEntities;
private final String mTransitionName;
private final Context mContext;
private final List<String> mEntities;
private OnPageClickListener mClickListener; private OnPageClickListener mClickListener;
private boolean mIsLooper; private boolean mIsLooper;
BannerPagerAdapter(Context context, List<String> entities, String transitionName) { void setTransitionName(String transitionName) {
mContext = context;
this.mEntities = entities;
mTransitionName = transitionName; mTransitionName = transitionName;
}
void setContext(Context context) {
mContext = context;
}
void setEntities(List<String> entities) {
mEntities = entities;
mIsLooper = mEntities.size() > 1; mIsLooper = mEntities.size() > 1;
} }
@ -49,25 +52,23 @@ class BannerPagerAdapter extends PagerAdapter {
@NonNull @NonNull
@Override @Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) { public Object instantiateItem(@NonNull ViewGroup container, final int position) {
if (sBannerPagerFactory == null) { ImageView imageView = createBannerPagerView(container, mContext, position);
throw new NullPointerException("You need to provide a ScalableBannerPagerFactory!");
}
ImageView imageView = sBannerPagerFactory.createBannerPagerView(mContext);
setTransitionName(imageView); setTransitionName(imageView);
callImageClicked(position, imageView);
sBannerPagerFactory.setOnClickListener(imageView, v -> {
if (mClickListener != null) {
mClickListener.onClick(imageView, mIsLooper ? position - 1 : position);
}
});
String url = mEntities.get(position); String url = mEntities.get(position);
ImageLoaderFactory.getImageLoader().display(imageView, url); ImageLoaderFactory.getImageLoader().display(imageView, url);
container.addView(imageView, 0); container.addView(imageView, 0);
return imageView; 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) { private void setTransitionName(ImageView bannerLayout) {
if (!TextUtils.isEmpty(mTransitionName)) { if (!TextUtils.isEmpty(mTransitionName)) {
ViewCompat.setTransitionName(bannerLayout, mTransitionName); ViewCompat.setTransitionName(bannerLayout, mTransitionName);
@ -84,9 +85,4 @@ class BannerPagerAdapter extends PagerAdapter {
container.removeView((View) object); container.removeView((View) object);
} }
@Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
super.setPrimaryItem(container, position, object);
}
} }

@ -11,7 +11,8 @@ import com.android.base.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import androidx.viewpager.widget.PagerAdapter; import javax.annotation.Nullable;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
/** /**
@ -28,7 +29,6 @@ public class BannerViewPager extends FrameLayout {
private OnBannerPositionChangedListener mOnBannerPositionChangedListener; private OnBannerPositionChangedListener mOnBannerPositionChangedListener;
private boolean mScalable;
private String mTransitionName; private String mTransitionName;
private OnPageClickListener mOnPageClickListener; private OnPageClickListener mOnPageClickListener;
@ -45,8 +45,6 @@ public class BannerViewPager extends FrameLayout {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerViewPager); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerViewPager);
/*是否可以缩放*/
mScalable = typedArray.getBoolean(R.styleable.BannerViewPager_zvp_scale_enable, false);
/*用于支持5.0的transition动画*/ /*用于支持5.0的transition动画*/
mTransitionName = typedArray.getString(R.styleable.BannerViewPager_zvp_item_transition_name); mTransitionName = typedArray.getString(R.styleable.BannerViewPager_zvp_item_transition_name);
int pageId = typedArray.getResourceId(R.styleable.BannerViewPager_zvp_pager_number_id, -1); int pageId = typedArray.getResourceId(R.styleable.BannerViewPager_zvp_pager_number_id, -1);
@ -74,7 +72,7 @@ public class BannerViewPager extends FrameLayout {
mPageNumberView = pageNumberView; mPageNumberView = pageNumberView;
} }
public void setImages(List<String> entities) { public void setImages(List<String> entities, @Nullable BannerPagerAdapter adapter) {
if (entities == null || entities.isEmpty()) { if (entities == null || entities.isEmpty()) {
mImageUrlList.clear(); mImageUrlList.clear();
mViewPager.setAdapter(null); mViewPager.setAdapter(null);
@ -83,16 +81,21 @@ public class BannerViewPager extends FrameLayout {
} }
mImageUrlList.clear(); mImageUrlList.clear();
setPageSize(entities.size()); setPageSize(entities.size());
if (entities.size() > 1) { if (entities.size() > 1) {
addExtraPage(entities); addExtraPage(entities);
showBanner(); showBanner(adapter);
setLooper(); setLooper();
} else { } else {
mImageUrlList.addAll(entities); mImageUrlList.addAll(entities);
showBanner(); showBanner(adapter);
} }
} }
public void setImages(List<String> entities) {
setImages(entities, null);
}
private void setPageSize(int pageSize) { private void setPageSize(int pageSize) {
if (mPageNumberView != null) { if (mPageNumberView != null) {
mPageNumberView.setPageSize(pageSize); mPageNumberView.setPageSize(pageSize);
@ -169,18 +172,18 @@ public class BannerViewPager extends FrameLayout {
mImageUrlList.add(entities.get(0)); mImageUrlList.add(entities.get(0));
} }
private void showBanner() { private void showBanner(BannerPagerAdapter adapter) {
PagerAdapter adapter; if (adapter != null) {
if (mScalable) { adapter.setContext(getContext());
BannerPagerAdapter bannerPagerAdapter = new BannerPagerAdapter(getContext(), mImageUrlList, mTransitionName); adapter.setEntities(mImageUrlList);
bannerPagerAdapter.setOnBannerClickListener(mOnPageClickListener); adapter.setTransitionName(mTransitionName);
adapter = bannerPagerAdapter; adapter.setOnBannerClickListener(mOnPageClickListener);
mViewPager.setAdapter(adapter);
} else { } else {
OptimizeBannerPagerAdapter optimizeBannerPagerAdapter = new OptimizeBannerPagerAdapter(getContext(), mImageUrlList, mTransitionName); OptimizeBannerPagerAdapter optimizeBannerPagerAdapter = new OptimizeBannerPagerAdapter(getContext(), mImageUrlList, mTransitionName);
optimizeBannerPagerAdapter.setOnBannerClickListener(mOnPageClickListener); optimizeBannerPagerAdapter.setOnBannerClickListener(mOnPageClickListener);
adapter = optimizeBannerPagerAdapter; mViewPager.setAdapter(optimizeBannerPagerAdapter);
} }
mViewPager.setAdapter(adapter);
} }
public void setOnPageClickListener(OnPageClickListener onPageClickListener) { public void setOnPageClickListener(OnPageClickListener onPageClickListener) {
@ -199,8 +202,4 @@ public class BannerViewPager extends FrameLayout {
void onPagePositionChanged(int position); void onPagePositionChanged(int position);
} }
public static void setScalableBannerPagerFactory(ScalableBannerPagerFactory scalableBannerPagerFactory) {
BannerPagerAdapter.sBannerPagerFactory = scalableBannerPagerFactory;
}
} }

@ -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:
*
* <pre>{@code
* imageView.setOnPhotoTapListener((view, x, y) -> {
* if (mClickListener != null) {
* mClickListener.onClick(imageView, mIsLooper ? position - 1 : position);
* }
* });
* }</pre>
*/
void setOnClickListener(ImageView view, View.OnClickListener onClickListener);
}

@ -2,8 +2,6 @@
<resources> <resources>
<declare-styleable name="BannerViewPager"> <declare-styleable name="BannerViewPager">
<!--if pager is scalable, set it to true.-->
<attr name="zvp_scale_enable" format="boolean"/>
<attr name="zvp_pager_number_id" format="reference"/> <attr name="zvp_pager_number_id" format="reference"/>
<attr name="zvp_item_transition_name" format="string|reference"/> <attr name="zvp_item_transition_name" format="string|reference"/>
</declare-styleable> </declare-styleable>

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
/** /**
@ -131,7 +132,7 @@ public class MediaSelector {
private void processSingle(Intent data) { private void processSingle(Intent data) {
final ArrayList<BaseMedia> medias = Boxing.getResult(data); final ArrayList<BaseMedia> medias = Boxing.getResult(data);
if (medias != null) { if (medias != null && !medias.isEmpty()) {
mCallback.onTakePictureSuccess(medias.get(0).getPath()); mCallback.onTakePictureSuccess(medias.get(0).getPath());
} }
} }
@ -143,15 +144,17 @@ public class MediaSelector {
for (BaseMedia media : medias) { for (BaseMedia media : medias) {
strings.add(media.getPath()); strings.add(media.getPath());
} }
if (!strings.isEmpty()) {
mCallback.onTakeMultiPictureSuccess(strings); mCallback.onTakeMultiPictureSuccess(strings);
} }
} }
}
public interface Callback { public interface Callback {
default void onTakeMultiPictureSuccess(List<String> pictures) { default void onTakeMultiPictureSuccess(@NonNull List<String> pictures) {
} }
default void onTakePictureSuccess(String picture) { default void onTakePictureSuccess(@NonNull String picture) {
} }
} }

Loading…
Cancel
Save