简体   繁体   English

如何在水平RecyclerView中基于Vertical RecyclerView标题设置相同的值

[英]How Can I Set Same value In Based on Vertical RecyclerView heading In Horizontal RecyclerView

I want to display the value based on Vertical RecyclerView in which there is three different heading title like Speaker,headphone and MicroPhones these three are the Category.In these category there are values which is based on these category like multiple type of speaker and headphone.The problem is that I want to display these category type based on Vertical header in horizontal recycler view.I am taking four-four item from each category so that I can display each item based on Vertical ReclerView in horizontal Recycler View. 我想显示基于Vertical RecyclerView的值,其中有三个不同的标题,如扬声器,耳机和麦克风,这三个是类别。在这些类别中,有基于这些类别的值,例如扬声器和耳机的多种类型。问题是我想在卧式回收机视图中基于``垂直''标题显示这些类别类型。我正在从每个类别中获取四到四个项目,以便可以在卧式回收机视图中基于Vertical ReclerView显示每个项目。

Here is My Main Activity from where I am Fetching the value 这是我从中获取价值的主要活动

    public class MainActivity extends AppCompatActivity implements RecyclerViewDataAdapter.OnItemClickListner {
        private ProgressDialog progressDialog;
        private Toolbar toolbar;
        String category_Name, Category_ID, Product_ID, Product_Name, Product_Image, Product_Price, Product_Sale, Cart;
        Context context;
        ArrayList<SectionDataModel> singleItemModels;
        ArrayList<SingleItemModel> allSampleData;
        RecyclerView my_recycler_view, my_recycler_vieww;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getCategoryone();
            allSampleData = new ArrayList<SingleItemModel>();
            my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view);
    //        my_recycler_vieww = (RecyclerView) findViewById(R.id.recycler_view_list);
    //        my_recycler_vieww.setHasFixedSize(true);
    //        my_recycler_vieww.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

            my_recycler_view.setHasFixedSize(true);
            my_recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        }

        public void getCategoryone() {

            final RequestQueue queue = Volley.newRequestQueue(getApplication());
            StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://proaudiobrands.com/app/feature.php",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.i("shabina shopping  response", response);
                            try {
                                singleItemModels = new ArrayList<>();
                                allSampleData = new ArrayList<>();
                                JSONObject mainObj = new JSONObject(response);
                                Log.d("shabina ", response);
                                JSONArray Feature_product = mainObj.getJSONArray("Feature_product");

                                for (int i = 0; i < Feature_product.length(); i++) {
                                    SectionDataModel sectionDataModel = new SectionDataModel();
                                    JSONObject Feature_Product = Feature_product.getJSONObject(i);
                                    String Category_Namee = Feature_Product.getString("Category_Namee");
                                    String Category_IDs = Feature_Product.getString("Category_IDs");
                                    JSONArray Product_List = Feature_Product.getJSONArray("Product_List");
                                    sectionDataModel.setHeaderTitle(Category_Namee);

                                    for (int j = 0; j < Product_List.length(); j++) {
                                        JSONObject Category_Name = Product_List.getJSONObject(j);
                                        category_Name = Category_Name.getString("Category_Name");
                                        Category_ID = Category_Name.getString("Category_ID");
                                        Product_ID = Category_Name.getString("Product_ID");
                                        Product_Name = Category_Name.getString("Product_Name");
                                        Product_Image = Category_Name.getString("Product_Image");
                                        Product_Price = Category_Name.getString("Product_Price");
                                        Product_Sale = Category_Name.getString("Product_Sale");
                                        Cart = Category_Name.getString("Cart");
                                        if (j == 4) {
                                            break;

                                        }

                                        Log.e("sushil Category_Name", category_Name + " " + Category_ID + " " + Product_ID + " " + Product_Name + " " + Product_Image + " " + Product_Price + " " + Product_Sale + " " + Cart);
                                        SingleItemModel singleItemModel1 = new SingleItemModel(category_Name, Category_ID, Product_ID, Product_Name, Product_Image, Product_Price, Product_Sale, Cart);
                                        allSampleData.add(singleItemModel1);
                                    }

                                    sectionDataModel.setAllItemsInSection(allSampleData);
                                    singleItemModels.add(sectionDataModel);
                                }


                                Log.e("Single ItemModel", String.valueOf(singleItemModels));
                                RecyclerViewDataAdapter myAdapter = new RecyclerViewDataAdapter(getApplication(), singleItemModels, MainActivity.this);
                                my_recycler_view.setAdapter(myAdapter);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }


                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("error", error.toString());

                }


            }) {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("method", "feature");
                    params.put("userId", "PRO1");


                    return params;
                }
            };
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                    90000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            queue.add(stringRequest);


        }

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(this, "Hello" + position, Toast.LENGTH_SHORT).show();

        }
    }

