如何在 RecyclerView 中制作动态微调器?
Posted
技术标签:
【中文标题】如何在 RecyclerView 中制作动态微调器?【英文标题】:How to make Dynamic Spinner inside RecyclerView? 【发布时间】:2021-09-13 02:50:11 【问题描述】:我将创建一个 Dynamic CardView,其中包含 TextView (Item Name)、Spinner 等组件strong>(活动选择)和EditText(静态备注)。
我已经为数组中的每个列表设置/获取了 Item Name,但是对于 Spinner Item 我遇到了错误。
请参阅下文以供参考。谢谢!
1. FormActivity.java
private void buildRecyclerView()
mRecyclerView = findViewById(R.id.rv_item); //RecyclerView
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ItemAdapter(mItemList, this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mItemList,我要过去到 ItemAdapter.class
"item_id": 1,
"location_id": 1,
"item_name": "Credit Back Door",
"value": [
"A",
"B",
"C"
]
,
"item_id": 2,
"location_id": 1,
"item_name": "2F Admin Bridgeway",
"value": [
"D",
"E",
"F"
]
2. form_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemCard"
android:layout_
android:layout_
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="8dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relativeLayout2"
android:layout_
android:layout_
android:layout_margin="8dp"
android:padding="10dp"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="4dp">
<TextView
android:id="@+id/txt_item"
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:text="1. Item"
android:textAllCaps="false"
android:textColor="#327034"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="10dp" />
<Spinner
android:id="@+id/dd_activity"
android:layout_
android:layout_
android:layout_marginTop="34dp"
android:animateLayoutChanges="false"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="9dp" />
<EditText
android:id="@+id/txt_remarks"
android:layout_
android:layout_
android:layout_marginTop="68dp"
android:hint="Remarks"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="9dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
2.2 动态 CardView 用户界面 Please click here to see the picture
3. ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>
public ArrayList<ItemParams> mItemList;
public Context Context;
public ItemAdapter(ArrayList<ItemParams> itemList, Context context)
mItemList = itemList;
Context = context;
public static class ItemViewHolder extends RecyclerView.ViewHolder
public TextView txt_item;
public Spinner dd_activity;
public EditText txt_remarks;
public CardView itemCard;
public ItemViewHolder(View itemView)
super(itemView);
itemCard = itemView.findViewById(R.id.itemCard);
txt_item = itemView.findViewById(R.id.txt_item);
dd_activity = itemView.findViewById(R.id.dd_activity);
txt_remarks = itemView.findViewById(R.id.txt_remarks);
@NonNull
@Override
public ItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
ItemAdapter.ItemViewHolder cvh = new ItemAdapter.ItemViewHolder(v);
//SPINNER ITEM SAMPLE BUT IT WILL BE A DYNAMIC FROM ONE OF THE LIST
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(parent.getContext(),R.layout.form_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cvh.dd_activity.setAdapter(dataAdapter);
return cvh;
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemViewHolder holder, int position)
ItemParams mFormItem = mItemList.get(position);
holder.txt_item.setText(mFormItem.getItem_name());
@Override
public int getItemCount()
return mItemList.size();
错误:
2021-07-01 13:28:34.645 7337-7337/com.unilab.healthcheck E/RecyclerView: No adapter attached; skipping layout
2021-07-01 13:28:35.050 7337-7337/com.unilab.healthcheck E/ArrayAdapter: You must supply a resource ID for a TextView
2021-07-01 13:28:35.053 7337-7337/com.unilab.healthcheck E/AndroidRuntime: FATAL EXCEPTION: main Process: com.unilab.healthcheck, PID: 7337 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
【问题讨论】:
那么,微调器的错误是什么? 嗯,用构造函数也显示 ItemAdapter.ItemViewHolder 类。 您在 ArrayAdapter 之前创建 ItemViewHolder,这样新的 ItemHolder 将没有 ArrayAdapter。 @Matheus 请查看更新后的代码。谢谢! @Zain 请查看更新后的帖子。谢谢! 【参考方案1】:由于您不必在适配器中初始化数组,而是可以在活动或片段中创建列表,无论您拥有什么, 你可以试试这个 sn-ps(参考来自你的代码)
->首先您必须将此代码添加到活动或片段中
//SPINNER ITEM SAMPLE
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
recycleview.setAdapter(new ItemAdapter(getActivity(),list));
-> 然后
package com.brnd.pos.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>
Context context;
List<String> list;
public ItemAdapter(Context context, List<String> list)
this.context = context;
this.list = list;
@NonNull
@Override
public ItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
return new ItemAdapter.ItemViewHolder(v);
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemViewHolder holder, int position)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(parent.getContext(), R.layout.form_item, R.id.dd_activity, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cvh.dd_activity.setAdapter(dataAdapter);
ItemParams mFormItem = mItemList.get(position);
holder.txt_item.setText(mFormItem.getItem_name());
@Override
public int getItemCount()
return list.size();
//below viewholder is just for your reference you have to find view id's
public static class ItemViewHolder extends RecyclerView.ViewHolder
public ItemViewHolder(@NonNull View itemView)
super(itemView);
【讨论】:
感谢您的回复。后续问题,如果微调项的值取决于传递给 ItemAdapter 的列表怎么办?请查看更新后的帖子。再次感谢您。【参考方案2】:FormActivity.java
private void buildRecyclerView()
mRecyclerView = findViewById(R.id.rv_item); //RecyclerView
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new ItemAdapter(mItemList,getApplicationContext()));
ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>
public ArrayList<ItemParams> mItemList;
public Context context;
public ItemAdapter(ArrayList<ItemParams> itemList, Context context)
this.mItemList = itemList;
this.context = context;
public static class ItemViewHolder extends RecyclerView.ViewHolder
public TextView txt_item;
public Spinner dd_activity;
public EditText txt_remarks;
public CardView itemCard;
public ItemViewHolder(View itemView)
super(itemView);
itemCard = itemView.findViewById(R.id.itemCard);
txt_item = itemView.findViewById(R.id.txt_item);
dd_activity = itemView.findViewById(R.id.dd_activity);
txt_remarks = itemView.findViewById(R.id.txt_remarks);
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
return new ItemViewHolder(v);
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position)
//SPINNER ITEM SAMPLE BUT IT WILL BE A DYNAMIC FROM ONE OF THE LIST
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
holder.dd_activity.setAdapter(
new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, list));
holder.txt_item.setText(mItemList.get(position).getItem_name());
@Override
public int getItemCount()
return mItemList.size();
但我相信错误在 xml 中:
android.R.layout.simple_spinner_dropdown_item
如果错误仍然出现,请尝试切换到类似于此的 XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_dropdown_textView"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:padding="15dp"
android:text="@string/texte"
android:textAlignment="center"
android:textSize="18sp"></TextView>
【讨论】:
以上是关于如何在 RecyclerView 中制作动态微调器?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 doInBackgroung() 时制作 android AsyncTask 加载微调器
如何通过微调器过滤 FirebaseRecyclerOptions 中的数据?
如何在 React Native 中制作微调器?我想通过 PanResponder 旋转框