0
votes

I am new to android and I am using list view with one text view and button.I am performing some visibility stuff where after clicking on button1 it will hide the button1 and show button2.

Set visibility View.GONE and View.VISIBLE is working perfectly but problem is occurring when I scroll my list view magically it is reseting all button1 to View.VISIBLE state.

Please anyone help me to overcome with this problem.

Code:

 @Override
public View getChildView(final int i, final int i1, boolean b, View 
 view, ViewGroup viewGroup) {

    if(view == null) {
        holder =  new ViewHolder();
        LayoutInflater parentInflater = (LayoutInflater) mctx.getSystemService(mctx.LAYOUT_INFLATER_SERVICE);
        view = parentInflater.inflate(R.layout.itemname_child_layout, null);
    } else {
        holder = (ViewHolder) view.getTag();
    }

   holder.button1 = view.findViewById(R.id.button1);
   holder.button1 = view.findViewById(R.id.button2);

   //initial button state
   holder.button1.setVisibility(View.VISIBLE);
   holder.button1.setVisibility(View.GONE);

   holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
      }
   }

   view.setTag(holder);

 return view;
}
2
try to use recylerviewBunny

2 Answers

1
votes

you need to have record of the clicking of button because when you scroll listview it refreshes to the previous stage.

So declare a variable universally in your adapter class like this:

  ArrayList<Integer> count = new ArrayList(); 

Now set list of 0 till the adapter size

    for(int i=0;i<adapter.size;i++){
     count.add(0);
     }

Then after clicking on button1 set your button visibility like this:

              holder.recordBtn.setTag(position);

     if(count.get(position)==0){
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
        }
         else{
         holder.button1.setVisibility(View.VISIBLE);
         holder.button2.setVisibility(View.GONE);
           }

       holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         int pos = (int)view.getTag();
         if(count.get(pos)==0){
         count.set(pos,1);
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
        }
         else{
         count.set(pos,0);
         holder.button1.setVisibility(View.VISIBLE);
         holder.button2.setVisibility(View.GONE);
           }
      }
   }
0
votes

Try this...i have edited your code

 @Override
 public View getChildView(final int i, final int i1, boolean b, View 
 view, ViewGroup viewGroup) {

    if(view == null) {
        holder =  new ViewHolder();
        LayoutInflater parentInflater = (LayoutInflater) mctx.getSystemService(mctx.LAYOUT_INFLATER_SERVICE);
        view = parentInflater.inflate(R.layout.itemname_child_layout, null);
        holder.button1 = view.findViewById(R.id.button1);
        holder.button2 = view.findViewById(R.id.button2);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }


   //initial button state
   holder.button1.setVisibility(View.VISIBLE);
   holder.button2.setVisibility(View.GONE);

   holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
      }
   }


 return view;
}