RecyclerView with FirebaseRecyclerAdapter showing wrong values when scrolling - android

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?

Related

Android Firebase Pagination for Recyclerview with Gridlayoutmanager

I have a recyclerview which initially fetching last 10 posts. I want a process where when the recyclerview reaches bottom after scrolling it again fetches next 10 posts and so on. I have used gridlayoutmanager for the recyclerview.
My code is:
Query query = mDatabase.orderByChild("UserId").equalTo(userId).limitToLast(10);
FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder>(
HomeBlog.class,
R.layout.profile_row,
BlogViewHolder.class,
query
)
{
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, HomeBlog model, int position) {
final String post_key = getRef(position).getKey();
//final String postPosition = String.valueOf(position);
viewHolder.setImage(post_key);
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
//mBloglist is the recyclerview
Now I may put the above code in a method and call it from onStart in the beginning and then call it everytime (with some modification) when the recyclerview reaches bottom (with showing a progressbar perhaps)
I guess at first I need some scrolllistener to detect when recyclerview reaches bottom, and then when it does, fetch more data. But I cant seem to do it. Any help will be appreciated.

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

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

How to get reference to a View inside CardView from an activity?

I have got CardView with Views inside a RecyclerView. I have created adapter in which assets are attached to Views and everything works. Now, I would like to change these Views from my activity. Is it any simple way to do that?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Offer>offers;
TextView timer; //timer inside CardView
private void getViewReferences() {
recyclerView = (RecyclerView)findViewById(R.id.mainRecyclerView);
timer = (TextView)findViewById(R.id.timer);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewReferences();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
timer.setText("13:16"); //NullPointerException here
}
private void initializeData(){
offers = new ArrayList<>();
offers.add(new Offer("godzina", R.drawable.zdj, 200));
offers.add(new Offer("godzina", R.drawable.zdj));
}
private void initializeAdapter() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(offers);
recyclerView.setAdapter(adapter);
}
}
So you don't want to change the Views but you want to update the text values of the views in your list.
You can't directly do that by trying to find the Views in the recyclerView and change the text. The adapter is responsible for giving values to your list. So you need to update the dataset in the adapter and call one of the notifyDataSetChanged methods on the adapter to update the recyclerView.
The big advantage of this design is that it places Views in Cache in order to reuse them multiple times without having to inflate and construct them again. This is a huge performance improvement.
You need to read the docs and some tutorials to understand the adapter design pattern better.

Categories

Resources