Recyclerview Retrofit


@FormUrlEncoded@POST("scategory_product")
Call<ProductModel> GetProduct(@Field("AccessKey") String key,
                              @Field("Language") String Language,
                              @Field("Sub_Category_Id") String Sub_Category_Id);

<LinearLayout    android:id="@+id/lnr_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1">

        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:orientation="vertical">

            <TextView                android:id="@+id/tvCategoryName"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:paddingStart="10dp"                android:paddingTop="10dp"                android:paddingEnd="10dp"                android:text=""                tools:text="name"                style="@style/customfontstyle"                android:textColor="@color/blue"                android:textSize="16sp" />

            <TextView                android:id="@+id/tvItems"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:alpha=".7"                android:paddingStart="10dp"                android:paddingEnd="10dp"                android:paddingBottom="10dp"                style="@style/customfontstyle1"                android:text=""                tools:text="3 items"                android:textColor="@color/colorBlack" />
            <FrameLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1">

                <androidx.recyclerview.widget.RecyclerView                    android:id="@+id/rv_product"                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:nestedScrollingEnabled="false"                    tools:itemCount="2"                    tools:listitem="@layout/item_product" />


            </FrameLayout>
            <TextView                android:id="@+id/tv_nodata"                style="@style/customfontstyle"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:gravity="center"                android:text="@string/no_data_found"                android:textColor="@color/blue"                android:textSize="15sp"                android:visibility="gone" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</LinearLayout>

private void GetProduct()
{

    progressDialog.setTitle(getResources().getString(R.string.please_wait));
    progressDialog.setMessage(getResources().getString(R.string.loading));
    progressDialog.setCancelable(false);
    progressDialog.show();

    ApiInterface apiService = ApiClient.getClient(activity).create(ApiInterface.class);
    Call<ProductModel> call = apiService.GetProduct(Constant.ACCESSKEY, new ExtraPreferences(activity).getLanguage(),SubCategoryId);
    call.enqueue(new Callback<ProductModel>() {
        @Override        public void onResponse(Call<ProductModel> call, Response<ProductModel> response)
        {
            progressDialog.dismiss();
            //Log.e("Store==", response.body().toString());            if (response.body().getStatus().equals("1"))
            {
                rvProduct.setVisibility(View.VISIBLE);
                tvNodata.setVisibility(View.GONE);

                if(productlist.size()>0)
                    productlist.clear();

                tvItems.setText((productlist.size()+1)+" items");

                for (int i = 0; i < response.body().getProducts().size(); i++) {
                    productlist.add(response.body().getProducts().get(i));
                }
                rvProduct.setLayoutManager(new GridLayoutManager(activity, 2, RecyclerView.VERTICAL, false));
                rvProduct.setAdapter(new ProductAdapter(activity,productlist));

            } else {
                rvProduct.setVisibility(View.GONE);
                tvNodata.setVisibility(View.VISIBLE);
                rvProduct.setLayoutManager(new GridLayoutManager(activity, 2, RecyclerView.VERTICAL, false));
                rvProduct.setAdapter(new ProductAdapter(activity,productlist));
                // Toast.makeText(getActivity(), "No data", Toast.LENGTH_SHORT).show();            }
        }

        @Override        public void onFailure(Call<ProductModel> call, Throwable t) {
            // Log error here since request failed            progressDialog.dismiss();
            Log.e("NETWORK ERROR --> ", t.toString());
        }
    });



public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Myviewholder> {
    Context context;
    ArrayList<ProductModel.Product> productlist;

    public ProductAdapter(Activity activity, ArrayList<ProductModel.Product> productlist) {
        this.context = activity;
        this.productlist = productlist;
    }

    @NonNull    @Override    public Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Myviewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false));
    }

    @Override    public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
        final ProductModel.Product bean = productlist.get(position);
        holder.tvCollectionName.setText(bean.getProductName());
        holder.tvPrice.setText(context.getResources().getString(R.string.rupee) + bean.getPrice().get(0).getSellPrice());
        holder.tvPrice.setPaintFlags(holder.tvPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);


        RequestOptions options = new RequestOptions()
                .centerCrop()
                //.placeholder(R.drawable.demo)                .error(R.drawable.demo)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH);

        Glide.with(context)
                .load(Constant.Product_Image + bean.getProductImage())
                .apply(options)
                .thumbnail(Glide.with(context).load(R.drawable.loader))
                .into(holder.ivCollection);



        holder.tvDiscountPrice.setText(context.getResources().getString(R.string.rupee) + bean.getPrice().get(0).getDiscountedPrice());
        holder.tvDiscountPercent.setText("( " + bean.getPrice().get(0).getDiscountPercent() + " % off )");


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {


                context.startActivity(new Intent(context, ProductDetailsActivity.class)
                        .putExtra("ProductId", bean.getProductId())
                        .putExtra("MerchantId", String.valueOf(bean.getMerchantId())));
                Functions.animNext(context);

            }
        });


    }

    @Override    public int getItemCount() {
        return productlist.size();
    }

    public class Myviewholder extends RecyclerView.ViewHolder {
        ImageView ivCollection;
        TextView tvCollectionName, tvProductName, tvDiscountPrice, tvDiscountPercent, tvPrice;

        public Myviewholder(@NonNull View itemView) {
            super(itemView);

            ivCollection = itemView.findViewById(R.id.ivCollection);
            tvCollectionName = itemView.findViewById(R.id.tvCollectionName);
            tvProductName = itemView.findViewById(R.id.tvProductName);
            tvDiscountPrice = itemView.findViewById(R.id.tvDiscountPrice);
            tvDiscountPercent = itemView.findViewById(R.id.tvDiscountPercent);
            tvPrice = itemView.findViewById(R.id.tvPrice);
        }
    }
}


