I have a Fragment
that contains a RecyclerView
. Within that Fragment
, I load some data from a database, populate the RecyclerView
with it and then want to scroll to a certain position in that RecyclerView
.
To perform the scrolling on the UI thread after loading the data, I wanted to use a handler:
public class MyFragment extends Fragment {
private RecyclerView mRecyclerView;
private mHandler Handler = new Handler();
//....
private void onDataAvailable() {
// ...
int scrollPosition = getScrollPosition();
mHandler.post(new Runnable() {
public void run() {
mRecyclerView.smoothScrollToPosition(scrollPosition);
}
});
}
}
However, it never scrolls down.
When I use runOnUiThread
, everything works:
public class MyFragment extends Fragment {
private RecyclerView mRecyclerView;
//....
private void onDataAvailable() {
// ...
int scrollPosition = getScrollPosition();
getActivity().runOnUiThread(new Runnable() {
public void run() {
mRecyclerView.smoothScrollToPosition(scrollPosition);
}
});
}
}
No matter if I instantiate mHandler
as new Handler()
or as new Handler(Looper.getMainLooper())
, it doesn't work.
My understanding was that new Handler(Looper.getMainLooper())
would allow me to execute tasks on the UI thread and should have the same effect as runOnUiThread
. What's wrong in my thinking?
UPDATE
For test purposes, I am calling onDataAvailable
from the end of my onCreateView
function and with mock data loaded. Same effect: With my own handler, it fails to scroll. With runOnUiThread, it works perfectly.
Even more interestingly, I printed the thread ID from outside the runnable for both the handler and runOnUiThread. Both times, the code is run on Thread 1, no difference.
UPDATE 2
Everything works if I use mRecyclerView.scrollToPosition
rather than smoothScrollToPosition
. Any idea what that could be?