RecyclerView: Highlight selected item - android

I have implemented a RecyclerView and I have set it up to use CAB. But how can I highlight the selected items? If a certain position I checked I stored in a SparseBooleanArray.
My first thought was to store the specific View containg all the elements in my ViewHolder and then in onBindViewHolder set the background somehow to: ?android:attr/activatedBackgroundIndicator
But how can I do that? Is that a useful approach?

I finally solved this by simply adding some minor things:
First of all, the items of the RecyclerView have to use this as background:
android:background="?android:attr/activatedBackgroundIndicator"
Then for the RecyclerView simply call:
setSelected(true); on the indiviual views.

If you want to change the View itself, you need to dispatch adapter.notifyItemChanged(position) and in return, recycler view will call onBind method where you can set the background.
If you don't need to update the view itself, I would suggest using an item decorator.

Related

How to get data from specific rows only in RecyclerView?

I'm stuck on a little problem.
When I click on a row in my RecyclerView, I'd like to change its color to some other color (i.e.: red).
Then, if I click this row again, I would like to set its color back to the normal one (i.e.: white).
Then, if I click on a second row or n-th row, I would like to do the same.
I've tried to work on the RecyclerView Adapter class, in my ViewHolder I tried some ideas counting user clicks, setting a boolean to check the colored rows, and getAdapterPosition to know what is the row's position... but actually all my tries failed!
Would you please help me with that problem?
It seems that I'm close to the solution, but need a little help
You're going to need to keep track of which items were selected on the list (otherwise any changes to the view will simply be recycled).
Keep a list of selected items within your adapter. In the adapter's onBindViewHolder you can check against the list and if the list contains the view/item, you can color the view accordingly.
Edit: Refer here for a working example
How to properly highlight selected item on RecyclerView?

View for a list of items that don't scroll, and handle on item click events by the item position

Just like a ListView but instead it doesn't scroll. Its content is added programatically via an ArrayAdapter. Is there any view that can be used for this purpose?
PS: Not LinearLayout, since it doesn't get its content from an adapter (I want the observer pattern)
Edit: Let me explain a little bit more. Suppose you need a list of items, but the list itself is not scrollable, what is scrollable is the screen.
That being said, the list of items should show ALL items, not a limited amount based on a fixed height. The way to go is LinearLayout as it is both non-scrollable and shows all items within itself.
But there is a third requierement. You don't want to add(View) directly, but instead, you want something similar to an ArrayAdapter so that you have a control of the items and their position, so you can handle on item click events based on their position. As far as I know, this can't be done using a LinearLayout. So my question is, does any view exist for this purpose?
You could try using a ListView, but disable scrolling, as described here
Put your layout inside a ScrollView and yes you have to use a Linearlayout. You need to set tag for each of your view that you add dynamically and whenever your view is clicked you can get it's position by view.getTag() and perform the required operation.
Note : Adding views at run time may affect performance.

How to Set the Focus on Selected Grid in GridView

I am implementing the Grid View in my android application.
I want the set the focus on my selected grid. How can i get the solution for this problem ?
I recommend to set a flag to each grid items which indicates selected or not.
and, when selecting grid item set the flags properly and call notifyDatasetChanged() on your adapter.
this will work. I did same.
If you need to set focus to GridView indeed, than call GridView's requestFocus() method. If you need to set focus at specified item in GridView, than find it by getChildAt() and also call requestFocus(). You also should be sure that your device isn't in Touch mode

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

Refresh a ListView if I change the content of the ListItem

I have a ListView in my android activity. And I populate the ListView by sub-class the BaseAdaptor (which returns a View in getView() method).
What if in my click listener of a button in a list item view, I
change the text of the TextView in the List item view
or
change the dimension of the list item view by adding/removing children of the list item view
What is an efficient to refresh my listView? I don't want the listView to re-trigger a query since there is no data change.
Thank you.
if you just change the content of your Children View, you have not more to do than TextView.setText(newText).
If you change the count of your ListView childrens, you have to call BaseAdaptor.notifyDataSetChanged()
I was about to suggest that you invalidate(), but you don't want to trigger a redraw, so I can only suggest that some form of global flag is your best bet. Set the flag, invalidate or redraw and query (and reset) the flag in OnDraw()

Categories

Resources