How do I place new items at the top of a recyclerView? - android

I'm adding content to my database (Firebase) which are suppose to be loaded into my recyclerview. But the new content always appear at the bottom of the recyclerview. Is there any way to make them appear at the top rather than appear at the bottom

If you want to appear new Items on the top just Add this code in your Activity.
you need to reverse your RecyclerView, then you totally need this code segment. It does not require any modification or any special library. It’s so simple to work on LinearLayoutManager .
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLayoutManager);
First you need to create a LinearLayoutManager object and use “setReverseLayout(true)” and “setStackFromEnd(true)” methods to reverse and add item at the end.

I finally solved the issue all i needed to do was to initialize an integer variable as 0,add it along side with the object of the model class that is to be gotten from the Firebase database and finally notifying an item inserted to the adapter.The code will explain better
int position = 0;
ArrayList<Places> data = new ArrayList<>();
LinearLayoutManager lg = new LinearLayoutManager(this);
then the final code was this
valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Places content = ds.getValue(Places.class);
data.add(position,content);
adapter = new YourAdapter(data, getApplicationContext);
recyclerview.setLayoutManager(new Line);
recyclerview.setAdapter(adapter);
adapter.notifyItemInserted(position);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Handle error code here
}
});

Related

Re-order Query after click on Button - FirestoreRecyclerOptions

Currently, i can sort my documents in a Query, with it's:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100);
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query,
AnuncioPrincipal.class).build();
adapter = new AnuncioAdapter(options);
RecyclerView recyclerView = root.findViewById(R.id.recyclerCadAnun);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
This works fine, but, i need that it change when the user click on button, i need that var "campo" must be changed to "dataPreco" and the list in recycler view must be updated:
botaoFiltrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
campo = "dataAnuncio";
}
});
In spite of "campo" be changed to "dataAnuncio", the recyclerView still remains equals.
I need that the button click, the recyclerView change to the new query orderBy() method.
The FirestoreRecyclerAdapter has an updateOptions method that you can call to update the options.
So you could call that with something like:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100); // new campo
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query, AnuncioPrincipal.class).build();
adapter.updateOptions(options);

RecyclerView with FirebaseRecyclerAdapter showing wrong values when scrolling

In my Activity, I use a RecyclerView that uses a FirebaseRecyclerAdapter to draw a list:
mChatDetailsAdapter = new ChatDetailsAdapter(
this,
MessageData.class,
R.layout.layout_chat_item,
ChatDetailsAdapter.MessageViewHolder.class,
query);
mChatListView.setHasFixedSize(true);
LinearLayoutManager lLinearLayoutManager = new LinearLayoutManager(ChatDetailsActivity.this, LinearLayoutManager.VERTICAL, false);
lLinearLayoutManager.setStackFromEnd(true);
lLinearLayoutManager.setReverseLayout(true);
mChatListView.setLayoutManager(lLinearLayoutManager);
mChatListView.addItemDecoration(new VerticalSpaceItemDecoration(UIUtils.dpToPx(ChatDetailsActivity.this, 8)));
mChatListView.setAdapter(mChatDetailsAdapter);
The Adapter takes care about the views:
#Override
protected void populateViewHolder(final MessageViewHolder pViewHolder, final MessageData pMessageData, int pPosition) {
pViewHolder.mMessageTextView.setText(pMessageData.getMessage());
pViewHolder.mSelfLinearLayout.setVisibility(View.VISIBLE);
pViewHolder.mTimeTExtView.setText(StringUtils.toMessageTimeString(mContext, pMessageData.getTime()));
}
In the onBindViewHolder() method I basically do the same, be assigning the data from the model (works correctly after looking at the log messages). Now when scrolling the list the RecyclerView changes its values and doesn't recycle correctly and I have no idea why. Any idea?

Loading more items on user scroll with FirebaseIndexRecyclerAdapter

