我想显示基于垂直RecyclerView的值,其中有三个不同的标题标题,如扬声器、耳机和麦克风,这三个是类别。在这些类别中,有基于这些类别的值,如多种类型的扬声器和耳机。问题是我想在水平回收器 View 中基于垂直标题显示这些类别类型。我从每个类别中取出四到四个项目,以便可以在水平回收器 View 中基于垂直ReclerView显示每个项目。
这是我从中获取值的主要 Activity
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();
}
});
}
}
}
这是我的模型类
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;
}
}
这是我的部分模型类
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;
}
}
这是我的图片
我的问题是我的Arraylist中有12个对象,并且所有12个对象都显示在每个适配器中,而不是基于类型
最佳答案
if 语句 if(arrayList.get(0).getHeaderTitle().equals(arrayListt.get(position).getCategory_Name()))
返回 false,持有者项目 my_recycler_vieww
只是重用它的缓存值,这就是为什么你会得到相同的项目。声明应该是这样的
`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);
}
关于java - 如何在水平 RecyclerView 中基于垂直 RecyclerView 标题设置相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47959459/