We know that how to fetch categories from shopify store there we get each category id that we ll used further to get products.
1. Main activity class to get product list :
public class ProductsListActivity extends AppCompatActivity implements ShopifyOperationInterface {
private RecyclerView rvProducts;
private RecyclerView.Adapter mAdapter;
private ArrayList<Product> mArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_list);
final String mCollectionId = getIntent().getStringExtra(AppConstant.sKeyCollectionId);
rvProducts = (RecyclerView)findViewById(R.id.rvProducts);
TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
rvProducts.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(ProductsListActivity.this);
rvProducts.setLayoutManager(mLayoutManager);
rvProducts.setItemAnimator(new DefaultItemAnimator());
tvTitle.setText(getIntent().getStringExtra(AppConstant.sKeyCollectionName));
mArrayList = ShopifyOperationsHandler.getProductList(Long.parseLong(mCollectionId), ProductsListActivity.this);
}
@Override
public void success(String aType) {
if (aType.equalsIgnoreCase(ProductsListActivity.class.getName())) {
Log.e("list size:", mArrayList.size() + "");
if (mArrayList != null && mArrayList.size() > 0) {
rvProducts.setAdapter(mAdapter);
}
}
}
}
2. Adapter class for product list :
public class AdapterProductsList extends RecyclerView.Adapter<AdapterProductsList.MyViewHolder> {
private ArrayList<Product> mProductsDataArrayList;
private Activity mActivity;
public AdapterProductsList(ArrayList<Product> aProductsDataArrayList, Activity aActivity){
mProductsDataArrayList = aProductsDataArrayList;
mActivity = aActivity;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvWineName;
ImageView imgWine;
Button btnPrice;
LinearLayout llAddCart;
ProgressBar progressbarProducts;
public MyViewHolder(View itemView) {
super(itemView);
this.tvWineName = (TextView) itemView.findViewById(R.id.tvWineName);
this.imgWine = (ImageView) itemView.findViewById(R.id.imgWine);
this.btnPrice = (Button) itemView.findViewById(R.id.btnPrice);
this.llAddCart = (LinearLayout)itemView.findViewById(R.id.llAddCart);
this.progressbarProducts = (ProgressBar)itemView.findViewById(R.id.progressbarProducts);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_products, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView tvWineName = holder.tvWineName;
ImageView imgWine = holder.imgWine;
Button btnPrice = holder.btnPrice;
LinearLayout llAddCart = holder.llAddCart;
final ProgressBar progressBar = holder.progressbarProducts;
tvWineName.setText(mProductsDataArrayList.get(listPosition).getTitle());
btnPrice.setText("$" + mProductsDataArrayList.get(listPosition).getMinimumPrice());
}
@Override
public int getItemCount() {
return mProductsDataArrayList.size();
}
}
3. Method to get products list :
public static ArrayList<Product> getProductList(Long aCollectionId, final Activity aActivity){
final ArrayList<Product> mArrayList = new ArrayList<>();
initializeProgress(aActivity);
showProgress(aActivity);
final ShopifyOperationInterface mShopifyOperationInterface = (ShopifyOperationInterface)aActivity;
SampleApplication.getBuyClient().getProducts(1, aCollectionId, null, null, new Callback<List<Product>>() {
@Override
public void success(List<Product> products) {
Log.e("products list size:", products.size() + "");
if (products.size() > 0) {
for (int i = 0; i < products.size(); i++) {
Log.e("products title:", products.get(i).getTitle() + "");
mArrayList.add(products.get(i));
}
}
mShopifyOperationInterface.success(ProductsListActivity.class.getName());
}
@Override
public void failure(BuyClientError error) {
Log.e("products error:", error.getMessage());
dismissProgress(aActivity);
}
});
dismissProgress(aActivity);
return mArrayList;
}
4. You can create your own item file like this :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/list_item_color"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/imgWine"
android:layout_width="@dimen/myceller_wine_size"
android:layout_height="@dimen/myceller_wine_size"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_centerInParent="true"
android:src="@drawable/img_placeholder" />
<ProgressBar
android:id="@+id/progressbarProducts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/progress" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_ten"
android:layout_marginRight="@dimen/padding_ten"
android:orientation="vertical">
<TextView
android:id="@+id/tvWineName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/padding_ten"
android:layout_toLeftOf="@+id/llAddCart"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="@color/dark_red"
android:textSize="@dimen/text_size_mid"
android:textStyle="bold" />
<Button
android:id="@+id/btnPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvWineName"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/padding_ten"
android:layout_marginTop="@dimen/padding_ten"
android:background="@drawable/red_btn_corner"
android:padding="5dp"
android:text=""
android:textColor="@color/white"
android:textSize="@dimen/text_size_large" />
<LinearLayout
android:id="@+id/llAddCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_ten"
android:src="@drawable/cart" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
In our next blog you can check how can we add any product into cart.
1 Comment(s)