how to stop recyclerview from scrolling to hidden items (View.gone)? - android

i have a recyclerview with hidden items under condition and it works great but when i scroll the recyclerview it keeps scrolling an empty page after i scroll all the data included here is my code
adapter
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.id.setText(mData.get(position).getId());
holder.date.setText(mData.get(position).getDate());
holder.itemName.setText(mData.get(position).getItemName());
holder.price.setText(mData.get(position).getPrice());
if (holder.id.getText().toString().equals("")){
holder.itemView.setVisibility(View.GONE);}
else{
holder.itemView.setVisibility(View.VISIBLE);
}
in recyclerview activity
mData.add(new items(num,itemName,price,date,row));
adapter = new Adapter(mData,recyclerViewItems.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
linearLayoutManager.setReverseLayout(false);
linearLayoutManager.setStackFromEnd(false);
recyclerView.setAdapter(adapter);
i want the recyclerview to stop scrolling at the last visible item and i dont know how to do it
please help me
thank you in advance

Related

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?

RecyclerView scroll in incorrect direction when new element is inserted

I use smoothScrollToPosition to scroll RecyclerView. It does scroll every time a new entry is inserted; but to the top, not to the bottom, which is the direction i want.
list_chat = (RecyclerView) findViewById(R.id.list_chat);
//Set up Layout Manager
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
list_chat.setLayoutManager(linearLayoutManager);
//set adapter
list_chat.setAdapter(adapter);
//set scroll
list_chat.post(new Runnable() {
#Override
public void run() {
list_chat.smoothScrollToPosition(adapter.getItemCount());
}
});
The adapter is from Firebase
adapter = new FirebaseRecyclerAdapter<ChatItem, ChatRecylerViewHolder>(ChatItem.class,R.layout.chat_item
,ChatRecylerViewHolder.class,queryChat ) {
#Override
protected void populateViewHolder(ChatRecylerViewHolder viewHolder, ChatItem model, int position) {
viewHolder.tvAuthorChat.setText(model.chatAuthor);
viewHolder.tvContentChat.setText(model.chatContent);
}
};
You do notice you are using linearLayoutManager.setStackFromEnd(true); this mean you first position is at the bottom. I suggest you a better option.
RecycleView didn't work the way listView work, you can scroll it with your layout manager something like this
linearLayoutManager.scrollToPositionWithOffset(position,offset);
Which position is the position you want to scroll to, offset is the offset within the current position. You could just use with one parameter as well.
linearLayoutManager.scrollToPosition(position);
Ok. I found the answer.
First, the old problem with my question: i thought list_chat.post is called whenever an item is inserted (turn out that is wrong). The reason for it keeps scrolling top is linearLayoutManager.setStackFromEnd(true);
Thus, the question comes down to Where to call the scrolling ?
The answer is : Since adapter manages data, it makes sense to guess that adapter will notify the insertion.
Here is the code
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
list_chat.smoothScrollToPosition(adapter.getItemCount());
}
});

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);

FirebaseRecyclerAdapter doesnt work with databindings

I'm using RecyclerView with databindings but when I run the app the first time nothing is showing up then after update some content or update the app via instant Run the content appears.
my ViewHolder:
class MyViewHolder extends RecyclerView.ViewHolder {
private ItemBinding mBinding;
public MyViewHolder(View itemView) {
super(itemView);
mBinding = DataBindingUtil.bind(itemView);
}
ItemBinding getBinding() {
return mBinding;
}
}
my Adapter:
public class MyAdapter extends FirebaseRecyclerAdapter<MyModel, MyViewHolder> {
public MyAdapter(Query ref) {
super(MyModel.class, R.layout.my_item, MyViewHolder.class, ref);
}
#Override
protected void populateViewHolder(MyViewHolder viewHolder, MyModel model, int position) {
ItemBinding binding = viewHolder.getBinding();
binding.setMyModel(model);
binding.executePendingBindings();
}
}
I found in some other question I need call binding.executePendingBindings() that was I did without success.
Edit
I just added a log call:
Log.d(BuildConfig.TAG, "called populateViewHolder " + position);
on the populateViewHolder method. The log is never printed.
Edit 2
The way how I'm initializing my recyclerView:
// onCreate
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view)
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(llm);
mRef = FirebaseDatabase.getInstance().getReference().child(CHILD_TREE)
// onStart
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);
From my comment:
Have you already looked at the firebase/FirebaseUI-Android issue..? Setting the RecyclerView height from wrap_contentto match_parent solved the problem there.
Apparently, there really is a problem with the height set to wrap_content in the RecyclerView when using the FirebaseRecyclerAdapter with DataBinding.
Change it to
android:layout_height="match_parent"
and it should work.
The FirebaseUI-Android Team thinks that this is a problem with the RecyclerView itself and has closed the issue. This is the crosspost from the Github Issue.
Store your data in an ArrayList of your model.Then try implementing the onBindViewHolder method in your adapter. onBindViewHolder has two attributes-holder and position. The holder will be an object of your MyViewHolder class, and you can use the position to get the object of your model at that position from the array list.
Edited
You are calling given statements
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);
in onStart . onStart calls after onCreate. you have set adapter to recyclerView before it's initialized and reference of Database isn't yet created .Put the above two statements in onCreate after
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view)
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(llm);
mRef = FirebaseDatabase.getInstance().getReference().child(CHILD_TREE) ;
// adapter must be set after getting mRef
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);

Categories

Resources