Change the recyclerview's focus to certain item position? - android

So I have a standard RecyclerView that takes a list of posts and displays them to the user. My problem is when the user comes into the activity, I want to be able to change the focus to a particular post.
Ex. User A sees that he has a notification that of someone has liked his/her post. He/she clicks that notification, and it loads the activity of user's post, and then the focus changes to the position of the post that had bee liked.
I pass in the postID to the activity using an intent. I then run the Recycler View, and onBindHolder i try to see if the post's ID matches to the postID I sent in via intent. If it does, I now set the activity's variable postFocusPosition to the position of that post.
Now I'm stuck on what that magic method i need to call in RecyclerView class to actually change the focus to that item position!
Hope someone can help enlighten me on that magic line(s) of code that changes focus!

recyclerView.setAdapter(adapter);// set adapter on recyclerview
recyclerView.scrollToPosition(mSkipTo); //use to focus the item with index
adapter.notifyDataSetChanged();
mProgressDialog.dismiss();

For smooth scrolling effect and to avoid any resizing issue of recyclerview items, you can use
recyclerView.smoothScrollToPosition(itemposition); //you can get this position, from your adapter class through an interface method
As with "scrollToPosition", I was facing resizing issue of recyclerview items.
Note : You could use this method for both horizontal and vertical recyclerview.

just use this
recyclerView.scrollToPosition(position);

Related

Update View in Activity in response to recycler view item click

I have this type of activity layout
In my layout, the yellow portion is my recyclerview, which is currently working as the expandable list.
Now in my scenario, when the user clicks on any item in the recyclerview than the layout changes in the activity shown by, green area and its child in the image.
Is there any way to do so?
Here is the simple example of pass value from recyclerview to activity using eventbus. Try this maybe will help you to solve your problem.
You can use callback method.On click of item update UI using callback method.
Yes you can achieve this easily by using EventBus.

Recyclerview manipulate items that are not selected

i am trying to add some animations to my Recyclerview. So far everything works great, but i have reached on problem i don't have an answer to.
I have a List where the user should be able to select only one Item. When an item is selected an animation starts and a checked box appears. Now if another item is selected it should also play this animation (This Part works) but it should also remove the checkbox from previous selected one with an animation (This part does not work).
My Problem is that i do not have a clue how to get the adapterPosition of the previous selected item.
I am thankful for any help
So i solved it, maybe someday someone will stumble upon this question, so here is the answer.
The Recyclerview adapter method getAdapterPosition() returns the sam value as the index in the ArrayList given to the Adapter.
All you have to do is fetch the index of the item you want to manipulate and then you can work with that.
If some one needs more details, then post a comment.

Android Grid View onItemClick not working

I have a grid view when user presses a button i am changing grid view data and setting adapter again now on item click is not working but Touch listener is working WHY ?
I have two types of adapters when user presses button a i am setting first adapter when user presses button b i am setting second adapter data is changing but on item click not working?
Try to use AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html).
Because this kind of updates is always a problem, you have to do your adapter's update in onProgressUpdate or onPostExecute.
So, when your button is pressed, you execute your AsyncTask.
I know this is old, but maybe somebody else will seek for answers...
Anyway, I had a same issue and wasn't able to figure out why my onItemClick() didn't work. Instead of implementing onItemClick() inside your adapter, try doing it with adapter.setOnItemClickListener(). It worked for me ;)

onCreate -> scroll to a certain part of the list?

I have an intent that loads a new activity, I also have some extra intent data to get when the new activity is started. Depending on what button is clicked on the first activity, it should scroll to a certain part of the scroll view on the next activity
It would be easiest to have it scroll down enough so that a certain item is at the top and the first thing read (defined by its ID)
Does anyone know how to do this?
Thanks
Check out the smoothScrollToPosition method in ListView:
http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)
This is what you can call to automatically show your list view to the desired position
ListView.setSelectionFromTop(index, 0);
to find out where to start you must compare the item that you want the list to show at startup from the list and the item that is received from your intent.
if you exactly know the position of a specific item in the listview you can call the method written above directly

Android ArrayAdapter tell all other views to refresh themselves

I need all of the other views in my arrayadapter to check their attributes and return to a default attribute at certain times
use case: listview items (a)(b)(c)(d) , when you touch (a) it's background turns black. This is done in the ontouchListener. But when you touch view (b) it's background turns black BUT (a)'s needs to turn back to the default which is not black
I dabbled around with recalling the arrayadapter at the end of the view.OnClickListener but the problem with this is that it also resets the scroll position of the view, so if this list was much longer - which it is - and user touched item (r) , then it would reset the list and put the user back at item (a) at the top
I was looking at notifydatasetchangedbut I am not sure how to use that and where it should work
insight appreciated
notifyDataSetChanged will do the job. You can call that at any time in your code and this will trigger the adapter to call getView for the visible list items again and so update their content. So basically the only thing you have to do is to update the list item states or information before calling that method.

Categories

Resources