在recyclerview的嵌套recyclerview中的嵌套列表中添加项目
Posted
技术标签:
【中文标题】在recyclerview的嵌套recyclerview中的嵌套列表中添加项目【英文标题】:Add items within nested list within nested recyclerview in a recyclerview 【发布时间】:2020-05-31 05:59:29 【问题描述】:我有一个 recyclerview,我试图在其中添加一个包含列表的嵌套 recyclerview。这是我想要做的:
This is the closest solution I could find 堆栈溢出,但它们使用标题而不是左对齐标题,这些标题包含所有其他嵌套视图。如何在 recyclerview 内的嵌套 recyclerview 中创建嵌套列表?
【问题讨论】:
我们需要两个recyclerViews和两个适配器,我称之为inner和outter.inside外部适配器的ViewHolder放置innerRecyclerview和内部的外部适配器的onBindViewHolder设置适配器到innerRecyclerView @SalarArabpour 这听起来是个不错的解决方案。你认为我对这个想法的处理方式会更好吗?我觉得我正在考虑使用太多的回收器视图来获得我想要的东西。我只是想在固定项目的主列表中制作一个动态的项目列表。 我可以向您保证,使用两个 recyclerviews 是最好的选择。明天当我访问我的笔记本电脑时,我将向您发布示例代码! @SalarArabpour 你太棒了! 【参考方案1】:我们需要两个 RecyclerViews 和两个 Adapters 我称之为 inner 和 outer.inside ViewHolder 的外部适配器放入 innerRecyclerView 和内部的外部适配器设置适配器的 onBindViewHolder 到 innerRecyclerView.i 最近开发了一个这样的测试项目
这里是外部适配器
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();
这是内部适配器
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();
外部视图适配器 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_ android:layout_
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_
android:layout_ 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_
android:layout_
android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
android:layout_alignParentRight="true"/>
<android.support.v7.widget.RecyclerView
android:layout_ android:layout_
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_
android:layout_ android:id="@+id/ad_image_address"
android:visibility="gone"/>
<ImageView android:layout_ android:layout_
android:id="@+id/iv_ad"
android:layout_below="@id/recyclerview_horizental"/>
</RelativeLayout>
内部视图适配器 xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal"
android:layout_ android:layout_
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_
android:layout_ android:layout_weight="1"
android:id="@+id/rel_layout">
<ImageView android:layout_ android:layout_
android:id="@+id/iv_rec"/>
<TextView android:layout_
android:layout_ 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_
android:layout_ 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_
android:layout_ 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_
android:layout_ 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_
android:layout_ 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_
android:layout_ 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_
android:layout_ android:layout_weight="1"
android:visibility="gone"> </RelativeLayout>
</LinearLayout>
最终结果会是这样的......(这是类似的东西,不完全是xml文件)
类别是外部RecyclerView
,水果是内部RecyclerView
如果您在执行此操作时遇到问题,请询问我...
【讨论】:
太棒了!我让它在android studio上工作。只有一个问题:您如何使类别将产品列表包装在左侧而不是作为标题?我能够使内部 recyclerview 垂直,现在正在尝试将类别标题垂直和水平居中到产品列表的左侧 最简单的方法是将标题TextView 包装在RelativeLayout 中。带有 xml 属性的 TextView android:centerVertical="true" 我可以看看你的 xml 布局吗? 我也添加了 xml 布局,但请记住,屏幕截图是相似的,而不是 xml 代码,我只是无法构建项目并从该客户的网站上找到了类似的屏幕截图 谢谢面包,你可以在点击内部适配器时做到这一点。看看这个例子...holder.btnAction.setOnClickListener(new View.OnClickListener() @Override public void onClick(View查看) list.remove(position); notifyDataSetChanged(); );以上是关于在recyclerview的嵌套recyclerview中的嵌套列表中添加项目的主要内容,如果未能解决你的问题,请参考以下文章