RIpple effect on RecyclerView not working on light tap

两盒软妹~` 提交于 2019-11-30 10:28:30

I faced a similar problem. I had an ItemClickListener on the RecyclerView, and in that listener I was pushing a new fragment. By trial and error I noticed that removing or emptying the item click listener would get the highlight to show every time, even on light taps.

The fix was to change my onItemClick method:

@Override
public void onItemClick(final RecyclerView parent, final View view, final int position, final long id)
{
    ViewCompat.postOnAnimationDelayed(parent, new Runnable()
    {
        @Override
        public void run()
        {
            // Your click code goes here
        }
    }, 50);
}

This postpones your click action until 50ms after the next animation step and gives the animation enough time to load and run before your click action is performed.

Chitrank Dave

This happens because you have used addOnItemTouchListener for item click listener. Just create an interface in RecyclerAdapter

In adapter create an interface

public interface OnItemClickListener {
    void onItemClick(Item item);
}

private final List<ContentItem> items;
private final OnItemClickListener listener;

public Adapter(List<ContentItem> items, OnItemClickListener listener) 
{
   this.items = items;
   this.listener = listener;
}

@Override 
public void onBindViewHolder(ViewHolder holder, int position) {
   holder.bind(items.get(position), listener);
}


//In your Activity
recycler.setAdapter(new Adapter(items, new 
Adapter.OnItemClickListener() {
@Override public void onItemClick(ContentItem item) {
    Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));

instead of below code and it will work like a charm.

recyclerView.addOnItemTouchListener( 
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // TODO Handle item click
      }
   })
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!