lundi 13 juin 2016

Android view animation applies to few elements in recyclerview instead of one by onClick event

I'm trying to realize the item-click feature animation that makes alpha value to became 0f for each items in recyclerview, but I'm facing some problems:

  1. When I click on item it disappears (expected), but then, if I scroll down a little, I can found another one item that also disappeared (not expected).
  2. And another strange behaviour is, when I scroll recyclerview items, that disappeared, appears again (items that I did click earlier) and other items disappears (items that I did not click earlier)

Disappears means that alpha value becomes 0.

I've also found other solutions but mostly it's just using onClick event and gives me same problems.

Please help.

Code that I have:

ListAdapter

public class InteractionCardPrincipleListAdapter extends RecyclerView.Adapter {

    List<InteractionCardPrincipleListDataModel> mData;
    Context mContext;

    public InteractionCardPrincipleListAdapter(Context context, List<InteractionCardPrincipleListDataModel> data) {
        mData = data;
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.interaction_card_principle, parent, false);
        return new InteractionCardPrincipleListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        ((InteractionCardPrincipleListViewHolder) holder)
                .getInteractionCardPrincipleFrontText()
                .setText(mData.get(position).getTitle());
        ((InteractionCardPrincipleListViewHolder) holder)
                .getInteractionCardPrincipleFrontImage()
                .setImageResource(mData.get(position).getResource());
        ((InteractionCardPrincipleListViewHolder) holder)
                .getInteractionCardPrincipleBackText()
                .setText(mData.get(position).getDescription());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}

ViewHolder

public class InteractionCardPrincipleListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private final AppCompatTextView mInteractionCardPrincipleFrontText;
    private final AppCompatTextView mInteractionCardPrincipleBackText;
    private final AppCompatImageView mInteractionCardPrincipleFrontImage;

    public InteractionCardPrincipleListViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        mInteractionCardPrincipleFrontText = (AppCompatTextView) view.findViewById(R.id.interaction_card_principle_front_text);
        mInteractionCardPrincipleFrontImage = (AppCompatImageView) view.findViewById(R.id.interaction_card_principle_front_image);
        mInteractionCardPrincipleBackText = (AppCompatTextView) view.findViewById(R.id.interaction_card_principle_back_text);

    }

    public AppCompatTextView getInteractionCardPrincipleFrontText() {
        return mInteractionCardPrincipleFrontText;
    }

    public AppCompatTextView getInteractionCardPrincipleBackText() {
        return mInteractionCardPrincipleBackText;
    }

    public AppCompatImageView getInteractionCardPrincipleFrontImage() {
        return mInteractionCardPrincipleFrontImage;
    }

    @Override
    public void onClick(View v) {
        v.animate()
                .alpha(0f)
                .setDuration(500);
    }
}

Aucun commentaire:

Enregistrer un commentaire