我在Adapter文件中有一个CardView项目的RecyclerView列表和一个onClickListener(),通过onItemClick()在MainActivity中使用它来启动CardViewDetails活动。一切正常。然后我添加了一个Spinner来根据一个标准过滤CardViews。过滤按预期工作。但是,现在当我点击过滤列表中的CardView时,CardViewDetails活动中向用户显示了错误的CardView。
例如,假设我在RecyclerView列表中有15个CardView项目。并假设我在Spinner下拉列表中进行选择,将列表过滤为三个CardView。假设我现在点击过滤列表中的第二个CardView,并且CardView项目是原始RecyclerView列表中的最后 CardView。用户界面按预期进入CardViewDetails活动,但返回原始列表中的第二个CardView,而不是原始列表中我预期的最后一个CardView。
在使用微调器选择过滤列表后适配器更新时,如何使用原始RecyclerView列表返回ViewHolder中的原始适配器位置?我应该制作原始列表的副本,并以某种方式使用适配器文件中的复制列表吗?
public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public List<ListItem> listItems;
public Context context;
private RecyclerItemClickListener recyclerItemClickListener;
public MyRecylerAdapter(Context context) {
this.context = context;
this.listItems = new ArrayList<>();
}
public Context getContext() {
return context;
public void setOnItemClickListener(RecyclerItemClickListener recyclerItemClickListener) {
this.recyclerItemClickListener = recyclerItemClickListener;
}
// Do R. list filtering from MainActivity.
public ArrayList<ListItem> filter2(List<ListItem> listItems, String filterType) {
final ArrayList<ListItem> filteredModelList2 = new ArrayList<>();
for (ListItem item : listItems) {
final String cardTypeTest = item.getType();
if (cardTypeTest.equals(filterType)) {
filteredModelList2.add(item);
}
}
return filteredModelList2;
}
public void setFilter(ArrayList<ListItem> listItem) {
listItems = new ArrayList<>();
listItems.addAll(listItem);
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent,false);
final ItemHolder itemHolder = new ItemHolder(view);
// Attach a Click listener to the items's (row) view.
// onItemClick is the click method in MainActivity.
itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
// Handles the row being clicked.
@Override
public void onClick(View view) {
int adapterPos = itemHolder.getAdapterPosition(); // get the item position.
if (adapterPos != RecyclerView.NO_POSITION) {
if (recyclerItemClickListener != null) {
recyclerItemClickListener.onItemClick(itemHolder.itemView,adapterPos);
}
}
}
});
return itemHolder;
}
RecyclerItemClickListener.java
public interface RecyclerItemClickListener {
void onItemClick(View view, int position);
}
MainActivity
public class MainActivity extends AppCompatActivity implements
RecyclerItemClickListener {
private MyRecylerAdapter adapter;
private List<ListItem> allList = null;
private Spinner spinner2;
private ArrayList<ListItem> filteredModelList0 = null;
...
// Set up a spinner instance and apply an OnItemSelectedListener on it.
spinner2 = (Spinner) findViewById(R.id.skycard_filter);
final String[] spinnerItems2 = getResources().getStringArray(R.array.skycard_filters);
// Create an ArrayAdapter using the string array and a default spinner layout
final CustomAdapter<String> adapter2 = new CustomAdapter<>
(this,android.R.layout.simple_spinner_item, spinnerItems2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
final String type = "CardView Type";
filteredModelList0 = adapter.filter2(adapter.listItems,type);
...
switch (position) {
case 0:
adapter.clear();
adapter.addAll(allList);
// Need to filter the entire R. list
filteredModelList0 = adapter.filter2(adapter.listItems, type);
adapter.setFilter(filteredModelList0);
break;
...
}
@Override
public void onItemClick(View view,int position) {
// Create a new intent to send data from this MainActivity to the CardViewDetails Activity.
Intent intent = new Intent(this,CardViewDetails.class);
// Send the position of the CardView item that was clicked on through
// the intent.
intent.putExtra("position",position);
startActivity(intent);
finish();
}