Similar Facebook reactions - Animated view with Ob

2019-05-11 08:14发布

问题:

I'm trying to animate a set of views vertically in an RecyclerView.Adapter, the animation works well using android:clipChildren="false", android:clipToPadding="false" and viewHolder.linear.postInvalidate(), but the ClickListener does not work after animation ends. I'm using ObjectAnimator because I read this link Android Animation - Button stays clickable

Some code

    @Override
    public void onBindViewHolder(final TViewHolder viewHolder, int i) {

   viewHolder.flGroupButtons.postInvalidate();
   viewHolder.iv1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //not working
      });
..... clicklisteners

   viewHolder.iv4.setOnClickListener(new View.OnClickListener() {

      PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f);
        PropertyValuesHolder pvTranslativ1 = PropertyValuesHolder.ofFloat("translationY", viewHolder.iv1.getY() - (measureHeight * 5));


     ObjectAnimator animatoriv1 = ObjectAnimator.ofPropertyValuesHolder(viewHolder.iv1, scaleX, scaleY, pvTranslativ1);
        animatoriv1.setInterpolator(new DecelerateInterpolator());
        animatoriv1.setDuration(300);

     ....
     ......
     AnimatorSet as = new AnimatorSet();
        as.playTogether(animatoriv1, animatoriv2, animatoriv3, animatoriv4);
        as.start();

});

}

Only iv1 clicklistener is working when is not collapsed. How can I get it work?

回答1:

You can't do this, because this is not the way Android touch event process works.

ViewGroup or it's subclass(FrameLayout, RelativeLayout, LinearLayout, etc) dispatch touch event to it's childviews based on childview's frame.so when you click on iv2 or iv3 or iv4, the touch event didn't even passed to the parent(RecyclerView's item),so the parent view never had a change to handle or dispatch the touch event to iv2 or iv3 or iv4.

what you should do is pop a new Window with iv1-iv4 in it using WindowManager, and carefully calculate the position so that it will look just like the way you did to the RecyclerView's item.only then you can receive the touchevent and onClick event

Facebook Reactions is definitely using WindowManager to pop a new Window with buttons:

here's what it looks like when I turn on the Profile GPU rendering option in Developer options




notice there are two rows of bars on screen now when I long click the like button,the smaller one is a new window which contains reactions buttons



回答2:

The clicklistener is just in the wrong position, it should be in the viewholder

// Adapter

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
    return NewsViewHolder.newInstance(parent);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final NewsEntry newsEntity = news.get(position);
    ((NewsViewHolder) holder).bind(newsEntity);
}

// ViewHolder
public NewsViewHolder(NewsViewHolderBinding binding) {
    super(binding.getRoot());
    itemView.findViewById(R.id.viewname).setOnClickListener(this);
}