1

i have got this problem in my code, that when a make a transition on one button in viewholder which is done in Onclicklistener, multiple transition occurs on different rows i.e if i click on button in row 1 then the button moves right which is expected, but button in row 6 also moves right which is not expected. i have checked that the transition ID of button in Row 1 is same as Button is Row 6 i don't know, what is wrong with my code. please help. check my code.

public class UserCustomAdapter extends ArrayAdapter<User> {                                                                                                                                                                                        
Context context;                                                                                                                                                                                                                               
int layoutResourceId;                                                                                                                                                                                                                          
ArrayList data = new ArrayList();                                                                                                                                                                                                              
String weekDay;                                                                                                                                                                                                                                
AlertDialog alert;                                                                                                                                                                                                                             

public UserCustomAdapter(Context context, int layoutResourceId,                                                                                                                                                                                
                         ArrayList<User> data, String weekDay) {                                                                                                                                                                               
    super(context, layoutResourceId, data);                                                                                                                                                                                                    
    this.layoutResourceId = layoutResourceId;                                                                                                                                                                                                  
    this.context = context;                                                                                                                                                                                                                    
    this.data = data;                                                                                                                                                                                                                          
    this.weekDay = weekDay;                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                              

@Override                                                                                                                                                                                                                                      
public View getView(final int position, final View convertView, final
ViewGroup parent) {                                                                                                                                                      

    View row = convertView;                                                                                                                                                                                                                    
    final UserHolder holder;                                                                                                                                                                                                                   

if (row == null) {    

    LayoutInflater inflater1 = ((Activity) context).getLayoutInflater();

        row = inflater1.inflate(layoutResourceId, parent, false);                                                                                                                                                                              
        holder = new UserHolder();                                                                                                                                                                                                             

        holder.subject = (TextView) row.findViewById(R.id.subject);                                                                                                                                                                                                   
   holder.percentage = (TextView) row.findViewById(R.id.percentage_edit);                                                                                                                                                
        holder.attended = (Button) row.findViewById(R.id.attended);                                                                                                                                                                            
        holder.bunked = (Button) row.findViewById(R.id.bunked);                                                                                                                                                                                
        holder.other = (Button) row.findViewById(R.id.other);                                                                                                                                                                                  

        row.setTag(holder);                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                          
    else {                                                                                                                                                                                                                                     
        holder = (UserHolder) row.getTag();                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                          

    //to set the text into the fields                                                                                                                                                                                                          
    User user = (User) data.get(position);                                                                                                                                                                                                     

    holder.subject.setText(user.getName());                                                                                                                                                                                                    
    holder.percentage.setText(user.getAddress());                                                                                                                                                                                              

    holder.attended.setId(position);                                                                                                                                                                                                           
    holder.bunked.setId(position);                                                                                                                                                                                                             
    holder.other.setId(position);                                                                                                                                                                                                              

 holder.attended.setOnClickListener(new View.OnClickListener() {                                                                                                                                                                            

        @Override                                                                                                                                                                                                                              
        public void onClick(View v) {                                                                                                                                                                                                          


                // FOR ANIMATIONS OF BUTTONS                                                                                                                                                                                                   
                TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.28f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);                        
                translate.setDuration(600);                                                                                                                                                                                                    
                holder.attended.startAnimation(translate);                                                                                                                                                                                     
                Log.d("trans", holder.attended + "");                                                                                                                                                                                          
                translate.setFillAfter(true);                                                                                                                                                                                                  

                TranslateAnimation translate1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);                        
                translate1.setDuration(600);                                                                                                                                                                                                   
                holder.other.startAnimation(translate1);                                                                                                                                                                                       
                translate1.setFillAfter(true);                                                                                                                                                                                                 

                TranslateAnimation translate2 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);                        
                translate2.setDuration(600);                                                                                                                                                                                                   
                holder.bunked.startAnimation(translate2);                                                                                                                                                                                      
                translate2.setFillAfter(true);                                                                                                                                                                                                 

        }                                                                                                                                                                                                                                      

    });                                                                                                                                                                                                                                        
          return row;
}
 static class UserHolder {
    TextView subject;
    TextView percentage;
    Button attended;
    Button bunked;
    Button other;
}
}                                                                                                                                                                        
  • you probably need to store the anim state like in an array (on/off) and if the anim is on then perform the animation at that position else do nothing. you can do the if check just after (User user = (User) data.get(position); ) – Tasos Jan 03 '16 at 17:34
  • can you please elaborate,how do i add an array after (User user = (User) data.get(position) – Satyaraj Moily Jan 03 '16 at 17:45
  • you already have an array for the list items right? – Tasos Jan 03 '16 at 17:48
  • yes i do have. but i don't understand what is that gonna effect the transition – Satyaraj Moily Jan 03 '16 at 17:59
  • can you type the code to store the anim state in a array please – Satyaraj Moily Jan 03 '16 at 18:05
  • anything you change for an item let say an icon from green to yellow, you find that it will change it further down the list too. let say you click on item 1 on the list to change the icon it will change it for 6, 12, 18. i used to know why that happened but i forgot since then, the way i resolved it is by adding (state) as an extra item in the array and set to (off). Thats for all items in the array. Then in your case at the end of the animation code you set the state to (on) like this (user.setState("true");). cont -- – Tasos Jan 03 '16 at 18:09
  • now after (User user = (User) data.get(position); ) you add (String state = user.getState(); if (Objects.equals(state, "true")) { //perform anim } else { //dont perform anim } ) -- something like that, but in your case its animations so you need deal will the (else) part on how to stop the animation performing and do nothing if true as you only need to perform the anim once. as you do that on your onclicklistener part – Tasos Jan 03 '16 at 18:15
  • i understood your concept, i was trying to figure out the whole night,but i have observed one more problem, that the button in row 1 after the transition if i scroll down and scroll up many time,the buttons in other rows will also transit like row 2,row 3. is it because i do the transition inside the onclicklistener? is the a better way to do it? or is my code totally wrong? – Satyaraj Moily Jan 04 '16 at 09:48
  • no your code looks ok. try this way as i have setup my button listener in the adapter --- remove (holder.attended.setOnClickListener(new View.OnClickListener() {) -- and add -- (final Button btn = (Button) row.findViewById(R.id.attended); btn.setTag(position); btn.setOnClickListener(new View.OnClickListener() {) and remove this also (holder.attended = (Button) row.findViewById(R.id.attended);) – Tasos Jan 04 '16 at 10:10
  • nope,that didn't solve the problem. – Satyaraj Moily Jan 04 '16 at 10:52
  • the transition keeps moving even of different rows when i scroll up and down.its not constant – Satyaraj Moily Jan 04 '16 at 10:54
  • you have the if else check as i mentioned above -- in the else -- check here on how to stop animations -- http://stackoverflow.com/questions/4112599/how-to-stop-an-animation-cancel-does-not-work – Tasos Jan 04 '16 at 10:56
  • so the problem is solved. thank you so much for that. but there is another problem i.e. the button in row 1 moves left to right if i click on it as expected but when i scroll down and scroll back up its shows the transition of the row 1 button again which moves left to right. and i keep scrolling up and down. it transits every time. but it has to stay in its position every time i scroll. i know why does this happen, it because as you told me to add an if check after ( final User user = (User) data.get(position);) so every time i scroll the transit occurs. – Satyaraj Moily Jan 04 '16 at 12:31
  • so how to i fix the button to its position, and doesnt effect when i scroll – Satyaraj Moily Jan 04 '16 at 12:35
  • yeah i had a feeling that will happen. as i mentioned in a comment, because its animination and not turning an icon from green to red which is instant the animation has some timing. maybe you should, in the (if (Objects.equals(state, "true")) stop the animation and reanimate without any timing so its instant. (translate.setDuration(0); ) – Tasos Jan 04 '16 at 12:41
  • OMG!!! atlast the problem solved. was stuck over this for months. you were a saviour. thank you so much for the help. awesome mahn. – Satyaraj Moily Jan 04 '16 at 13:47
  • sorry for the trouble, i have another problem, i.e. each row i have a button named attended and bunked. and i have onclick for both. so i added another array to find out which button was clicked and under the (User user = (User) data.get(position);) inside the if check for state "true" i checked which button and did the transition accordingly, but what happens is that when i scroll down and up, both the transitions happen for the same row like if i say i clicked attended in row 1 which moves attended button towards right and bunked towards right as expected cont-- – Satyaraj Moily Jan 04 '16 at 15:25
  • now in row 2 when i clicked bunked button which moves the attended button towards left and bunked buttons stands still in its position as expected. but now when i scroll down and back up, the row 1 button transits as follows attended moves left and bunked move right. that means both the transition occurs for the same row. please help me with this :'( – Satyaraj Moily Jan 04 '16 at 15:32
  • okay i found out the way to solve it. thank so much once again for all the help. is there anyway i can you a favor. – Satyaraj Moily Jan 04 '16 at 17:38
  • ill put an an answer and you can give me a vote – Tasos Jan 04 '16 at 20:36

1 Answers1

1

From the comments

a way to solve the issue is to add and array item eg state which is initially is set to off and then set it to on as you click the button.

Then is simply a case of an if else check

if state is on animate the button with 0 timing or else cancel the animation so it doesnt animate any more buttons

Tasos
  • 5,263
  • 1
  • 13
  • 26