5

I have implemented the recycler view in navigation drawer in android.This is working fine.I am able to switch between item by clicking on recycler view item. but i am not able to change the background color for the selected item.please suggest me how to imeplement it.I have tried this so far.

1.Background Selector in RecyclerView Item Tried to make recycler view clickable,focusable but didn't work

2.http://innodroid.com/blog/post/tracking-selected-item-in-recyclerview

implemented but didn't understand where to write the code for changing background

Please help me out.

Community
  • 1
  • 1
Rahul Batra
  • 123
  • 1
  • 2
  • 10

2 Answers2

3

What you really need to understand with RecyclerView is that it's not the same control as a Listview with a funky adapter.

RecyclerView does not exhibit many of the ListView's functionalities and whilst it's understandable to compare it to a ListView or a GridView (or event a StaggeredGridView), it shouldn't be confused with them.

With RecyclerView, the responsibilities of handling the "background change" selector relies on the underlying control that the RecyclerView is holding. It's also the same with onClick and many other perks you get for free in a ListView.

Why it is better (or worse) to use a RecyclerView to a ListView is a different matter that I won't go into but to fix your problem, in order to set a background selector on your RecyclerView, add this to the layout that you're inflating in your ViewHolder (i.e. the actual layout that's being used inside the RecyclerView, similar to your "list row item" that you would inflate inside an ArrayAdapter if it were a ListView):

android:clickable="true"
android:background="?android:selectableItemBackground" 

Which should set the background appropriately.

kha
  • 19,123
  • 9
  • 34
  • 67
  • I am just suggesting if list view can do the work for you then why go for recycler view because hard to implement that click feature & all. In order to user display just few item in list use list view unless you have requirement for custom views.@kha – TechChain May 07 '15 at 08:44
  • 1
    @TechGuy fair enough. I thought there was an official recommendation about this. Whilst I agree ListView is much simpler to use, RecyclerView has its perks (such as very easy swapping of the `LayoutManager` and some performance benefits so personally, I try to use it everywhere I can. It can also be because I've spent countless hours trying to figure out how they work, so I want to get something back from those hours or I'm simply a masochist :). – kha May 07 '15 at 08:49
-1

Recycler view are recommend when you have very large items & wants to have a custom UI. If you want to display only few items the would recommend to use List View.

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • 1
    Do you have any reference for this? I've never heard this argument before and don't quite understand the reasoning behind it so I would be very happy to look at the reasoning for my own benefit. – kha May 07 '15 at 08:36
  • 3
    @TechGuy The whole point of `RecyclerView` is to replace `ListView`. – Eugene May 07 '15 at 08:47
  • downvote because this doesn't even try to address the question, much less answer it. (I also disagree with the supposed recommendation, as one clear advantage to using RecyclerView is that it prevents you from making the same common mistakes made by lazy developers implementing ListViews, but that's not my reason for downvoting.) There is no difference between a ListView and RecyclerView that has anything to do with "large items" or "a custom UI" so recommending one over the other on that basis doesn't make any sense. – Lorne Laliberte Mar 15 '16 at 21:10