如何避免在RecyclerView中重复项目的布局更改?

时间:2017-12-12 10:36:35

标签: android android-recyclerview recycler-adapter

在我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();
    }
}

我暂时停留在这个问题上,无法找到或想出任何方法来解决这个问题。任何回应都将不胜感激。

1 个答案:

答案 0 :(得分:0)

顾名思义,RecyclerView会在ViewHolders不在屏幕时对其进行回收。因此,您的onBind方法并不总是使用刚刚膨胀的ViewHolder调用,但可能使用已经更改的方法。例如,向下滚动时,将屏幕保留在顶部的视图会再次添加到底部。

确保在onBind中设置ViewHolder的所有布局属性,例如布局是否展开。您可能在logList的每个项目中都有一个布尔isExpanded,并根据onBind

中的设置扩展状态