I'm using a FirebaseIndexRecyclerAdapter as described on https://github.com/firebase/FirebaseUI-Android/tree/master/database. But my list has many items and I want to only load a few items at first and then lazy load the rest when the user scrolls up.
My first thought was that I could use mKeyRef.limitToLast(5) but then updating this requires recreating the Adapter, right?
How do I achieve this lazy-loading mechanic?
RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseIndexRecyclerAdapter<Chat, ChatHolder>(
Chat.class,
android.R.layout.two_line_list_item,
ChatHolder.class, mIndexRef,
mDataRef) {
#Override
public void populateViewHolder(ChatHolder chatMessageViewHolder, Chat chatMessage, int position) {
chatMessageViewHolder.setName(chatMessage.getName());
chatMessageViewHolder.setText(chatMessage.getText());
}
};
recycler.setAdapter(mAdapter);

Recyclerview fully scroll to position

My recyclerview should open the first item closed. To this end, I wrote these lines.
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.scrollToPosition(1);
paletteRecyclerView.setLayoutManager(llm);
I want to check if it is possible to scroll to position 1 fully. If my recyclerview is small I must open it without closing the first item. How can I check it?
I assume that you want to know if there are items to scroll in RecyclerView. So you can check as below after setting the adapter to the recyclerView.
recyclerView.post(new Runnable() {
#Override
public void run() {
int recyclerViewheight = recyclerView.getHeight();
int totalChildViewsHeight = recyclerView.computeVerticalScrollRange();
if (recyclerViewheight > totalChildViewsHeight) {
//there is no scroll (there are no more items to scroll)
} else {
//there is scroll (there are items to scroll)
}
}
});
Try with below code...
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
RecyclerView.Adapter adapter = new YourAdapter();
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(position);

Empty Recycler view is not getting updated with new data

I intialize my recycler view adapter with an empty arraylist initially.
Then later once I fetch data from API I try to update the adapter by calling notifyDataSetChanged() but none of call back methods in my adapter is getting fired
void initRecyclerView()
{
myList = new ArrayList<>();
myRecyclerAdapter = new myRecyclerAdapter(getActivity(), myList, "");
myRecyclerView.setAdapter(myRecyclerAdapter);
linearLayoutManager = new LinearLayoutManager(getActivity());
myRecyclerView.setHasFixedSize(true);
myRecyclerView.setLayoutManager(linearLayoutManager);
}
After getting data from API I just update the adapter as follow :
void updateRecyclerView(ArrayList<Data> newList)
{
if (myList.isEmpty())
linearLayoutManager.removeAllViews();
myList.clear();
myList.addAll(newList);
myRecyclerAdapter.notifyDataSetChanged();
}
void updateRecyclerView(ArrayList<Data> newList)
{
//you don't have to removeviews
// if (myList.isEmpty())
// linearLayoutManager.removeAllViews();
myList.clear();
myList.addAll(newList);
myRecyclerAdapter.notifyDataSetChanged();
}
I made a costliest mistake and ponder around something else instead of looking it at different aspect.
I had SwipeRefreshLayout that wraps RecyclerView.
I should've just use setRefreshing(false) once data downloaded through API call. Instead I used setVisibility(VIEW.GONE) on mymSwipeRefreshLayout`.
This visibility change of SwipeRefreshLayout impacted RecyclerView within.
I hope none will do this mistake. Perhaps someone might come across this tiniest mistake so I Mark my answer as accepted answer.
First of all try to set layout manager and then set adapter to list :
void initRecyclerView()
{
linearLayoutManager = new LinearLayoutManager(getActivity());
myRecyclerView.setHasFixedSize(true);
myRecyclerView.setLayoutManager(linearLayoutManager);
myList = new ArrayList<>();
myRecyclerAdapter = new myRecyclerAdapter(getActivity(), myList, "");
myRecyclerView.setAdapter(myRecyclerAdapter);
}
Also pay attention that if you want to change data set or notify your adapter about the changes, you should do it in your main thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
myList.clear();
myList.addAll(newList);
myRecyclerAdapter.notifyDataSetChanged();
}
});

Categories

Resources