Here is My Vertical RecyclerView I which there is header type like HeadPhone,Speaker,MicroPhone

 public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {

    private ArrayList<SectionDataModel> arrayList;
    private Context context;
    private OnItemClickListner onItemClickListener;

    public interface OnItemClickListner {
        void onItemClick(View view, int position);
    }

    public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> arrayList, OnItemClickListner onItemClickListener) {
        this.arrayList = arrayList;
        this.onItemClickListener = onItemClickListener;
        this.context = context;
    }

    @Override
    public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
        ItemRowHolder mh = new ItemRowHolder(v);
        return mh;
    }

    @Override
    public void onBindViewHolder(ItemRowHolder holder, final int position) {


        final String Category = arrayList.get(position).getHeaderTitle();
        holder.tvProduct_Name.setText(Category);
        holder.tvProduct_Name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Postion" + arrayList.get(position).toString(), Toast.LENGTH_SHORT).show();
            }
        });
        ArrayList<SingleItemModel> arrayListt = arrayList.get(position).getAllItemsInSection();

        if (arrayList.get(0).getHeaderTitle().equals(arrayListt.get(position).getCategory_Name())) {
            SectionListDataAdapter sectionListDataAdapter = new SectionListDataAdapter(context, arrayListt);
            holder.my_recycler_vieww.setHasFixedSize(true);
            holder.my_recycler_vieww.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            holder.my_recycler_vieww.setAdapter(sectionListDataAdapter);

        }
        holder.btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Hello" + arrayList.get(position), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        if (arrayList == null)
            return 0;
        return arrayList.size();
    }

        public class ItemRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private TextView tvCategory_Name, tvCategory_ID, tvProduct_ID, tvProduct_Name, tvProductPrice, tvAmount, tvSale;
            private ImageView imgproduct;
            RecyclerView my_recycler_vieww;
            protected Button btnMore;

            public ItemRowHolder(View itemView) {
                super(itemView);
                tvProduct_Name = (TextView) itemView.findViewById(R.id.itemTitle);
                my_recycler_vieww = (RecyclerView) itemView.findViewById(R.id.recycler_view_list);


                this.btnMore = (Button) itemView.findViewById(R.id.btnMore);


            }

            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }

    }
Here Is Horizontal RecyclerView in which i have to display Section type

public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {

    private ArrayList<SingleItemModel> itemsList;
    private Context mContext;


    public interface OnItemClickListner {
        void onItemClick(View view, int position);
    }

    public SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
        this.itemsList = itemsList;
        this.mContext = context;
    }

    @Override
    public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
        SingleItemRowHolder mh = new SingleItemRowHolder(v);
        return mh;
    }

    @Override
    public void onBindViewHolder(SingleItemRowHolder holder, int i) {
        SingleItemModel singleItem = itemsList.get(i);

        holder.tvTitle.setText(itemsList.get(i).getCategory_Name());
        Picasso.with(mContext).load(singleItem.getProduct_Image()).fit().into(holder.itemImage);


    }

    @Override
    public int getItemCount() {
        return (null != itemsList ? itemsList.size() : 0);
    }

    public class SingleItemRowHolder extends RecyclerView.ViewHolder {

        protected TextView tvTitle;

        protected ImageView itemImage;


        public SingleItemRowHolder(View view) {
            super(view);

            this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            this.itemImage = (ImageView) view.findViewById(R.id.itemImage);


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


                    Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();

                }
            });


        }

    }

}

