更新回复:
使用 CarItems 列表的替代方法:
@OnClick(...)
public void onButtonClicked() {
int size = ((MyCustomAdapter) mRecyclerView.getAdapter()).getItemCount();
for (int i = 0; i < size; i++) {
if (((MyCustomAdapter) mRecyclerView.getAdapter()).isItemChecked(i)) {
// Get each selected item
CarItem carItem = (MyCustomAdapter) mRecyclerView.getAdapter()).getItem(i);
// Do something with the item like save it to a selected items array.
}
}
}
您还可以向适配器添加getCheckedItems() 方法:
public List<CarItem> getCheckedItems() {
List<CarItem> checkedItems = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
if (isItemChecked(i)) {
checkedItems.add(getItem(i));
}
}
return checkedItems;
}
并像这样从RecyclerView 使用它:
((MyCustomAdapter) mRecyclerView.getAdapter()).getCheckedItems();
有没有办法从RecyclerView的Adapter返回数据
到我设置它的片段?
一般来说,操作片段中的数据:
将以下方法添加到适配器并通过 recyclerView 从片段中调用它们,并将其转换为您的自定义适配器:((MyCustomAdapter) mRecyclerView.getAdapter()).getItem(...);
public void setListItems(List<CarItem> data) {
this.data = data;
}
public List getListItems() {
return data;
}
@Override
public int getItemCount() {
return this.data.size();
}
public CarItem getItem(int position) {
return data.get(position);
}
public void setItem(CarItem item, int position) {
data.set(position, item);
}
我有一个 RecyclerView 在 RecyclerView 的每个对象中我
有一个复选框。每次按下其中一个复选框时,我都会
喜欢在Adapter上插入数据。
管理多选状态和保存状态
阅读this explanation并检查this sample app for multichoice。
This library 还使用 SparseBooleanArray 扩展了用于选择的适配器以保存所选项目。
或者使用这个来保存选中状态,稍后在按下按钮时使用它来只访问选中项的数据:
public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyCustomViewHolder> {
private ArrayList<CarItem> data;
private SparseBooleanArray checkedState = new SparseBooleanArray();
public void setCheckedState(int position, boolean checked) {
checkedState.append(position, checked);
}
public boolean isItemChecked(int position) {
return checkedState.get(position);
}
public SparseBooleanArray getCheckedState() {
return checkedState;
}
public void onBindViewHolder(final MyCustomViewHolder holder, final int position) {
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setCheckedState(holder.getAdapterPosition(), isChecked);
//Here I add the data that I want to store
if (isChecked) {
data.set(holder.getAdapterPosition(), holder.getItem());
} else {
data.set(holder.getAdapterPosition(), null);
}
}
});
}
Reason to use adapter position on this related Google I/O 2016 video:
我创建了 getItem() 方法来获取里面的数据
MyCustomViewHolder 但我不知道如何将数据返回到
我将 Adapter 设置为 RecyclerView 的片段。
向/从视图持有者设置/获取数据
这些方法也可用于获取数据,可能使用 setTag()/getTag() 机制。
viewHolder.itemView.setTag(yourNewData);
/**
* Returns this view's tag.
*
* @return the Object stored in this view as a tag, or {@code null} if not
* set
*
* @see #setTag(Object)
* @see #getTag(int)
*/
@ViewDebug.ExportedProperty
public Object getTag() {
return mTag;
}
/**
* Sets the tag associated with this view. A tag can be used to mark
* a view in its hierarchy and does not have to be unique within the
* hierarchy. Tags can also be used to store data within a view without
* resorting to another data structure.
*
* @param tag an Object to tag the view with
*
* @see #getTag()
* @see #setTag(int, Object)
*/
public void setTag(final Object tag) {
mTag = tag;
}
有一次我完成(按
将适配器设置为 RecyclerView 的 Fragment 上的按钮)
必须返回数据。
我需要额外的信息,了解您在返回数据时尝试执行的操作以及您在此处再次将适配器设置为 recyclerview 的原因。
findViewHolderForAdapterPosition(int)
/**
* Return the ViewHolder for the item in the given position of the data set. Unlike
* {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending
* adapter changes that may not be reflected to the layout yet. On the other hand, if
* {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been
* calculated yet, this method will return <code>null</code> since the new positions of views
* are unknown until the layout is calculated.
* <p>
* This method checks only the children of RecyclerView. If the item at the given
* <code>position</code> is not laid out, it <em>will not</em> create a new one.
*
* @param position The position of the item in the data set of the adapter
* @return The ViewHolder at <code>position</code> or null if there is no such item
*/
public ViewHolder findViewHolderForAdapterPosition(int position) {
if (mDataSetHasChangedAfterLayout) {
return null;
}
final int childCount = mChildHelper.getUnfilteredChildCount();
for (int i = 0; i < childCount; i++) {
final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) {
return holder;
}
}
return null;
}
@OnClick(...)
public void onButtonClicked() {
int size = ((MyCustomAdapter) mRecyclerView.getAdapter()).getItemCount();
for (int i = 0; i < size; i++) {
ViewHolder vh = (MyCustomViewHolder) findViewHolderForAdapterPosition(i);
if (vh != null && ((MyCustomAdapter) mRecyclerView.getAdapter()).isItemChecked(i)) {
Object obj = vh.itemView.getTag();
// Manipulate the data contained in this view holder
// but perhaps would be better to save the data in the CarItem
// and don't manipulate the VH from outside the adapter
}
}
}