Scroll to middle of selected item of recyclerView - android

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

Related

Scroll RecyclerView with current Complete visible item height

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

Horizontal scrolling of list item on at a time like facebook friend suggestion list in android

I need to implement a list that has some card which scroll horizontally (scroll one item at a time like facebook friend suggestion list).
Use RecyclerView & set layout manager to that like this
recyclerView.setLayoutManager(new LinearLayoutManager(this) {
#Override
public boolean canScrollVertically() {
return false;
}
});
recyclerView.setAdapter(adapter);
I hope this will help you.
for set horizontal RecyclerView use :
LinearLayoutManager mLayoutManager = new LinearLayoutManager(Context, RecyclerView.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
and for showing horizontal scrollBar in RecyclerView , in xml layout :
android:scrollbars="horizontal"
and with this line you can set custom scrollBar:
android:scrollbarThumbHorizontal="#drawable/recycler_scroll"

How do I detect when a specific item reaches top of a RecyclerView in Android?

I'm creating an app with a list of questions in a RecyclerView. In the same parent layout, I have a static TextView above the RecyclerView that displays instruction for a set of questions. What I want is to change the text in the TextView whenever a specific question reaches the top of the RecyclerView when scrolled. How can I accomplish this?
You would need to listen to the scrolling of your RecyclerView and get the first item displayed onto the screen.
Thanksfully, Android let us set a custom ScrollListener to a RecyclerView and with your LayoutManager you are able to know which item is shown first :
// initialize your RecyclerView and your LayoutManager
mLinearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
// set a custom ScrollListner to your RecyclerView
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// Get the first visible item
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
//Now you can use this index to manipulate your TextView
}
});
EDIT : added Mateus Gondim clearance about the LayoutManager
You should override the default Recycler.OnScrollListener.
Have a look at this Code : https://gist.github.com/mipreamble/b6d4b3d65b0b4775a22e

Smoothscroll with RecyclerView put item on bottom of list

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

Grid View layout

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

Categories

Resources