简体   繁体   English

在 recyclerview 中的嵌套 recyclerview 内的嵌套列表中添加项目

[英]Add items within nested list within nested recyclerview in a recyclerview

I have a recyclerview and within it I am trying to add a nested recyclerview that contains a list.我有一个 recyclerview,在其中我试图添加一个包含列表的嵌套 recyclerview。 Here is what I am trying to make:这是我想要做的: 我想要的输出

This is the closest solution I could find on stack overflow but they use headers instead of left-aligned titles that wrap all other nested views.这是我在堆栈溢出时能找到的最接近的解决方案,但它们使用标题而不是左对齐的标题,这些标题包含所有其他嵌套视图。 How do I create a nested list within a nested recyclerview within a recyclerview?如何在 recyclerview 中的嵌套 recyclerview 中创建嵌套列表?

We need two RecyclerViews and two Adapters which i call inner and outer.inside ViewHolder of outer adapter put innerRecyclerView and inside onBindViewHolder of outer adapter set adapter to innerRecyclerView.i recently developed a test project like this我们需要两个 RecyclerViews 和两个适配器,我称之为inner和outer.inside ViewHolder的外部适配器将innerRecyclerView和外部适配器的onBindViewHolder设置为innerRecyclerView.i最近开发了一个这样的测试项目

here is outerAdapter这是外部适配器

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private List<Category> categories;
private List<Ads> ads_list;
private List<Products> productsList;
private int count;
private int width;

public MainAdapter(Context context, List<Category> categories, List<Ads> ads_list,List<Products> productsList,int width) {
    this.context = context;
    this.categories = categories;
    this.ads_list = ads_list;
    this.productsList=productsList;
    this.width=width;
    //Helper.logDebug("main__adapter",String.valueOf(categories.size()));
    //Helper.logDebug("main_adapter","haminjoori");
}

private class ViewHolder extends RecyclerView.ViewHolder{
    private RecyclerView recyclerView;
    private TextView tv_type,ad_tv_image_address;
    private ImageView iv_ad;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        recyclerView=itemView.findViewById(R.id.recyclerview_horizental);
        recyclerView.setNestedScrollingEnabled(false);
        tv_type=itemView.findViewById(R.id.tv_type);
        iv_ad=itemView.findViewById(R.id.iv_ad);
        ad_tv_image_address=itemView.findViewById(R.id.ad_image_address);
        //Helper.logDebug("main_adapter","haminjoori");
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view= LayoutInflater.from(context).inflate(R.layout.recycler_layout,viewGroup,false);
    //Helper.logDebug("main_adapter","haminjoori");
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    ViewHolder holder= (ViewHolder) viewHolder;
    try {
        String banner_url=ads_list.get(i).getImage();
        holder.ad_tv_image_address.setText(banner_url);
        Picasso.get().load(context.getString(R.string.base_url_banner)+banner_url).into(holder.iv_ad);
    }catch (Exception ex){
        holder.ad_tv_image_address.setText("");
    }
    Helper.logDebug("main_adapter",holder.ad_tv_image_address.getText().toString());
    holder.tv_type.setText(categories.get(i).getName());
    SecondAdapter secondAdapter=new SecondAdapter(categories.get(i).getProducts(),context,width);
    LinearLayoutManager layoutManager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);

    holder.recyclerView.setLayoutManager(layoutManager);
    holder.recyclerView.setAdapter(secondAdapter);



}

@Override
public int getItemCount() {
    return categories.size();
}
}

and here is inner adapter这是内部适配器

public class SecondAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private int count;
private List<Products> productsList;
private List<Category> categories;
private int width;

public SecondAdapter(List<Products> productsList, Context context,int width) {
    this.productsList = productsList;
    this.context = context;
    this.width=width;
}

private class ViewHolder extends RecyclerView.ViewHolder {
    private TextView market_price,our_price,name,unit,min_amount,image;
    private ImageView imagee;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        itemView.getLayoutParams().width=width/2;


        market_price=itemView.findViewById(R.id.product_market_price);
        our_price=itemView.findViewById(R.id.product_our_price);
        name=itemView.findViewById(R.id.fruit_name);
        unit=itemView.findViewById(R.id.fruit_unit);
        image=itemView.findViewById(R.id.product_image_url);
        imagee=itemView.findViewById(R.id.iv_rec);
        min_amount=itemView.findViewById(R.id.product_min_amount);

