1
  • <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:background="@color/transparent"
        android:layout_height="wrap_content">
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardUseCompatPadding="true"
            app:cardCornerRadius="6dp"
            app:cardElevation="5dp">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:id="@+id/comPreview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:adjustViewBounds="true"
                    android:src="@mipmap/test" />
    
                <TextView
                    android:id="@+id/comName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/comPreview"
                    android:layout_marginTop="20dp"
                    android:layout_marginBottom="5dp"
                    android:text="标题"
                    android:textColor="@color/black"
                    android:textSize="20sp" />
    
                <TextView
                    android:id="@+id/comPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/comName"
                    android:text="¥0.0"
                    android:textColor="@color/red" />
    
                <TextView
                    android:id="@+id/paidNum"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/comName"
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@id/comPrice"
                    android:text="0人付款" />
    
                <TextView
                    android:id="@+id/comStock"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/comName"
                    android:layout_alignParentRight="true"
                    android:text="库存10000" />
            </RelativeLayout>
    
        </androidx.cardview.widget.CardView>
    
    </RelativeLayout>
    


    public class CategoryPagerPresenterImpl implements ICategoryPagerPresenter {
        
        private Map<String, Integer> pageInfo = new HashMap<>();
        private static final int DEFAULT_PAGE = 1;
    
        private CategoryPagerPresenterImpl(){}
    
        private static ICategoryPagerPresenter sInstance = null;
    
        public static ICategoryPagerPresenter getInstance(){
            if (sInstance == null) {
                sInstance = new CategoryPagerPresenterImpl();
            }
            return sInstance;
        }
    
        @Override
        public void getContentByCategory(String categoryTitle, int categoryId) {
    
            for (ICategoryPagerCallback callback : callbacks) {
                if (callback.getCategoryId() == categoryId) {
                    callback.onLoading();
                }
            }
            //根据分类加载内容
            Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
            Api api = retrofit.create(Api.class);
            Integer targetPage = pageInfo.get(categoryTitle);
            if (targetPage == null) {
                targetPage = DEFAULT_PAGE;
                pageInfo.put(categoryTitle, targetPage);
            }
            String shopPagerUrl = UrlUtils.createShopPagerUrl(categoryTitle);
            LogUtils.d(CategoryPagerPresenterImpl.this, "shop pager url --> " + shopPagerUrl);
    
            Call<Commodities> task = api.getCommoditiesByCategory(shopPagerUrl);
            task.enqueue(new Callback<Commodities>() {
                @Override
                public void onResponse(Call<Commodities> call, Response<Commodities> response) {
                    int code = response.code();
                    LogUtils.d(CategoryPagerPresenterImpl.this, "code --> " + code);
                    if (code == HttpURLConnection.HTTP_OK) {
                        Commodities commodities = response.body();
                        LogUtils.d(CategoryPagerPresenterImpl.this, "commodities --> " + commodities.toString());
                        //更新UI
                        handleShopPagerContentResult(commodities, categoryId);
                    } else {
                        handlerNetworkError(categoryId);
                    }
                }
                @Override
                public void onFailure(Call<Commodities> call, Throwable t) {
                    LogUtils.d(CategoryPagerPresenterImpl.this, "onFailure --> " + t.toString());
                }
            });
        }
    
        private void handlerNetworkError(int categoryId) {
            for (ICategoryPagerCallback callback : callbacks){
                if (callback.getCategoryId() == categoryId) {
                    callback.onError();
                }
            }
        }
    
        /**
         * 通知UI更新
         * @param commodities
         * @param categoryId
         */
        private void handleShopPagerContentResult(Commodities commodities, int categoryId) {
            for (ICategoryPagerCallback callback : callbacks){
                if (callback.getCategoryId() == categoryId) {
                    if (commodities == null || commodities.getData().size() == 0) {
                        callback.onEmpty();
                    } else {
                        callback.onContentLoaded(commodities.getData());
                    }
                }
            }
        }
    
        private ArrayList<ICategoryPagerCallback> callbacks = new ArrayList<>();
        @Override
        public void registerCallback(ICategoryPagerCallback callback) {
            if (!callbacks.contains(callback)) {
                callbacks.add(callback);
            }
    
        }
    
        @Override
        public void unregisterCallback(ICategoryPagerCallback callback) {
            callbacks.remove(callback);
        }
    }
    


    public class ShopPagerContentAdapter extends RecyclerView.Adapter<ShopPagerContentAdapter.InnerHolder> {
    
        List<Commodities.DataBean> mData = new ArrayList<>();
    
        @NonNull
        @Override
        public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stagger_shop_commodity, parent, false);
            return new InnerHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
            Commodities.DataBean dataBean = mData.get(position);
            holder.setData(dataBean);
        }
    
        @Override
        public int getItemCount() {
            if (mData != null) {
                return mData.size();
            }
            return 0;
        }
    
        public void setCommodities(List<Commodities.DataBean> commodities) {
            mData.clear();
            mData.addAll(commodities);
            notifyDataSetChanged();
        }
    
        public class InnerHolder extends RecyclerView.ViewHolder {
    
            @BindView(R.id.comName)
            public TextView comName;
            @BindView(R.id.comPrice)
            public TextView comPrice;
            @BindView(R.id.comStock)
            public TextView comStock;
            @BindView(R.id.paidNum)
            public TextView paidNum;
            @BindView(R.id.comPreview)
            public ImageView comPreview;
    
            //找到条目控件
            public InnerHolder(@NonNull View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
    
            public void setData(Commodities.DataBean dataBean) {
                comName.setText(dataBean.getComName());
                comPrice.setText("¥"+dataBean.getComPrice());
                comStock.setText("库存"+dataBean.getComStock());
                paidNum.setText(dataBean.getPaidNum()+"人付款");
                Glide.with(itemView.getContext()).load(dataBean.getComPreview()).into(comPreview);
    
            }
        }
    }
    


    1480009817853648898  评论     打赏       Opcheers
    • 失效,应该是写布局有问题。但是没代码,不好分析

      1139423796017500160  评论     打赏       工头断点
      • 问题1 :失效 找不到指定id 或者你这个id在你这个view的下面(相对布局有顺序的)

        属性具体查看:https://www.runoob.com/w3cnote/android-tutorial-relativelayout.html


        问题2: 是不是数据太少了 没太懂 就像康师傅说的 要代码看看

        1382711465131241472  评论     打赏       阿肥
        • 上代码

          1153952789488054272  评论     打赏       拉大锯
          相关问题
          _empty · Android
          2019-10-20 23:51 628 2
          application · Android
          2019-11-05 00:26 779 2
          三流废物 · android / 小白
          2019-11-22 00:35 1067 5
          豪豪好嘛 · Android / GPS
          2019-11-26 20:15 884 5
          Jian · android
          2019-11-28 01:51 615 2
          2019-12-01 01:01 705 2
          小陈学编程 · Android
          2019-12-01 19:08 633 2
          fkinggod · Android
          2019-12-08 09:19 661 5
          fkinggod · Android
          2019-12-08 20:47 610 2
          kissmyass · Android
          2019-12-09 07:08 537 2
          独一无二的名zi · Android
          2019-12-30 06:48 415 2