2015-04-21 15 views
10

Tôi gặp sự cố khi sử dụng Hoạt ảnh trong Bộ điều hợp của mình.Sử dụng hoạt ảnh trong bộ chuyển đổi với mẫu ViewHolder

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

     if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      convertView = inflater.inflate(resource, parent, false); 
      holder = new ViewHolder(); 

      holder.newRoomView = (TextView) convertView.findViewById(R.id.newRoom); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Room item = items.get(position); 

     // animate new rooms 
     if (item.isNewRoom()) { 
      AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f); 
      alphaAnim.setDuration(1500); 
      alphaAnim.setAnimationListener(new AnimationListener() { 
       public void onAnimationEnd(Animation animation) { 
        holder.newRoomView.setVisibility(View.INVISIBLE); 
       } 

       @Override 
       public void onAnimationStart(Animation animation) {} 

       @Override 
       public void onAnimationRepeat(Animation animation) {} 
      }); 
      holder.newRoomView.startAnimation(alphaAnim); 
     } 

     // ... 

     return convertView; 
    } 

Khi thêm một phòng mới bên ngoài của adapter và gọi notifyDataSetChanged phòng mới được hoạt hình một cách chính xác, nhưng khi onAnimationEnd được gọi, khác (phòng không mới) là ẩn.

Có cách nào để ẩn phòng chính xác không?

+0

tại sao bạn không dùng thử bằng cách sử dụng 'RecyclerView'? –

+0

đăng bố cục của mặt hàng của bạn, plz – SilentKnight

Trả lời

2

Vì bạn đã không tuyên bố holder biến trong getView() phương pháp, tôi chỉ có thể giả định rằng bạn đã khai báo nó như là một biến cá thể trong lớp của bạn. Đây là vấn đề của bạn. Vào thời điểm hoạt ảnh được hoàn thành, biến số holder đang giữ tham chiếu đến một mục hoàn toàn khác.

Bạn cần sử dụng biến cục bộ được khai báo là final bên trong phương thức getView(). Tôi không biết nếu bạn cần holder biến này bên ngoài getView() phương pháp hay không, nhưng nếu bạn làm thế, bạn có thể làm điều này:

// animate new rooms 
    if (item.isNewRoom()) { 
     final ViewHolder holderCopy = holder; // make a copy 
     AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f); 
     alphaAnim.setDuration(1500); 
     alphaAnim.setAnimationListener(new AnimationListener() { 
      public void onAnimationEnd(Animation animation) { 
       holderCopy.newRoomView.setVisibility(View.INVISIBLE); 
      } 

      @Override 
      public void onAnimationStart(Animation animation) {} 

      @Override 
      public void onAnimationRepeat(Animation animation) {} 
     }); 
     holder.newRoomView.startAnimation(alphaAnim); 
    } 

này, tất nhiên, sẽ không hoạt động nếu các hình ảnh động phải mất quá lâu mà chế độ xem đã được tái chế trong thời gian chờ đợi.

0

Có thể là do cơ chế tái chế của ListView. Thử gắn thẻ chế độ xem hoạt ảnh và sử dụng findViewByTag để truy xuất chế độ xem onAnimationEnd. Ví dụ.

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

    if (convertView == null) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     convertView = inflater.inflate(resource, parent, false); 
     holder = new ViewHolder(); 

     holder.newRoomView = (TextView) convertView.findViewById(R.id.newRoom); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    Room item = items.get(position); 

    // animate new rooms 
    if (item.isNewRoom()) { 
     AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f); 
     alphaAnim.setDuration(1500); 
     alphaAnim.setAnimationListener(new AnimationListener() { 
      public void onAnimationEnd(Animation animation) { 
       View view = convertView.findViewWithTag(position); 
       if (view != null) { 
        view.setVisibility(View.INVISIBLE); 
       } 

      } 

      @Override 
      public void onAnimationStart(Animation animation) {} 

      @Override 
      public void onAnimationRepeat(Animation animation) {} 
     }); 
     holder.newRoomView.startAnimation(alphaAnim); 
     holder.newRoomView.setTag(position); 
    } 

    // ... 

    return convertView; 
} 
0

Hãy nhớ rằng bạn cần luôn có một tuyên bố khác cho những việc như thế này. Nếu không, bất kỳ điều gì khác bằng cách sử dụng cùng một người giữ chế độ xem cũng sẽ bị ẩn.

if (item.isNewRoom()) { 
     AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f); 
     alphaAnim.setDuration(1500); 
     alphaAnim.setAnimationListener(new AnimationListener() { 
      public void onAnimationEnd(Animation animation) { 
       holder.newRoomView.setVisibility(View.INVISIBLE); 
      } 

      @Override 
      public void onAnimationStart(Animation animation) {} 

      @Override 
      public void onAnimationRepeat(Animation animation) {} 
     }); 
     holder.newRoomView.startAnimation(alphaAnim); 
    }else{ 
     holder.newRoomView.setVisibility(View.VISIBLE); 
    } 
1
if (item.isNewRoom()) { 

     // store view reference first 
     final View newRoomView = holder.newRoomView; 
     ... 
     alphaAnim.setAnimationListener(new AnimationListener() { 
      public void onAnimationEnd(Animation animation) { 

       // hide exactly this view when animation ends 
       newRoomView.setVisibility(View.INVISIBLE); 
      } 
     ... 
    } 
0

OK. Tôi đã có vấn đề này trước, và điều này có lẽ là một lỗi Android:

Bạn đang sử dụng đối tượng bên trong để thiết lập các listener hoạt hình:

alphaAnim.setAnimationListener(new AnimationListener(){/*bla bla bla*/}); 

Thay đổi mã trên một cái gì đó như thế này:

AnimationListener myListener = new AnimationListener(){/*bla bla bla*/}; 
alphaAnim.setAnimationListener(myListener); 

Khá điên một giải pháp, tôi biết, nhưng nó đã lưu ass của tôi trong một số trường hợp tương tự.

+0

Đáng buồn thay. điều này dường như không làm việc cho tôi: ( – Chris

+0

Câu trả lời này là vô ích. Mã bạn đã đề xuất là chính xác giống như mã bạn đang thay thế. Bạn vẫn đang tạo một lớp ẩn danh, giống như trước đây. –

+0

@David Wasser: Điều này đã làm việc cho tôi trong quá khứ. Bạn có chắc chắn 100% rằng các mã sẽ giống hệt nhau trên Android không ?: – Behnam

Các vấn đề liên quan