xml Adepter

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@android:color/white"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical">

    <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">

            <FrameLayout                android:id="@+id/frameCollection"                android:layout_width="match_parent"                android:layout_height="0dp"                android:layout_weight="1">

                <ImageView                    android:id="@+id/ivCollection"                    android:layout_width="match_parent"                    android:layout_height="250dp"                    android:layout_gravity="center"                    android:contentDescription="@string/todo"                    android:src="@drawable/demo" />
            </FrameLayout>

            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@android:color/white"                android:orientation="vertical"                android:paddingTop="5dp"                android:paddingBottom="5dp">

                <TextView                    android:id="@+id/tvCollectionName"                    style="@style/customfontstyle1"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:gravity="center"                    android:text="Project Causal"                    android:textColor="@color/colorGolden"                    android:textSize="14sp"                    android:visibility="visible" />

                <TextView                    android:id="@+id/tvProductName"                    style="@style/customfontstyle1"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:gravity="center"                    android:singleLine="true"                    android:text="Flared Kurta with Tie-Up"                    android:textSize="14sp"                    android:visibility="gone" />

                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_marginTop="5dp"                    android:gravity="center"                    android:orientation="horizontal"                    android:visibility="visible">

                    <TextView                        android:id="@+id/tvDiscountPrice"                        style="@style/customfontstyle1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:gravity="center"                        android:singleLine="true"                        android:text="\u20B9 599"                        android:textColor="@color/colorBlack"                        android:textStyle="bold" />

                    <TextView                        android:id="@+id/tvPrice"                        style="@style/customfontstyle1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginStart="8dp"                        android:layout_marginEnd="8dp"                        android:gravity="center"                        android:singleLine="true"                        android:text="599"                        android:textSize="14sp" />

                    <TextView                        android:id="@+id/tvDiscountPercent"                        style="@style/customfontstyle1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:gravity="center"                        android:singleLine="true"                        android:text="(50% off)"                        android:textColor="@color/colorGolden"                        android:textSize="14sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>


</LinearLayout>



public class ProductModel
{


