在我RecyclerViewAdapter
;当在RecyclerView
列表项的任何布局中进行更改时,它会偶尔重复一次。在RecyclerViewAdapter的内部类ViewHolder
中,我在列表项中设置了名为onClickListener
的布局的mainLayout
。单击此按钮可在同一列表项中展开另一个名为expandableLayout
的布局。点击它时,它不仅会扩展所需的项目,还会扩展11个项目中的每个项目。简单的布局更改也是如此,例如更改文本颜色。
这里是 RecyclerViewAdapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private Context mContext;
private ViewPager mViewPager;
private DataManager mDataManager;
public RecyclerViewAdapter(RecyclerView.LayoutManager layoutManager, Context context, ViewPager viewPager) {
this.mContext = context;
this.mViewPager = viewPager;
this.mDataManager = new DataManager(mContext, mViewPager);
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
FrameLayout mainLayout;
RelativeLayout editButton;
RelativeLayout deleteButton;
ExpandableLayout expandableLayout;
//Defined other View's of the item
ViewHolder(View listItemLayout) {
super(listItemLayout);
this.mainLayout = listItemLayout.findViewById(R.id.listItem_mainLayout);
this.editButton = listItemLayout.findViewById(R.id.expandedLogItem_edit);
this.deleteButton = listItemLayout.findViewById(R.id.expandedLogItem_delete);
this.expandableLayout = listItemLayout.findViewById(R.id.listItem_expandableLayout);
this.mainLayout.setOnClickListener(this);
this.editButton.setOnClickListener(this);
this.deleteButton.setOnClickListener(this);
//Initialized other View's of the item.
}
@Override
public void onClick(View view) {
if (view.getId() == mainLayout.getId()) {
expandableLayout.toggle();
//This expands or collapses not just the clicked item,
//but every item once in 11 items.
//I tried other things like changing the text color
//of a TextView, and the result is the same.
} else if (view.getId() == editButton.getId()) {
mDataManager.editItemAt(getAdapterPosition());
} else if (view.getId() == deleteButton.getId()) {
mDataManager.deleteItemAt(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
}
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View listItemLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
return new ViewHolder(listItemLayout);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
//Populating other View's of the item.
}
@Override
public int getItemCount() {
return logList.size();
}
}
我暂时停留在这个问题上,无法找到或想出任何方法来解决这个问题。任何回应都将不胜感激。
答案 0 :(得分:0)
顾名思义,RecyclerView会在ViewHolders不在屏幕时对其进行回收。因此,您的onBind
方法并不总是使用刚刚膨胀的ViewHolder调用,但可能使用已经更改的方法。例如,向下滚动时,将屏幕保留在顶部的视图会再次添加到底部。
确保在onBind
中设置ViewHolder的所有布局属性,例如布局是否展开。您可能在logList的每个项目中都有一个布尔isExpanded
,并根据onBind