How to set button visibility in a custom ListView?

I have a custom ListView with button , so when I click on button it should disappear and it disappears, but when I scroll down and return, button appears again and the other button disappears. How can I make a particular button disappear when clicked .... I use this code

 holder.checkbox = (Button) view.findViewById(R.id.checkBox1); holder.checkbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.checkbox.setVisibility(View.INVISIBLE); 
0
source share
7 answers

In ListView, when an item exits the screen, ListView destroys the item for efficiency, and when scrolling back to that item, it recreates it by calling the adapter's getView () method, so you need to save an array that has a logical value for the button status, which by default will be appear as visible, and in getView () you just place the check, getting the logical status value from the logical array, if true, then the element will be visible, otherwise the element is invisible. Therefore, when you disappear an element, you must set the status of the element to false as well. I assume this is the code in your getView (), and you have already defined your boolean array with the same length as the ListView elements, and true with the name "yourButtonStatusBooleanArray", after which the following changes will work for you.

 holder.checkbox = (Button) view.findViewById(R.id.checkBox1); if(!yourButtonStatusBooleanArray[position]) { holder.checkbox.setVisibility(View.INVISIBLE); } holder.checkbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.checkbox.setVisibility(View.INVISIBLE); yourButtonStatusBooleanArray[position]=false; 
+2
source

Try adding holder.checkbox.setVisibility(View.VISIBLE); after button announcement

+1
source

This is due to the recirculation of the species. What you need to do is maintain the say booleans array and each time the button is clicked, it switches to the corresponding boolean value. Then, in your getview, check the corresponding position of the boolean array and set the state of the button.

See link for more details.

checkbox in listview does not work properly

the above example for the checkbox edit your code.

+1
source

The problem is that the row view is reused, and therefore, after scrolling in the row view, a button with the state of another row may be displayed. You can solve this problem by explicitly setting the visibility of your button when initializing the view. Therefore, you will need to remember the state of the string somewhere, for example. in your data object or in an array that tracks already selected items.

 holder.checkbox.setVisibility(dataObject.getVisibilityState()); 

EDIT Reply to comment shown here to have blocks of code.

you need to implement the method yourself, the code was just an example of how you can use your own method. That way you can do something similar in your class. Grade:

  public class Item { private int visibility; ... other attributes of your item public int getVisibilityState(){ return visibility; } public void setVisible(){ visibility = View.VISIBLE; } public void setInvisible(){ visibility = View.INVISIBLE; } } 
+1
source

You need to implement this method in your ListAdapter, which is called every time your view of the line goes from screen to screen because the user scrolls the list. The view passed to your getView method can actually be used to display data for a completely different row. This is due to the fact that objects are processed - if you have data for 1000 rows, but only 8 views are suitable on the screen, the system creates 8 View objects for your rows, not 1000.

If your class implements a ListAdapter, you can override the getView method as follows:

 public class MyClass implements ListAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; //Create a view for this row if none exists yet if (rowView == null) { LayoutInflater inflater = (LayoutInflater) _context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); rowView = inflater.inflate(R.layout.YOUR_ROW_LAYOUT, parent, false); //make the button go away if it should not be visible if (buttonShouldBeNotVisible(convertView)) { //Your code should determine this convertView.checkbox.setVisibility(View.INVISIBLE); //Use View.GONE to make it take up no space } return convertView 

}}

+1
source

Views are returned to the list. You will need to save the state of the button in some data structure and install it in the getView(int, View, ViewGroup) adapters getView(int, View, ViewGroup) .

 private HashMap<Integer, Boolean> mButtonMap = new HashMap<Integer, Boolean>(); [...] @Override public View getView(int position, View convertView, ViewGroup parent) { View retVal; // Do all your stuff with each row if (!mButtonMap.containsKey(new Integer(position))) { button.setVisibility(View.VISIBLE); } else { button.setVisibility(View.INVISIBLE); } button.setTag(""+position); return retVal; } //In your onClickListener onClick(View v) { [...] int position = Integer.parseInt((String) v.getTag()); mButtonMap.put(position, true); button.setVisibility(View.INVISIBLE); } 
+1
source

You can do something like this:

 public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if (convertView == null) { vi = inflater.inflate(R.layout.grid_adapter, null); holder = new ViewHolder(); holder.checkbox = (Button) view.findViewById(R.id.checkBox1); holder.checkbox.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (arg0.getTag().equals("0")) { arg0.setVisibility(View.INVISIBLE); arg0.setTag("1"); } notifyDataSetChanged(); } }); vi.setTag(holder); } else holder = (ViewHolder)vi.getTag(); try { if (holder.checkbox.getTag().equals("1")) { holder.checkbox.setVisibility(View.INVISIBLE); } else { holder.checkbox.setVisibility(View.VISIBLE); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return vi; } 
+1
source

Source: https://habr.com/ru/post/945553/


All Articles