我想创建RecyclerView
,通过循环浏览Json
结果,我可以多次使用它。
我想要的是创建产品类别( RecyclerView
s ),每个产品都包含可以像这些Image一样水平滚动的产品。
所以我在Internet
周围搜索,我找不到任何东西。
然后我决定这样做:
data
)添加到这些回收站现在我的问题是我想在用户到达底部时添加更多类别(及其产品),而且我不知道如何以编程方式或任何其他替代方式添加更多的recyclerView。
以下是我的一些代码:
这是我的Json成绩
{
"products":[
{
"category":[
{
"category_name":"Audio & TV"
},
{
"inner_products":[
{
"id":"3442",
"product_thumb":"19279c812c9e56a0ca8fea095f36cb22.jpeg",
"brand_name":"Others",
"product_name":"4ga wiring kit",
"saved_amount":"0",
"product_price":"269",
"supplier_logo":"467909bb980b06aca7588be54fa4cb68.jpeg"
},
{
"id":"3444",
"product_thumb":"dd07c851cfaf1830f6b44f7c55298a18.jpeg",
"brand_name":"Kenwood",
"product_name":"car radio double din ",
"saved_amount":"400",
"product_price":"2299",
"supplier_logo":"467909bb980b06aca7588be54fa4cb68.jpeg"
}
]
}
]
},
{
"category":[
{
"category_name":"building tools"
},
{
"inner_products":[
{
"id":"124",
"product_thumb":"c482e6b5ea0ee76c4e6524b86442bf27.png",
"brand_name":"Others",
"product_name":"drill bosch blue cordless 18v 4a",
"saved_amount":"0",
"product_price":"3695",
"supplier_logo":"21fa5955772bea6a5a1beae155860e6f.jpeg"
},
{
"id":"123",
"product_thumb":"124591fc38e03702774c99fa00515ca7.png",
"brand_name":"Others",
"product_name":"grinder bosch blue angle 125mm 850w",
"saved_amount":"0",
"product_price":"799.95",
"supplier_logo":"21fa5955772bea6a5a1beae155860e6f.jpeg"
}
]
}
]
}
]
}
以下是我如何遍历Json
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading ");
progressDialog.show();
StringRequest productsStringrequest = new StringRequest(Request.Method.GET, products_url,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray productsArray = jsonObject.getJSONArray("products");
for (int i = 0; i < productsArray.length(); i++){
int[] procustRecycler = new int[]{R.id.procustRecycler_0, R.id.procustRecycler_1, R.id.procustRecycler_2, R.id.procustRecycler_3, R.id.procustRecycler_4, R.id.procustRecycler_5, R.id.procustRecycler_6, R.id.procustRecycler_7, R.id.procustRecycler_8, R.id.procustRecycler_9};
int[] pr_header = new int[]{R.id.product_header_0, R.id.product_header_1, R.id.product_header_2, R.id.product_header_3, R.id.product_header_4, R.id.product_header_5, R.id.product_header_6, R.id.product_header_7, R.id.product_header_8, R.id.product_header_9 };
JSONObject categoryObject = productsArray.getJSONObject(i);
JSONArray categoryArray = categoryObject.getJSONArray("category");
for (int b = 0; b < categoryArray.length(); b++){
JSONObject singleCategoryObject = categoryArray.getJSONObject(0);
RecyclerView allProductsRecycler = (RecyclerView) findViewById(procustRecycler[i]);
allProductsRecycler.setHasFixedSize(true);
allProductsRecycler.setLayoutManager(new LinearLayoutManager(Products.this, LinearLayoutManager.HORIZONTAL, false));
List<ProductsItems> products_list = new ArrayList<>();
if(b == 0) {
TextView product_header = (TextView) findViewById(pr_header[i]);
product_header.setText(singleCategoryObject.getString("category_name"));
continue;
}
JSONObject productsCategoryObject = categoryArray.getJSONObject(b);
JSONArray singleProductsArray = productsCategoryObject.getJSONArray("inner_products");
for (int c=0; c< singleProductsArray.length(); c++){
JSONObject object = singleProductsArray.getJSONObject(c);
ProductsItems items = new ProductsItems(
object.getInt("id"),
object.getString("product_name"),
object.getString("product_thumb"),
object.getString("supplier_logo"),
object.getString("brand_name"),
object.getString("product_price"),
object.getString("saved_amount")
);
products_list.add(items);
}
RecyclerView.Adapter allProductsAdapter = new ProductsAdapter(products_list, getApplicationContext());
allProductsRecycler.setAdapter(allProductsAdapter);
}
}
} catch (JSONException e) {
System.out.println(e.getMessage());
Toast.makeText(Products.this, "Catche error: "+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
if (error instanceof TimeoutError ) {
Toast.makeText(Products.this, "Network time out", Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(Products.this, "Login fail", Toast.LENGTH_LONG).show();
} else if (error instanceof ServerError) {
Toast.makeText(Products.this, "Server error", Toast.LENGTH_LONG).show();
} else if (error instanceof NetworkError) {
Toast.makeText(Products.this, "Network error", Toast.LENGTH_LONG).show();
} else if (error instanceof ParseError) {
Toast.makeText(Products.this, "Unknown", Toast.LENGTH_LONG).show();
}
}
}
);
SingleTon.getInstance(getApplicationContext()).addToRequestQue(productsStringrequest);