0

메일 링 앱에서 사용자가 여러 메일에 동일한 메일을 보낼 수 있습니다. 연락처 목록을 열면 사용자를 클릭 할 수 있고 프로필 사진은 선택 아이콘으로 바뀌어야합니다. RecyclerView - 선택한 항목 아이콘을 변경하려면 두 번의 클릭으로 작업해야합니다.

나는 사용자에게 아이콘 점멸을 클릭하여 선택하고 내가 처음으로 클릭을 변경하지 않는 경우. 두 번째 클릭해도 이미지가 계속 깜박이지만으로 바뀝니다. 다음 번에 해당 사용자를 클릭 할 때마다 깜박 거립니다.하지만 원하는대로 할 수 있습니다 - 선택/선택 취소됩니다.

가이드로 this 튜토리얼을 사용하고 있지만 좋은 것으로 문서화되어 있지 않습니다. 어떤 방법은 언급되지 않은 한 단어로 설명되지만 코드에는 나타납니다. 나는 다른 튜토리얼을 찾아 보았고 원래보다 훨씬 더 깊은 예제를 사용하지 않고 동일한 예제 (identical)를 많이 발견했다.

Adapter.java 다음 toggleSelection 방법에 notifyItemChanged를 호출 할 필요가

@Override 
public void onBindViewHolder(final ChooseContactsAdapter.ChooseContactsViewHolder holder, final int position) { 
    final Contact contact = contactList.get(position); 

    holder.userName.setText(contact.getUserName()); 

    TextDrawable.IBuilder builder = TextDrawable.builder() 
      .beginConfig() 
      .withBorder(0) 
      .toUpperCase() 
      .endConfig() 
      .round(); 

    ColorGenerator generator = ColorGenerator.MATERIAL; 
//  generate color based on a key (same key returns the same color), useful for list/grid views 
    int color = generator.getColor(contact.getUserId()); 
    textDrawable = builder.build(contactList.get(position).getUserName().substring(0, 1), color); 
    holder.thumbNail.setImageDrawable(textDrawable); 
    holder.contactId = contact.getUserId(); 
    // display profile image 
    applyProfilePicture(holder, contact); 

    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // toggle selection 
      toggleSelection(position); 

      // Change background color of the selected items in list view 
      holder.itemView.setBackgroundColor(selectedItems.get(position) ? 0x9934B5E4 : Color.TRANSPARENT); 

      // check if item still exists 
      if (position != RecyclerView.NO_POSITION) { 
       Contact contact = contactList.get(position); 
       Toast.makeText(v.getContext(), "You clicked " + contact.getUserName(), Toast.LENGTH_SHORT).show(); 

      } 
      // handle icon animation 
      applyIconAnimation(holder, position); 
     } 
    }); 
} 

    private void applyProfilePicture(ChooseContactsViewHolder holder, Contact contact) { 
    Picasso.with(context) 
      .load(AppConfig.URL_PROFILE_PHOTO + contact.getThumbnailUrl()) 
      .placeholder(textDrawable) 
      .error(textDrawable) 
      .transform(new CircleTransform()) 
      .into(holder.thumbNail); 
} 

private void applyIconAnimation(ChooseContactsViewHolder holder, int position) { 
    if (selectedItems.get(position, false)) { 
     holder.iconFront.setVisibility(View.GONE); 
     resetIconYAxis(holder.iconBack); 
     holder.iconBack.setVisibility(View.VISIBLE); 
     holder.iconBack.setAlpha(1); 
     if (currentSelectedIndex == position) { 
      FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, true); 
      resetCurrentIndex(); 
     } 
    } else { 
     holder.iconBack.setVisibility(View.GONE); 
     resetIconYAxis(holder.iconFront); 
     holder.iconFront.setVisibility(View.VISIBLE); 
     holder.iconFront.setAlpha(1); 
     if ((reverseAllAnimations && animationItemsIndex.get(position, false)) || currentSelectedIndex == position) { 
      FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, false); 
      resetCurrentIndex(); 
     } 
    } 
} 

private void toggleSelection(int pos) { 
    currentSelectedIndex = pos; 
    if (selectedItems.get(pos, false)) { 
     selectedItems.delete(pos); 
     animationItemsIndex.delete(pos); 
    } else { 
     selectedItems.put(pos, true); 
     animationItemsIndex.put(pos, true); 
    } 
    notifyItemChanged(pos); 
} 
+1

toggleSelection 메서드에서 notifyItemChanged를 호출 할 필요가 없다고 말하고 싶습니다. 이미 애니메이션을 사용하여 항목을 수동으로 변경하고 있습니다. – jmart

+0

@jmart 오늘 시험해보고 효과가 있는지 알려 드리겠습니다. – Kemo

+0

@jmart 위대한 작품 :) 당신이 내 질문에 대답하고 싶다면 당신의 대답을 받아 들일 것입니다. 고마워요. 다시 – Kemo

답변

3

없습니다. 애니메이션을 사용하여 항목을 수동으로 변경합니다.

notifyItemChanged을 호출하면 깜박임이 생기는 이유는 애니메이션과 간섭하기 때문입니다.