I have a RecyclerView
, which is set horizontal
and loaded with a FirebaseRecyclerAdapter
. On all items I set OnClickListeners
. The problem is, the RecyclerView always scrolls to the last item. So instead of showing the first item, I see the last one. When I don't set a OnClickListener, the RecyclerView works as it should and doesn't scroll to the last item.
I have already tried setFocusable(false)
and scrollToPosition(0)
My code:
In order to use a load more button
I override methods in the Adapter to use more than one layout, since the FirebaseAdapter doesn't load items inside a HorizontalScrollView, but I think this shouldn't be the problem.
mStoriesAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(Post.class, R.layout.list_item_outputdetail_story, PostHolder.class, query) {
@Override
protected void populateViewHolder(PostHolder viewHolder, final Post model, int position) {
if (model != null) {
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), StoryActivity.class);
intent.putExtra(Constants.PASS_POST_TO_STORY_ACTIVITY, model);
intent.putExtra(Constants.STORY_ACTIVITY_TYPE, Constants.STORY_ACTIVITY_ONE_STORY);
intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
startActivity(intent);
}
});
viewHolder.name.setText(model.getUser().getUserName());
viewHolder.heartsNumber.setText(String.valueOf(model.getNumLikes()));
viewHolder.title.setText(model.getTitleText());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(model.getTimeCreated());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
viewHolder.date.setText(simpleDateFormat.format(calendar.getTime()));
Glide.with(getActivity()).load(model.getUser().getPictureUri()).into(viewHolder.profilPicture);
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference imageReference = firebaseStorage.getReference().child(model.getPostId());
GlideApp.with(getActivity()).load(imageReference).into(viewHolder.imageView);
FirebaseUtils.checkForAlreadyLiked(model.getPostId(), viewHolder.hearts, firebaseKey);
} else if (position == 0) {
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), WriteStoryActivity.class);
intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
startActivity(intent);
}
});
} else {
viewHolder.showAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), StoryActivity.class);
intent.putExtra(Constants.STORY_ACTIVITY_TYPE, Constants.STORY_ACTIVITY_ALL_STORIES);
intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
startActivity(intent);
}
});
}
}
@Override
public Post getItem(int position) {
if (position == 0) {
return null;
} else if (super.getItemCount() < 5 || position < getItemCount() - 2) {
return super.getItem(position - 1);
} else {
return null;
}
}
@Override
public int getItemCount() {
if (super.getItemCount() < 5) {
return super.getItemCount() + 1;
} else {
return super.getItemCount() + 2;
}
}
@Override
public int getItemViewType(int position) {
if (super.getItemCount() < 5) {
if (position == 0) {
return R.layout.list_item_outputdetail_story_write_your_own;
} else {
return super.getItemViewType(position);
}
} else {
if (position == 0) {
return R.layout.list_item_outputdetail_story_write_your_own;
} else if (position == getItemCount() - 1) {
return R.layout.list_item_outputdetail_story_showall_fab;
} else {
return super.getItemViewType(position);
}
}
}
};
LinearLayoutManager storiesManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mStoriesRecyclerView.setLayoutManager(storiesManager);
mStoriesRecyclerView.setAdapter(mStoriesAdapter);
Thank you!