RECYCLER VIEW: A GIFT TO ANDROID ELEMENTS

RECYCLER VIEW

Recycler View is a great class that you should consider over LIST VIEW for building list interfaces. It offers more flexibility and has built-in hooks that make implementing animations and custom layouts much easier compared to List View.

Unfortunately Recycler View is missing a couple of features that List View had built-in. For example the ability to add an OnItemClickListener that triggers when an item is clicked. Recycler View allows you to set an OnClickListener in your adapter, but passing on that click listener from your calling code, to the adapter and to the View Holder, is complicated for catching a simple item click.

Fortunately, Recycler View supplies a addOnItemTouchListener that will catch all touch events on the item View. You can hook up a Gesture Detector to figure out what happens and trigger an action from there. This has some problems as well. If you don’t implement this correctly, the Gesture Detector will steal touch events and will mess up things like ripples. Also, the target view is never actually receiving the touch events in that case so your code has to emulate what happens when you click on a view to handle hap tic feedback, sound effects and accessibility events.

I came up with a solution, which is to let the view which is the item in your Recycler View, or more precisely, the ViewHolder.getItemView() handle the click.

The resulting code to hook up a click listener now looks like this:

ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {

@Override

public void onItemClicked(RecyclerView recyclerView, int position, View v) {

// do it

}

});

Users of Two Way View may notice how similar this is to ItemClickSupport in that library. Actually, I used Two Way Views’ version in the Bundle app before this, but encountered problems because it was using the touch listener technique. Once I implemented my own ItemClickSupport I went back to check the internals of the version in Two Way View and noticed that both implementations are pretty similar. I really like the elegant API that rehabnear.me came up with when implementing this in Two Way View: no more passing around click listeners!

The main difference compared to the Two Way View version is that my version uses a OnChildAttachStateChangeListener to set a OnClickListener on the item View of the View Holder without using a custom OnTouchListener.

Here’s the implementation:

public class ItemClickSupport {

 private final RecyclerView mRecyclerView;

 private OnItemClickListener mOnItemClickListener;

 private OnItemLongClickListener mOnItemLongClickListener;

 private View.OnClickListener mOnClickListener = new View.OnClickListener() {

@Override

 public void onClick(View v) {

 if (mOnItemClickListener != null) {

RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);

mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);

}

}

};

private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {

@Override

 public boolean onLongClick(View v) {

 if (mOnItemLongClickListener != null) {

RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);

 return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);

}

 return false;

}

};

private RecyclerView.OnChildAttachStateChangeListener mAttachListener= new RecyclerView.OnChildAttachStateChangeListener() {

@Override

public void onChildViewAttachedToWindow(View view) {

 if (mOnItemClickListener != null) {

view.setOnClickListener(mOnClickListener);

}

if (mOnItemLongClickListener != null) {

view.setOnLongClickListener(mOnLongClickListener);

}

}

 

@Override

public void onChildViewDetachedFromWindow(View view) {

 

}

};

 

private ItemClickSupport(RecyclerView recyclerView) {

mRecyclerView = recyclerView;

mRecyclerView.setTag(R.id.item_click_support, this);

mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);

}

 

public static ItemClickSupport addTo(RecyclerView view) {

ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);

 if (support == null) {

support = new ItemClickSupport(view);

}

 return support;

}

 

public static ItemClickSupport removeFrom(RecyclerView view) {

ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);

 if (support != null) {

support.detach(view);

}

 return support;

}

 

public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {

mOnItemClickListener = listener;

 return this;

}

 

public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {

mOnItemLongClickListener = listener;

 return this;

}

 

private void detach(RecyclerView view) {

view.removeOnChildAttachStateChangeListener(mAttachListener);

view.setTag(R.id.item_click_support, null);

}

 

public interface OnItemClickListener {

 

 void onItemClicked(RecyclerView recyclerView, int position, View v);

}

 

 public interface OnItemLongClickListener {

 

boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);

}

}

You also need to define R.id.item_click_support using ids.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<item name=”item_click_support” type=”id” />

</resources>

16 thoughts on “RECYCLER VIEW: A GIFT TO ANDROID ELEMENTS

  • April 3, 2019 at 2:30 pm
    Permalink

    This is inspired! I’m happy I found your post as it’s better than similar blogs I’ve seen from most people on this subject. Can I ask you to add to this? Could you write an additional example? many thanks!

    Reply
  • May 12, 2019 at 3:17 am
    Permalink

    My family every time say that I am wasting my time here at web, but I know I am getting experience every day by reading thes pleasant content.|

    Reply
    • June 29, 2019 at 9:02 am
      Permalink

      Thanks a lot man

      Reply
  • June 7, 2019 at 9:31 pm
    Permalink

    Well I truly enjoyed studying it. This information provided by you is very effective for proper planning.

    Reply
  • June 12, 2019 at 10:32 am
    Permalink

    I抳e been exploring for a little for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i抦 happy to convey that I’ve a very good uncanny feeling I discovered just what I needed. I most certainly will make certain to do not forget this website and give it a look on a constant basis.

    Reply
    • June 29, 2019 at 8:57 am
      Permalink

      Thanks a lot man

      Reply
    • June 29, 2019 at 8:57 am
      Permalink

      yeah I will.. I was improving my programming skills for sometimes.. Now I will be coming with better content

      Reply
  • June 24, 2019 at 6:26 pm
    Permalink

    I have been absent for some time, but now I remember why I used to love this website. Thanks , I will try and check back more often. How frequently you update your web site?

    Reply
    • June 29, 2019 at 8:52 am
      Permalink

      For some days I was busy learning programming, but I assure you I will be updating my website frequently now

      Reply
  • July 1, 2019 at 2:13 am
    Permalink

    Hello would you mind letting me know which hosting company you’re working with? I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a honest price? Cheers, I appreciate it!

    Reply
    • July 2, 2019 at 8:00 am
      Permalink

      This site is currently being running on Hostinger servers. If you don’t know about the company just do a google search.

      Reply
  • July 16, 2019 at 4:03 pm
    Permalink

    I have noticed you don’t monetize techbuck.in, don’t
    waste your traffic, you can earn additional bucks every month with new monetization method.

    This is the best adsense alternative for any type of website (they
    approve all sites), for more details simply search in gooogle: murgrabia’s
    tools

    Reply
  • July 29, 2019 at 10:34 pm
    Permalink

    Fantastic goods from you, man. I’ve bear in mind your stuff prior to and you are simply extremely excellent. I actually like what you’ve bought here, certainly like what you are stating and the way wherein you are saying it. You are making it entertaining and you still take care of to stay it sensible. I can not wait to learn much more from you. This is actually a wonderful website.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *