I have RecyclerView that contains user post , post height is not fixed , I have attached PagerSnapHelper to this RecyclerView .
How can I make scrolling distance of this RecyclerView with current complete visible item in RecyclerView .
binding.videoTrendsRecycle.setLayoutManager(mLayoutManager);
binding.videoTrendsRecycle.setMediaObjects(videosResponse.getVideosResponseArrayList());
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.findTargetSnapPosition(mLayoutManager, 0, 1000);
binding.videoTrendsRecycle.setOnFlingListener(null);
snapHelper.attachToRecyclerView(binding.videoTrendsRecycle);
VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(videosResponse.getVideosResponseArrayList(), initGlide(context), context);
Related
I have a Fragment with a RecyclerView. It works as a chat.
I request to my backend the last 50 messages and I load them in the recycler view
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new ChatMessagesAdapter(recyclerView, messages, getActivity());
recyclerView.setAdapter(mAdapter);
recyclerView.getLayoutManager().scrollToPosition(messages.size()-1);
mAdapter.notifyDataSetChanged();
Then, when I scroll up I request more messages with OnScrollListener and I load them
messages.addAll(0, oldMessages);
mAdapter.notifyDataSetChanged();
mAdapter.setLoaded();
recyclerView.getLayoutManager().scrollToPosition((oldMessages.size()) -1);
But as I set the the recyclerView in the first load with
layoutManager.setStackFromEnd(true);
when I load the old messages the scroll moves first item shown(before the last) to the bottom.
How can I add old messages on top and continue the scrolling like if the items were there from the first load? Without move the focus to the bottom
Use notifyItemRangeInserted instead of notifyDataSetChanged.
Use below code to update your adapter:
public void addList(List<ChatMessage> items) {
chatMessages.addAll(0, items);
notifyItemRangeInserted(0,items.size());
notifyItemChanged(items.size());//Only To update the last top item cause in my case it was the date header so i need to notify it
}
I have a RecyclerView in my activity and a LinearSnapHelper attached to it:
TimePickerAdapter timePickerAdapter = new TimePickerAdapter(hours);
recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
The problem is that when I want to initially scroll to a specific item, the snap helper is not being triggered to attach the item (see the image):
recyclerView.getLayoutManager().scrollToPosition(timePickerAdapter.getCurrentPosition());
When I scroll with my hand it starts to work as expected.
Any solutions to this?
SnapHelper rely on RecyclerView.OnFlingListener#onFling() or RecyclerView.OnScrollListener#onScrollStateChanged(recyclerView, RecyclerView.SCROLL_STATE_IDLE) to trigger snap action.
But scrollToPosition() will not tigger above callback. You can invoke smoothScrollBy(1, 0) after scrollToPosition() to trigger SnapHelper to scroll.
I just encountered the same issue - for me using recyclerView.smoothScrollToPosition(pos) instead of recyclerView.scrollToPosition(pos) did the trick
If items have the same width and space between them also the same you can use this for initial scroll
recyclerView.apply {
post {
val dx = initialPosition * (itemWidth + itemSpace)
scrollBy(dx, 0)
}
}
I want to be able to automatically scroll inside an horizontal RecyclerView and put the selected item in middle of the screen
In the attached image the selected item is "my apps" so i want to automatically scroll the RV so that "my apps" will show on center of screen
I know How to scroll to speciffic position:
linearLayoutManager.scrollToPositionWithOffset(2, 20);
How can i get the relative position of the item inside the recyclerView so i can scroll to it?
My RecyclerView def:
CategoriesAdapter adapter = new CategoriesAdapter(list, this);
catagoriesRV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
catagoriesRV.setLayoutManager(llm);
I want the user to be able to navigate to certain positions within a RecyclerView. I can determine the position of the items and when I use recycleView.smoothScrollToPosition() it scrolls to the correct item.
The problem is this item is at the bottom of the screen. How can I scroll so that the item appears at the top (it must be the first visible item).
Here is the code I use to setup the RecyclerView:
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(actionAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(InteractionTimelineActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setOnScrollListener(new RecyclerScrollListner());
And this is how I scroll:
if (recycleView != null) {
recycleView.smoothScrollToPosition(position + 1);
}
You have to calculate how many items fit on one screen and use that as offset when you scroll! You can use the methods findFirstCompletelyVisibleItemPosition() and findLastCompletelyVisibleItemPosition() of the LayoutManager to calculate the offset like this:
int offset = getLayoutManager()).findLastCompletelyVisibleItemPosition()
- getLayoutManager()).findFirstCompletelyVisibleItemPosition();
And once you have the offset you can scroll like this:
recycleView.smoothScrollToPosition(Math.min(position + offset + 1, adapter.getItemCount()));
I want to use the grid view in which first image is constant and on the click want to launch addProductActivity for all user , and after that the images from server will be displayed. How to achieve this please help.
You can achieve this by recyclerview where you can give layout manager as gridlayout manager :
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.fridID);
Adapter gridAdapter = new Adapter(yourProductArray, getContext(), this, this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setAdapter(gridAdapter);
and at the recyclerview Adapter when position is zero in onBindViewHolder(Adapter.ViewHolder holder, int position), always show the constant image
You can set onClickListener to the ImageView and when the position is zero and onClickListener is triggered show the add product screen
when a new product is added add the product to the yourProductArray and use gridAdapter.notifyDataSetChanged()