Hello,
Welcome to our Microsoft Q&A platform!
If you want to scroll to the center item, you can use function ScrollToPosition
to scroll to the special item. e.g.
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.ScrollToPosition(position);// position is the special position of your item in your RecyclerView
Update:
I tested on my side using the following code, it works on my side:
1.create a class CenterSmoothScroller
which implement interface LinearSmoothScroller
public class CenterSmoothScroller: LinearSmoothScroller
{
private Context mContext;
private static float MILLISECONDS_PER_INCH = 100f;
public CenterSmoothScroller(Context context) : base(context)
{
mContext = context;
}
public override int CalculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference)
{
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
protected override float CalculateSpeedPerPixel(DisplayMetrics displayMetrics)
{
return MILLISECONDS_PER_INCH / (int)displayMetrics.DensityDpi;
}
}
And in activity:
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.Orientation = LinearLayoutManager.Horizontal;
mRecyclerView.SetLayoutManager(mLayoutManager);
mRecyclerView.GetLayoutManager().StartSmoothScroll(new CenterSmoothScroller(this) { TargetPosition = 3 });
The result is:
Best Regards,
Jessie Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.