There are two layouts in the Recycler view, one of which is Visibility.Gone and click on the layout shown, and the layout that is Gone is Visibility.I'm trying to make it visible. When I click the layout on the adapter, I coded it so that the other layout could be seen, but when I click it, it appears for a while and disappears again. ㅜ 왜 Why is that?Yo
boolean istrue = true;
holder.llm2.setVisibility(View.GONE);
holder.llm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(istrue == true) {
new SubAsyncTask(position).execute("my url");
holder.llm2.setVisibility(View.VISIBLE);
istrue = false;
} } else if(istrue == false) {
new SubAsyncTask(position).execute("my url");
holder.llm2.setVisibility(View.INVISIBLE);
istrue = true;
}
}
});
}
Recycler views reuse item views, so if you want to control the visibility of the view, you must store the information in a separate model, get the information from the model, and set the visibility.
For example, suppose an item view has a text view, and when you click the button, you change the text. If you scroll through the Recycler View, the view will be reused and the intended screen will not appear. This is because the text has only changed the properties of the view, and it is reused by recycling views.
Just as the information to be displayed in the view is stored in each item of the Adapter, save the Visibility property separately and set the value in the onBindViewHolder()
function.
In the comments, code blocks are not available, so I wrote additional answers.
When scrolling, the layout disappears or a view that you did not click appears because the view is reused. In other words, the view of index 1 can be reused in index 10 when scrolling. At this time, if number 1 was in the Gone state, 10 views would be in the Gone state. (The actual status of the 10th model is not Gone)
Therefore, you must set the setVisibility() according to the visibility value of the model as shown in the following code to act as expected.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
...
holder.view.setVisibility(item.visibility ? View.Visible : View.Gone);
...
}
© 2024 OneMinuteCode. All rights reserved.