以编程方式创建多个recycleviewer Android

Posted

技术标签:

【中文标题】以编程方式创建多个recycleviewer Android【英文标题】:creating multiple recycleviewer programmatically Android 【发布时间】:2018-04-29 05:44:39 【问题描述】:

我想通过循环遍历我的Json 结果来创建可以多次使用的RecyclerView。 我想要的是创建产品类别(RecyclerViews),每个产品都包含可以水平滚动的产品,就像这些Image 一样。

所以我在Internet 周围搜索了任何东西。

然后我决定这样做:

    创建多个 recyclerView(例如 recycler_1、recycler_2) 然后循环通过我的 Json,同时将适配器 (data) 添加到这些回收器中

现在我的问题是,当用户到达底部时,我想添加更多类别(及其产品),我不知道如何以编程方式或任何其他替代方式添加更多 recyclerViews。


这是我的一些代码:

这是我的 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);

【问题讨论】:

【参考方案1】:

这个库最适合您的要求。

SectionRecyclerView

【讨论】:

以上是关于以编程方式创建多个recycleviewer Android的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式创建多个 NSTextField?

多个ListViews在android中以编程方式创建

如何在android中以编程方式创建多个列表

以编程方式创建具有多个标签的 UIView

ipad如何在一个视图上以编程方式创建多个表

在 Android 中以编程方式创建的多个文本视图的单个 onclick 侦听器