I used android RecyclerView.Adapter. i can not manage my onClickListener.When I do click on item worked and this ithem and another item When I set my itemView
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setVisibility(View.GONE);
}
});
and when do click 'GONE' works to two items. see in photo
this my adapter.
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);
}
});
}
}
}
what can i do to make them work only if we click them?
CodePudding user response:
You can use one list to save all selected indexes and use it in onBindViewHolder to manage visibility of views.
Add index to be removed in list by clicking on view.
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());
}
});
}
}
}