Sfoglia il codice sorgente

让瀑布流的分割线均匀

詹子聪 6 anni fa
parent
commit
d32c6953ac

+ 12 - 34
app/src/main/java/com/miekir/eden/ui/home/coupon/TemplateAdapter.java

@@ -33,51 +33,40 @@ import java.util.List;
  * Description: 首页商品适配器
  */
 public class TemplateAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
-    private int mRadius = 8;
     private Context mContext;
+    private int mDividerSize;
 
-    private int screenWidth;
-    private int mDividerWidth;
-    private int defaultSize;
-
-    public TemplateAdapter(Context context, @Nullable List<String> data, int dividerWidth) {
+    public TemplateAdapter(Context context, @Nullable List<String> data, int dividerSize) {
         super(R.layout.item_template, data);
-        mRadius = (int) context.getResources().getDimension(R.dimen.margin_s);
         mContext = context;
-        this.screenWidth = SizeTool.SCREEN_WIDTH;
-        this.mDividerWidth = dividerWidth;
-        this.defaultSize =  (screenWidth - dividerWidth * TemplateFragment.SPACE_LINES) / TemplateFragment.SPAN_COUNT;
+        mDividerSize = dividerSize;
     }
 
     @Override
     protected void convert(@NonNull BaseViewHolder holder, String url) {
         ImageView iv_template = holder.getView(R.id.iv_template);
 
-
-        String realUrl = url.startsWith("http") ? url : BuildConfig.HOST_STATIC_RESOURCE +url;
-
+        String realUrl = url.startsWith("http") ? url : BuildConfig.HOST_STATIC_RESOURCE + url;
         // 设置默认图片
         setDefaultView(iv_template);
         // 解决图片错乱
         iv_template.setTag(R.id.aciv_goods, realUrl);
         Glide.with(mContext).load(realUrl)
-                //.transform(new CenterCrop(), new RoundedCorners(48))
-                //.apply(RequestOptions.bitmapTransform(new RoundedCorners(mRadius)))
-                //.apply(new RequestOptions()
-                //        .skipMemoryCache(true)
-                //        .diskCacheStrategy(DiskCacheStrategy.NONE))
-                //.centerCrop() 千万不要加,加了就没有圆角效果了
-                //先加载原图大小的十分之一
                 .thumbnail(0.1f)
                 .into(new SimpleTarget<Drawable>() {
                     @Override
                     public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
-                        //iv_template.setScaleType(ImageView.ScaleType.CENTER_CROP);
-                        //iv_template.setImageDrawable(resource);
                         if (resource == null) {
                             return;
                         }
 
+                        int imageWidth = resource.getIntrinsicWidth();
+                        int imageHeight = resource.getIntrinsicHeight();
+                        // 减去间距
+                        int height = (SizeTool.SCREEN_WIDTH - mDividerSize * TemplateFragment.SPACE_LINES) / TemplateFragment.SPAN_COUNT * imageHeight / imageWidth;
+                        ViewGroup.LayoutParams para = iv_template.getLayoutParams();
+                        para.height = height;
+
                         String coverUrl = null;
                         try {
                             coverUrl = (String) iv_template.getTag(R.id.aciv_goods);
@@ -85,14 +74,6 @@ public class TemplateAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
                             e.printStackTrace();
                         }
                         if (!TextUtils.isEmpty(coverUrl) && TextUtils.equals(coverUrl, realUrl)) {
-                            int imageWidth = resource.getIntrinsicWidth();
-                            int imageHeight = resource.getIntrinsicHeight();
-                            // 减去间距
-                            int height = (screenWidth - mDividerWidth * TemplateFragment.SPACE_LINES) / TemplateFragment.SPAN_COUNT * imageHeight / imageWidth;
-                            ViewGroup.LayoutParams para = iv_template.getLayoutParams();
-                            para.height = height;
-                            para.width = (screenWidth - mDividerWidth * TemplateFragment.SPACE_LINES) / TemplateFragment.SPAN_COUNT;
-                            iv_template.setScaleType(ImageView.ScaleType.FIT_XY);
                             iv_template.setImageDrawable(resource);
                         }
 
@@ -116,14 +97,11 @@ public class TemplateAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
     }
 
     private void setDefaultView(ImageView iv_template) {
-        ViewGroup.LayoutParams para = iv_template.getLayoutParams();
-        para.height = defaultSize;
-        para.width = defaultSize;
-        iv_template.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
         iv_template.setImageResource(R.mipmap.logo_gray);
     }
 
     private ItemLongClickListener couponLongClickListener;
+
     public ItemLongClickListener getCouponLongClickListener() {
         return couponLongClickListener;
     }

+ 12 - 3
app/src/main/java/com/miekir/eden/ui/home/coupon/TemplateFragment.java

@@ -28,7 +28,7 @@ public class TemplateFragment extends BaseMVPFragment implements ITemplateView<S
 
     public static final int SPAN_COUNT = 2;                // 列数
     public static final int SPACE_LINES = SPAN_COUNT + 1;  // 空隙列数
-    public static final int SPACE_WIDTH_DP = 3;            // 空隙宽度
+    public static final int SPACE_WIDTH_DP = 4;            // 空隙宽度
 
     private int mTemplateType = FunctionFragment.TEMPLATE_TYPE_BONUS;
     private boolean mIsCurrentVisible;
@@ -68,7 +68,7 @@ public class TemplateFragment extends BaseMVPFragment implements ITemplateView<S
         rv_template = rootView.findViewById(R.id.rv_template);
         // 瀑布流。必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
         mLayoutManager = new StaggeredGridLayoutManager(SPAN_COUNT, StaggeredGridLayoutManager.VERTICAL);
-        // 防止瀑布流图片位置变来变去
+        // 如果是自己计算item宽高,可以使用以下防止瀑布流图片位置变来变去,GAP_HANDLING_NONE表示不对任何空白间隙做处理
         mLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
         rv_template.setLayoutManager(mLayoutManager);
 
@@ -158,14 +158,23 @@ public class TemplateFragment extends BaseMVPFragment implements ITemplateView<S
             return;
         }
 
+        boolean hasClear = false;
         if (mCurrentPage == PAGE_START) {
             mCouponBeanList.clear();
+            hasClear = true;
         }
 
         if (couponBeanList != null) {
+            int start = mCouponBeanList.size();
             mCouponBeanList.addAll(couponBeanList);
+            if (!hasClear) {
+                mAdapter.notifyItemRangeChanged(start, couponBeanList.size());
+            } else {
+                mAdapter.notifyDataSetChanged();
+            }
+        } else {
+            mAdapter.notifyDataSetChanged();
         }
-        mAdapter.notifyDataSetChanged();
         // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
         // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
         if (mIsRefresh) {

+ 18 - 10
app/src/main/java/com/miekir/eden/widget/decoration/DividerItemDecoration.java

@@ -9,23 +9,31 @@ import androidx.recyclerview.widget.RecyclerView;
  * 均等分割线
  */
 public class DividerItemDecoration extends RecyclerView.ItemDecoration {
-    private int halfSpace;
+    private int mSpace;
+    private int mHalfSpace;
 
     public DividerItemDecoration(int space) {
-        this.halfSpace = space;
+        this.mSpace = space;
+        mHalfSpace = space / 2;
     }
 
     @Override
     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
-
-        if (parent.getPaddingLeft() != halfSpace) {
-            parent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace);
-            parent.setClipToPadding(false);
+        // 让分割线一致
+        int position = parent.getChildAdapterPosition(view);
+        if (position == 0 || position == 1) {
+            outRect.top = mSpace;
+        } else {
+            outRect.top = 0;
         }
+        outRect.bottom = mSpace;
 
-        outRect.top = halfSpace;
-        outRect.bottom = halfSpace;
-        outRect.left = halfSpace;
-        outRect.right = halfSpace;
+        if (position % 2 == 0) {
+            outRect.left = mSpace;
+            outRect.right = mHalfSpace;
+        } else {
+            outRect.left = mHalfSpace;
+            outRect.right = mSpace;
+        }
     }
 }

+ 13 - 4
app/src/main/res/layout/item_template.xml

@@ -1,7 +1,16 @@
 <?xml version="1.0" encoding="utf-8"?>
-<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/iv_template"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="@dimen/height_cover"
-    android:src="@mipmap/logo_gray" />
+    android:layout_height="wrap_content">
+
+    <!--不能使用ImageView直接作为根布局,否则会出现莫名其妙的margin-->
+    <ImageView
+        android:id="@+id/iv_template"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:scaleType="centerInside"
+        android:src="@mipmap/logo_gray" />
+    <!--必须是centerInside属性,否则分割线不均匀-->
+</FrameLayout>
+