我有一个回收器 View ,在其中我尝试添加一个包含列表的嵌套回收器 View 。这是我想做的:
This is the closest solution I could find堆栈溢出,但它们使用标题而不是包装所有其他嵌套 View 的左对齐标题。如何在回收器 View 中的嵌套回收器 View 中创建嵌套列表?
最佳答案
我们需要两个RecyclerView和两个适配器,我称之为inner和outer。外部适配器的inside ViewHolder将innerRecyclerView和外部适配器的onBindViewHolder设置为innerRecyclerView。我最近开发了一个这样的测试项目
这是外部适配器
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();
}
}
外部 View 适配器 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>
内部 View 适配器 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>
最终结果将是这样的......(这是类似的东西,不完全是xml文件)
类别是外部RecyclerView
,水果是内部RecyclerView
如果您在实现此操作时遇到问题,请询问我...
关于java - 在回收器 View 中的嵌套回收器 View 内的嵌套列表中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243208/