        @SerializedName("Products")
        @Expose        private List<Product> products = null;
        @SerializedName("Message")
        @Expose        private String message;
        @SerializedName("Status")
        @Expose        private String status;

        public List<Product> getProducts() {
            return products;
        }

        public void setProducts(List<Product> products) {
            this.products = products;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }



    public class Price {

        @SerializedName("Price_Id")
        @Expose        private String priceId;
        @SerializedName("MRP")
        @Expose        private String mRP;
        @SerializedName("Sell_Price")
        @Expose        private String sellPrice;
        @SerializedName("In_Stock")
        @Expose        private String inStock;
        @SerializedName("Discount_Percent")
        @Expose        private String discountPercent;
        @SerializedName("Discounted_Price")
        @Expose        private String discountedPrice;
        @SerializedName("In_Cart")
        @Expose        private String inCart;

        public String getPriceId() {
            return priceId;
        }

        public void setPriceId(String priceId) {
            this.priceId = priceId;
        }

        public String getMRP() {
            return mRP;
        }

        public void setMRP(String mRP) {
            this.mRP = mRP;
        }

        public String getSellPrice() {
            return sellPrice;
        }

        public void setSellPrice(String sellPrice) {
            this.sellPrice = sellPrice;
        }

        public String getInStock() {
            return inStock;
        }

        public void setInStock(String inStock) {
            this.inStock = inStock;
        }

        public String getDiscountPercent() {
            return discountPercent;
        }

        public void setDiscountPercent(String discountPercent) {
            this.discountPercent = discountPercent;
        }

        public String getDiscountedPrice() {
            return discountedPrice;
        }

        public void setDiscountedPrice(String discountedPrice) {
            this.discountedPrice = discountedPrice;
        }

        public String getInCart() {
            return inCart;
        }

        public void setInCart(String inCart) {
            this.inCart = inCart;
        }

    }


    public class Product {

        @SerializedName("Product_Id")
        @Expose        private String productId;
        @SerializedName("Product_Name")
        @Expose        private String productName;
        @SerializedName("Product_Image")
        @Expose        private String productImage;
        @SerializedName("Wishlist")
        @Expose        private String wishlist;
        @SerializedName("Cart")
        @Expose        private String cart;
        @SerializedName("Cart_Price_Id")
        @Expose        private String cartPriceId;
        @SerializedName("Product_Status")
        @Expose        private String productStatus;
        @SerializedName("Merchant_Id")
        @Expose        private Integer merchantId;
        @SerializedName("Product_Price")
        @Expose        private String productPrice;
        @SerializedName("Price")
        @Expose        private List<Price> price = null;

        public String getProductId() {
            return productId;
        }

        public void setProductId(String productId) {
            this.productId = productId;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public String getProductImage() {
            return productImage;
        }

        public void setProductImage(String productImage) {
            this.productImage = productImage;
        }

        public String getWishlist() {
            return wishlist;
        }

        public void setWishlist(String wishlist) {
            this.wishlist = wishlist;
        }

        public String getCart() {
            return cart;
        }

        public void setCart(String cart) {
            this.cart = cart;
        }

        public String getCartPriceId() {
            return cartPriceId;
        }

        public void setCartPriceId(String cartPriceId) {
            this.cartPriceId = cartPriceId;
        }

        public String getProductStatus() {
            return productStatus;
        }

        public void setProductStatus(String productStatus) {
            this.productStatus = productStatus;
        }

        public Integer getMerchantId() {
            return merchantId;
        }

        public void setMerchantId(Integer merchantId) {
            this.merchantId = merchantId;
        }

        public String getProductPrice() {
            return productPrice;
        }

        public void setProductPrice(String productPrice) {
            this.productPrice = productPrice;
        }

        public List<Price> getPrice() {
            return price;
        }

        public void setPrice(List<Price> price) {
            this.price = price;
        }

    }
}

Comments

Popular posts from this blog

retrofil gjstatus lanuage

form object

Login Preference in android create class