Android。回收视图。 onClickLisenet 无法正常工作

Android. RecyclerView . onClickLisenet not work correct

我用了androidRecyclerView.Adapter。我无法管理我的 onClickListener.When 我确实点击了有效的项目并且这个项目和另一个项目 当我设置我的 itemView

itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    view.setVisibility(View.GONE);
                }
            });

什么时候点击 'GONE' 对两个项目起作用。 见照片

这是我的适配器。

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
private List<LessonListActivity.Words> words;


public WordAdapter(List<LessonListActivity.Words> words) {
    this.words = words;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View listItem = layoutInflater.inflate(R.layout.adapter_content, parent, false);
    ViewHolder viewHolder = new ViewHolder(listItem);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final LessonListActivity.Words myListData = words.get(position);
    holder.textView.setText(words.get(position).getEN());


}

@Override
public void setHasStableIds(boolean hasStableIds) {
    super.setHasStableIds(hasStableIds);
}

@Override
public int getItemCount() {
    return words.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView textView;
    public RelativeLayout relativeLayout;


    public ViewHolder(View itemView) {
        super(itemView);
        this.textView = (TextView) itemView.findViewById(R.id.word);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relative_layout);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setVisibility(View.GONE);
            }
        });
    }
}

}

我怎样才能让它们只有在我们点击它们时才起作用?

您可以使用一个列表来保存所有选定的索引,并在 onBindViewHolder 中使用它来管理视图的可见性。

通过单击视图在列表中添加要删除的索引。

 public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
        private List<LessonListActivity.Words> words;
        ArrayList<Integer> listSelected = new ArrayList<>();
    
        public WordAdapter(List<Words> words) {
            this.words = words;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View listItem = layoutInflater.inflate(R.layout.adapter_content, parent, false);
            ViewHolder viewHolder = new ViewHolder(listItem);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final LessonListActivity.Words myListData = words.get(position);
            holder.textView.setText(words.get(position).getEN());
    
            if (listSelected.contains(position)) {
                //Here view will be invisible it will show white space. you can set height 0 to remove white space.
                holder.itemView.setVisibility(View.GONE);
            } else holder.itemView.setVisibility(View.VISIBLE);
    
        }
    
        @Override
        public void setHasStableIds(boolean hasStableIds) {
            super.setHasStableIds(hasStableIds);
        }
    
        @Override
        public int getItemCount() {
            return words.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView textView;
            public RelativeLayout relativeLayout;
    
    
            public ViewHolder(View itemView) {
                super(itemView);
                this.textView = (TextView) itemView.findViewById(R.id.word);
                relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relative_layout);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!listSelected.contains(getLayoutPosition())) {
                            listSelected.add(getLayoutPosition());
                        }
                        notifyItemChanged(getLayoutPosition());
                    }
                });
            }
        }
    }