        Log.d("second_adapter","haminjoori");
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view= LayoutInflater.from(context).inflate(R.layout.recycler2_layout,viewGroup,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    ViewHolder holder= (ViewHolder) viewHolder;
    Helper.logDebug("second_adapter","haminjori");

    holder.name.setText(productsList.get(i).getName());
    holder.unit.setText(productsList.get(i).getUnit());
    holder.market_price.setText(String.valueOf(productsList.get(i).getMarketPrice()));
    holder.our_price.setText(String.valueOf(productsList.get(i).getOurPrice()));
    Helper.logDebug("secondAdapter",productsList.get(i).getImage());
    String address=productsList.get(i).getImage();
    address=address.substring(6);
    holder.image.setText(address);
    String full_address=context.getString(R.string.base_url_image)+address;
    Helper.logDebug("secondAdapter",full_address);
    Picasso.get().load(full_address).into(holder.imagee);
    holder.min_amount.setText(String.valueOf(productsList.get(i).getMin_amount()));
}

@Override
public int getItemCount() {

    Helper.logDebug("second_adapter",String.valueOf(productsList.size()));
    return productsList.size();
}
}

outer view adapter xml外部视图适配器 xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools"         
android:layout_height="wrap_content" android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android">

  <TextView android:layout_height="wrap_content" 
  android:layout_width="wrap_content" android:text="نوع میوه" 
  android:layout_marginBottom="4dp" android:layout_marginTop="4dp" 
  android:layout_centerHorizontal="true" style="@style/main_titles" 
  android:id="@+id/tv_type"/>

  <ImageView android:layout_height="wrap_content" 
  android:layout_width="wrap_content" 
  android:src="@drawable/ic_keyboard_arrow_right_black_24dp" 
  android:layout_alignParentRight="true"/>

  <android.support.v7.widget.RecyclerView 
  android:layout_height="wrap_content" android:layout_width="match_parent" 
  android:id="@+id/recyclerview_horizental" 
  android:layout_below="@id/tv_type" 
  app:layoutManager="android.support.v7.widget.LinearLayoutManager" 
  android:orientation="horizontal" 
  tools:listitem="@layout/recycler2_layout"/>

  <TextView android:layout_height="wrap_content" 
  android:layout_width="wrap_content" android:id="@+id/ad_image_address" 
  android:visibility="gone"/>

  <ImageView android:layout_height="80dp" android:layout_width="match_parent" 
  android:id="@+id/iv_ad" 
  android:layout_below="@id/recyclerview_horizental"/>

  </RelativeLayout>

inner view adapter xml内部视图适配器 xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal" 
android:layout_height="wrap_content" android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_height="wrap_content" 
android:layout_width="0dp" android:layout_weight="1" 
android:id="@+id/rel_layout">

<ImageView android:layout_height="200dp" android:layout_width="match_parent" 
android:id="@+id/iv_rec"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/fruit_name" 
android:textColor="@color/semi_black" android:textSize="14sp" 
android:layout_marginRight="8dp" android:layout_below="@id/iv_rec" 
android:layout_alignParentRight="true" android:text="نام میوه"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/fruit_unit" 
android:textColor="@color/black_white" android:textSize="12sp" 
android:layout_below="@id/iv_rec" android:text="واحد" 
android:layout_alignParentLeft="true" android:layout_marginLeft="8dp"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/product_market_price" 
android:textColor="@color/semi_black" android:textSize="12sp" 
android:layout_below="@id/fruit_unit" android:text="قیمت مارکت" 
android:layout_alignParentLeft="true" android:layout_marginLeft="8dp"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/product_our_price" 
android:textColor="@color/semi_black" android:textSize="12sp" 
android:layout_below="@id/product_market_price" android:text="قیمت ما" 
android:layout_alignParentLeft="true" android:layout_marginLeft="8dp"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/product_min_amount" 
android:textColor="@color/semi_black" android:textSize="12sp" 
android:layout_below="@id/product_market_price" 
android:layout_alignParentLeft="true" android:layout_marginLeft="8dp" 
android:visibility="gone"/>

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" android:id="@+id/product_image_url" 
android:textColor="@color/semi_black" android:textSize="12sp" 
android:layout_below="@id/product_market_price" 
android:layout_alignParentLeft="true" android:layout_marginLeft="8dp" 
android:visibility="gone"/>

</RelativeLayout>

<RelativeLayout android:layout_height="wrap_content" 
android:layout_width="0dp" android:layout_weight="1" 
android:visibility="gone"> </RelativeLayout>

</LinearLayout>

final result will be something like this...(this is something similar,not exactly what xml file is)最终结果将是这样的......(这是类似的东西,不完全是什么xml文件)

在此处输入图片说明

categories are outer RecyclerView and fruits are inner RecyclerView类别是外部RecyclerView ,水果是内部RecyclerView

ask me if you have problem implementating this...问我你在实现这个方面是否有问题......

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

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