RecyclerView - Scrolling down when new item is inserted - android

When my recyclerview gets a new item inserted using notifyItemInserted and smoothScrollToPosition, it creates a flickering/blink effect. What is causing it? I just want it to scrollDown when a new item is inserted.
mMessageList.add(messageObject);
runOnUiThread(new Runnable() {
#Override
public void run() {
mChatMessageAdapter.notifyItemInserted(mMessageList.size());
mRecyclerView.smoothScrollToPosition(mMessageList.size());
}
});

Related

RecyclerView Data get repeated after pull to refresh and at simultaneously Tab Changes in TabView

I have TabView with 3 Tab in MainActivity.I have set data on Tab1 using RecyclerView. Recyclerview is in SwipeRefreshView.When I pull to refresh recyclerview and at same time within second I changed tab the data in RecyclerView at Tab1 get double,triple.How to resolve this to avoid data duplication on Pull to refresh.
This is code to Pull to refresh
pullRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
String NetworkStatus = biz.fyra.bookapp.utils.NetworkStatus.checkConnection(getContext());
if (NetworkStatus.equals("false")) {
pullRefresh.setEnabled(false);
pullRefresh.setRefreshing(false);
} else {
pullRefresh.setEnabled(true);
//pullRefresh.setRefreshing(true);
db.deleteAllQueueFoodieDB();
checkInternet();
}
}
}, 1000);
}
});
Call arraylist.clear() method and recyclerView will not repeat its content after refresh.
Basically arraylist.clear() removes all the elements from arraylist.

RecyclerView notifyDataSetChanged IllegalStateException

I wana create mobile application that uses RecyclerView with pagination that loads each time from dataBase 10 items, then when the list reaches the bottom I load 10 other items, so I used this metho to be notified if I reached the end of the list :
public boolean reachedEndOfList(int position) {
// can check if close or exactly at the end
return position == getItemCount() - 1;
}
and I used this function to load items :
#Override
public void onBindViewHolder(Info holder, int position, List<Object> payloads) {
if (reachedEndOfList(position)) {
Log.d("reachedEnd", "true");
this.autoCompletesTmp = getTmp();
notifyDataSetChanged();
}
}
getTmp() update the list of items with another 10 items, but i get this exception when I reached the bottom of the list:
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
I had the same problem a while ago:
This helped:
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
//Do something like notifyDataSetChanged()
}
};
handler.post(r);

Android: ListView.getChildAt() throws null

Sometimes, when I call goToLast() it throws me a null exception in vista=lista.getChildAt(), it happens when the list is full, I dont know why I have this code:
private void goToLast() {
lista.post(new Runnable() {
#Override
public void run() {
lista.setSelection(mensajes.getCount() - 1);
View vista = lista.getChildAt(mensajes.getCount() - 1);
TextView txtMensaje = (TextView)vista.findViewById(R.id.txtMensajeLista);
txtMensaje.setBackgroundColor(Color.RED);
}
});
}
You should log lista.getChildCount(), see how many children the ListView has. ListView recycles views, which means it only hold limit number of views.
So if you want to get the last view, you should do something like this
private void goToLast() {
lista.post(new Runnable() {
#Override
public void run() {
lista.setSelection(mensajes.getCount() - 1);
// Figure out the last position of the view on the list.
int lastViewIndex = lista.getChildCount() - 1;
View vista = lista.getChildAt(lastViewIndex);
TextView txtMensaje = (TextView)vista.findViewById(R.id.txtMensajeLista);
txtMensaje.setBackgroundColor(Color.RED);
}
});
}
ListView.getChildAt() position is different to the position in your adapter. ListView recycles its views, so if you have more items in your adapter, than can fit on the screen it will not create views for all of those, but only the ones that are visible. If you want to update an item in the ListView you need to update it in the adapter and call notifyDataSetChanged()

Scrolling a ListView to a position after cursor loads

I have a fragment with a ListView, and I want the ListView to maintain its scroll position when the fragment is saved and restored. Usually this is easy, just call ListView.getFirstVisiblePosition() when saving and ListView.setSelection() (ListView.setSelectionFromTop() if you want to get fancy) when restoring.
However, I'm using loaders, and my ListView isn't fully populated when the activity starts. If I setSelection() during onActivityCreated() the list won't have anything to scroll to yet, and the call will be ignored.
I'm getting around this now by posting the scroll for sometime in the future, but this definitely isn't ideal. I'd prefer for it to scroll right as the data finishes loading. I'm almost doing that here, but swapCursor() doesn't refresh, it just schedules an invalidation for the future.
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
if (savedScrollPosition != null) {
Log.d(TAG, "loaded and scrolling to " + savedScrollPosition);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
list.setSelection(savedScrollPosition);
savedScrollPosition = null;
}
}, 100);
}
}
Do you know of any way of scrolling the ListView right as the ListView finishes populating with data?
Try this:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(savedScrollPosition);
savedScrollPosition = null;
}
});

android listview scroll to number of listitems

I am using Listview, i want to scroll down to list's 10 item when view get loaded, how can do that ????
Try yourListView.setSelection(position)
Try this:
mMessageList.addAll(result);
mMessageArrayAdapter.notifyDataSetChanged();
mMessageListView.clearFocus();
mMessageListView.post(new Runnable() {
#Override
public void run() {
mMessageListView.setSelection(searchedMessagePosition);
}
});

Categories

Resources