Here is my Model Class 这是我的模特班

public class SingleItemModel {
String Category_Name, Category_ID, Product_ID, Product_Name, Product_Image, Product_Price, Product_Sale, Cart;

public SingleItemModel(String category_Name) {
    Category_Name = category_Name;
}

public SingleItemModel(String category_Name, String category_ID, String product_ID, String product_Name, String product_Image, String product_Price, String product_Sale, String cart) {
    Category_Name = category_Name;
    Category_ID = category_ID;
    Product_ID = product_ID;
    Product_Name = product_Name;
    Product_Image = product_Image;
    Product_Price = product_Price;
    Product_Sale = product_Sale;
    Cart = cart;
}

public String getCategory_Name() {
    return Category_Name;
}

public void setCategory_Name(String category_Name) {
    Category_Name = category_Name;
}

public String getCategory_ID() {
    return Category_ID;
}

public void setCategory_ID(String category_ID) {
    Category_ID = category_ID;
}

public String getProduct_ID() {
    return Product_ID;
}

public void setProduct_ID(String product_ID) {
    Product_ID = product_ID;
}

public String getProduct_Name() {
    return Product_Name;
}

public void setProduct_Name(String product_Name) {
    Product_Name = product_Name;
}

public String getProduct_Image() {
    return Product_Image;
}

public void setProduct_Image(String product_Image) {
    Product_Image = product_Image;
}

public String getProduct_Price() {
    return Product_Price;
}

public void setProduct_Price(String product_Price) {
    Product_Price = product_Price;
}

public String getProduct_Sale() {
    return Product_Sale;
}

public void setProduct_Sale(String product_Sale) {
    Product_Sale = product_Sale;
}

public String getCart() {
    return Cart;
}

public void setCart(String cart) {
    Cart = cart;
}

}

Here is my Section model class 这是我的部门模型课

public class SectionDataModel {

private String headerTitle;
private ArrayList<SingleItemModel> allItemsInSection;


public SectionDataModel() {

}
public SectionDataModel(String headerTitle, ArrayList<SingleItemModel> allItemsInSection) {
    this.headerTitle = headerTitle;
    this.allItemsInSection = allItemsInSection;
}



public String getHeaderTitle() {
    return headerTitle;
}

public void setHeaderTitle(String headerTitle) {
    this.headerTitle = headerTitle;
}

public ArrayList<SingleItemModel> getAllItemsInSection() {
    return allItemsInSection;
}

public void setAllItemsInSection(ArrayList<SingleItemModel> allItemsInSection) {
    this.allItemsInSection = allItemsInSection;
}

}

Here is my Image 这是我的形象

这是我的形象

My problem is that there is 12 Object in my Arraylist and all the 12 Object are displaying in each adapter instead of based on Type 我的问题是我的Arraylist中有12个对象,并且所有12个对象都显示在每个适配器中,而不是基于Type

The if statement if(arrayList.get(0).getHeaderTitle().equals(arrayListt.get(position).getCategory_Name())) is returing false and the holder item my_recycler_vieww is simply reusing it's cached value, that why you're getting the same items. if语句if(arrayList.get(0).getHeaderTitle().equals(arrayListt.get(position).getCategory_Name()))变为false,而持有者项my_recycler_vieww只是重复使用了它的缓存值,这就是为什么得到相同的项目。 The statement should be like this 声明应该像这样

    `if(arrayList.get(0).getHeaderTitle().equals(arrayListt.get(position).getCategory_Name())) {
                SectionListDataAdapter sectionListDataAdapter = new SectionListDataAdapter(context, arrayListt);
                holder.my_recycler_vieww.setHasFixedSize(true);
                holder.my_recycler_vieww.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
                holder.my_recycler_vieww.setAdapter(sectionListDataAdapter);

            }
else{
  holder.my_recycler_vieww.setAdapter(null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM