Parcourir la source

Try to Fix #209;Deprecated setRoundRect;Code Optimized.

zhpanvip il y a 3 ans
Parent
commit
695c09191d

+ 1 - 1
app/src/main/java/com/example/zhpan/banner/activity/WelcomeActivity.kt

@@ -107,7 +107,7 @@ class WelcomeActivity : BaseDataActivity() {
     }
 
     private fun updateUI(position: Int) {
-        binding.tvDescribe?.text = des[position]
+        binding.tvDescribe.text = des[position]
         val translationAnim = ObjectAnimator.ofFloat(binding.tvDescribe, "translationX", -120f, 0f)
         translationAnim.apply {
             duration = ANIMATION_DURATION.toLong()

+ 1 - 1
app/src/main/java/com/example/zhpan/banner/adapter/MultiViewTypesAdapter.java

@@ -11,7 +11,7 @@ import com.zhpan.bannerview.BaseViewHolder;
 /**
  * <pre>
  *   Created by zhpan on 2020/4/6.
- *   Description:Multiple view types adapter sample.
+ *   Description:Sample of multiple view types .
  * </pre>
  */
 public class MultiViewTypesAdapter extends BaseBannerAdapter<BannerData> {

+ 1 - 1
app/src/main/java/com/example/zhpan/banner/fragment/HomeFragment.java

@@ -125,7 +125,7 @@ public class HomeFragment extends BaseFragment {
                     headerView.setVisibility(View.VISIBLE);
                     List<BannerData> dataList = response.getDataBeanList();
                     BannerData bannerData = new BannerData();
-                    bannerData.setDrawable(R.drawable.bg_card0);
+                    bannerData.setDrawable(R.drawable.bg_card3);
                     bannerData.setType(TYPE_NEW);
                     bannerData.setTitle("这是一个自定义类型");
                     dataList.add(1, bannerData);

+ 1 - 1
app/src/main/res/layout/item_new_type.xml

@@ -20,7 +20,7 @@
       android:paddingEnd="@dimen/dp_10"
       android:paddingStart="@dimen/dp_10"
       android:paddingTop="@dimen/dp_5"
-      android:text="多类型Item示例"
+      android:text="Multiple View Types "
       android:textColor="#fff"
       android:textSize="@dimen/sp_26" />
 

+ 29 - 31
bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java

@@ -80,12 +80,7 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
 
   private ViewPager2.OnPageChangeCallback onPageChangeCallback;
 
-  private final Runnable mRunnable = new Runnable() {
-    @Override
-    public void run() {
-      handlePosition();
-    }
-  };
+  private final Runnable mRunnable = this::handlePosition;
 
   private RectF mRadiusRectF;
   private Path mRadiusPath;
@@ -442,7 +437,6 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
 
   private void refreshIndicator(List<? extends T> data) {
     setIndicatorValues(data);
-    boolean canLoop = mBannerManager.getBannerOptions().isCanLoop();
     mBannerManager.getBannerOptions().getIndicatorOptions()
         .setCurrentPosition(BannerUtils.getRealPosition(mViewPager.getCurrentItem(), data.size()));
     mIndicatorView.notifyDataChanged();
@@ -485,7 +479,7 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
     super.onRestoreInstanceState(superState);
     currentPosition = bundle.getInt(KEY_CURRENT_POSITION);
     isCustomIndicator = bundle.getBoolean(KEY_IS_CUSTOM_INDICATOR);
-    setCurrentItem(currentPosition);
+    setCurrentItem(currentPosition,false);
   }
 
   /**
@@ -560,29 +554,6 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
     return this;
   }
 
-  /**
-   * Set round rectangle effect for BannerViewPager.
-   *
-   * @param radius round radius
-   */
-  public BannerViewPager<T> setRoundRect(int radius) {
-    return setRoundCorner(radius);
-  }
-
-  /**
-   * Set round rectangle effect for BannerViewPager.
-   *
-   * @param topLeftRadius top left round radius
-   * @param topRightRadius top right round radius
-   * @param bottomLeftRadius bottom left round radius
-   * @param bottomRightRadius bottom right round radius
-   */
-  public BannerViewPager<T> setRoundRect(int topLeftRadius, int topRightRadius,
-      int bottomLeftRadius,
-      int bottomRightRadius) {
-    return setRoundCorner(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
-  }
-
   /**
    * Enable/disable auto play
    *
@@ -1158,4 +1129,31 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
     mBannerManager.getBannerOptions().setDisallowParentInterceptDownEvent(disallowIntercept);
     return this;
   }
+
+  /**
+   * Set round rectangle effect for BannerViewPager.
+   *
+   * @param radius round radius
+   * @deprecated Use {@link #setRoundCorner(int)} instead.
+   */
+  @Deprecated
+  public BannerViewPager<T> setRoundRect(int radius) {
+    return setRoundCorner(radius);
+  }
+
+  /**
+   * Set round rectangle effect for BannerViewPager.
+   *
+   * @param topLeftRadius top left round radius
+   * @param topRightRadius top right round radius
+   * @param bottomLeftRadius bottom left round radius
+   * @param bottomRightRadius bottom right round radius
+   * @deprecated Use {@link #setRoundCorner(int, int, int, int)} instead.
+   */
+  @Deprecated
+  public BannerViewPager<T> setRoundRect(int topLeftRadius, int topRightRadius,
+      int bottomLeftRadius,
+      int bottomRightRadius) {
+    return setRoundCorner(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
+  }
 }

+ 13 - 12
bannerview/src/main/java/com/zhpan/bannerview/BaseBannerAdapter.java

@@ -27,21 +27,21 @@ public abstract class BaseBannerAdapter<T> extends RecyclerView.Adapter<BaseView
   public final BaseViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     View itemView =
         LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
-    return createViewHolder(parent, itemView, viewType);
+    BaseViewHolder<T> viewHolder = createViewHolder(parent, itemView, viewType);
+    itemView.setOnClickListener(clickedView -> {
+      int adapterPosition = viewHolder.getAdapterPosition();
+      if (mPageClickListener != null && adapterPosition != RecyclerView.NO_POSITION) {
+        int realPosition =
+            BannerUtils.getRealPosition(viewHolder.getAdapterPosition(), getListSize());
+        mPageClickListener.onPageClick(clickedView, realPosition);
+      }
+    });
+    return viewHolder;
   }
 
   @Override
   public final void onBindViewHolder(@NonNull BaseViewHolder<T> holder, final int position) {
     int realPosition = BannerUtils.getRealPosition(position, getListSize());
-    holder.itemView.setOnClickListener(new View.OnClickListener() {
-      @Override
-      public void onClick(View clickedView) {
-        if (mPageClickListener != null) {
-          mPageClickListener.onPageClick(clickedView,
-              BannerUtils.getRealPosition(position, getListSize()));
-        }
-      }
-    });
     bindData(holder, mList.get(realPosition), realPosition, getListSize());
   }
 
@@ -92,8 +92,9 @@ public abstract class BaseBannerAdapter<T> extends RecyclerView.Adapter<BaseView
   }
 
   /**
-   * Don't need override this method in subclass.
-   * This method calls {@link #onCreateViewHolder(ViewGroup, int)} to create a new{@link
+   * Generally,there is no need to override this method in subclasses.
+   *
+   * This method called by {@link #onCreateViewHolder(ViewGroup, int)} to create a default {@link
    * BaseViewHolder}
    *
    * @param parent The ViewGroup into which the new View will be added after it is bound to