Like the image above I want to draw a center line on the recycling view and get the center element when scrolling (and moving left or right)
Here is my attempt to draw a horizontal recycling view:

Is there a way to see which item is moved in the middle of the recycling overview? And how do I scroll RecycleView from one position to the left or to the right?

Update : I’ve tried to use scrolling to get the central position, but it doesn’t work as an aspect.

@Review
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstPos = layoutManager.findFirstVisibleItemPosition();
int lastPos = layoutManager.findLastVisibleItemPosition();
int middle = Math.abs(lastPos – firstPos) / 2 + firstPos ;

int selectPos = -1;
for (int i = 0; i

And get results:

I want to change the selection element (make the text white) only if it is on the blue rectangle

How can I solve this problem?

Solution 1:

I’ve done something similar. I can do exactly what you want me to do.
First of all my allogriff works as follows
Get center visible item of RecycleView when scrolling –

It’s my processing device adapter.

The public class DateAdapter expands RecyclerView.Adapter {
private ArrayList dateDataList ;

private static endpoint int VIEW_TYPE_PADDING = 1 ;
private static endpoint int VIEW_TYPE_ITEM = 2 ;
private int paddingWidthDate = 0 ;

private int selectedItem = -1 ;

public DateAdapter(ArrayList dateData, int paddingWidthDate) {
this.dateDataList = dateData;
this.paddingWidthDate = paddingWidthDate ;

}

@General
public DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
last View = LayoutInflater.from(parent.getContext())) inflate(R.layout.list_item_date,
parent, false);
return new DateViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())).inflate(R.layout.list_item_padding,
parent, false) ;

RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
layoutParams.width = paddingWidthDate;
view.setLayoutParams(layoutParams);
return new DateViewHolder(view);
}
}.

General
public void onBindViewHolder(DateViewHolder, int position) {
LabelerDate labelerDate = dateDataList.get(position);
if(getItemViewType(position) == VIEW_TYPE_ITEM) {
if(labelerDate.dateType.equals(BirthDayActivity.DateType.C31)
holder.tvDate.setText(String.valueOf(labelerDate.valueDate)) ;
holder.tvDate.setVisibility(View.VISIBLE) ;
holder.imgSmall.setVisibility(View.VISIBLE) ;

if (position == selected item) {
holder.tvDate.setTextColor(Color.parseColor(#094673)) ;
holder.tvDate.setTextSize(35) ;
holder.imgSmall.setBackgroundResource(R.color.text viewbold) ;

{\an5}other {\an5}
.tvDate.setTextColor (Color.GRAY); holder.
.tvDate.setTextSize(35); holder.
.imgSmall.setBackgroundResource(R.color.grey);
}
}
}

public void setSelecteditem(int selecteditem) {
this.selectedItem = selecteditem;
notifyDataSetChanged();
}

@Override
public int getItemCount() {
return dateDataList.size();
}

@Overview
public int getItemViewType(int position) {
LabelerDate labelerDate = dateDataList.get(position);
if (labelerDate.dateType.equals(BirthDayActivity.DateType.NONE)) {
return VIEW_TYPE_PADDING;
}
return VIEW_TYPE_ITEM;
}

public class DateViewHolder expands RecyclerView.ViewHolder {
public TextView tvDate;
public ImageView imgSmall ;

public DateViewHolder(View itemView) {
super(itemView);
tvDate = (TextView) itemView.findViewById(R.id.tvNumberDate);
imgSmall = (ImageView) itemView.findViewById(R.id.small_marked_dob);
}
}}}.

It’s the main algorithm:

public void getRecyclerviewDate() {
recyclerViewDate = (RecyclerView) findViewById(R.id.recyclerViewDay);
ViewTreeObserver vtoDate = recyclerViewDate.getViewTreeObserver();
vtoDate.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Observer
public boolean onPreDraw() {
recyclerViewDate.getViewTreeObserver().removeOnPreDrawListener(this);
finalWidthDate = recyclingerViewDate.getMeasuredWidth();
itemWidthDate = getResources().getDimension(R.dimen.item_dob_width);
paddingDate = (finalWidthDate – itemWidthDate) / 2;
firstItemWidthDate = paddingDate;
allPixelsDate = 0 ;

LinearLayoutManager end dateLayoutManager = new LinearLayoutManager(getApplicationContext());
dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recycleViewDate.setLayoutManager(dateLayoutManager);
recycleViewDate.addOnScrollListener(new RecyclerView.OnScrollListener() {
@PublicVacuum
onScrollStateChanged(RecyclerView recyclerclingerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
synchronized (this) {
if(newState == RecyclerView.SCROLL_STATE_IDLE){
calculatePositionAndScrollDate(recyclerView);
}
}

}

@Reviewpublic void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);allPixelsDate += dx;}}});if (labelerDates == null)labelerDates = new ArrayList();labelerDates.addAll(genLabelerDate(current month, current year));dateAdapter = new DateAdapter(labelerDates, (int) firstItemWidthDate);recyclerViewDate.setAdapter(dateAdapter);return true;});}/* this is especially important if expectedPositionDate = recyclingerView.getAdapter().getItemCount() – 2) {expectedPositionDate-;}scrollListPositionDate (recyclingerView, expectedPositionDate ;

/* is the most important, if the ExpectedPositionDate

And here is my result:
Get center visible item of RecycleView when scrolling –

Watch this video link, it’s a demo of my app.

Solution 2:

Sometimes we need the whole block of sample code together because we might be missing something. This is what I have, feel free to correct everything, because I could make a little mistake somewhere. And yes, this answer is an extension of @tranhieu’s answer. Thank you @tranhieu.

MainActive.java

com.test;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.Null;
import android.support.v7.app.AppCompatActivity;
import android.os.bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.Support.v7.widget.RecyclerView;
import android.util.log;
import android.view.LayoutInflater;
import android.view.view;
import android.view.viewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView ;

Import java.util.ArrayList ;

the MainActivity public class expands the activity {

private static end string TAG = MainActivity.class.getSimpleName() ;

public float firstItemWidthDate;
public float paddingDate;
public float itemWidthDate;
public int allPixelsDate;
public int finalWidthDate;
private DateAdapter dateAdapter;
private ArrayList labelerDates = new ArrayList() ;

Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getRecyclerviewDate() ;

}

public void getRecyclerviewDate() {
final RecyclerView recyclerViewDate = (RecyclerView) findViewById(R.id.rv_tasks_date);
if (recycleViewDate != invalid) {
recyclerViewDate.postDelayed(new Runnable() {
@Override
public void run() {
setDateValue();
}
}, 300);
recyclerViewDate.postDelayed(new Runnable() {
@Override
public void run() {
recyclerViewDate.smoothScrollToPosition(dateAdapter).getItemCount()-1);
setDateValue();
}
}, 5000);
}
ViewTreeObserver vtoDate = recyclingerViewDate.getViewTreeObserver();
vtoDate.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {)

@General Boolean
onPreDraw() {
recyclingerViewDate.getViewTreeObserver().removeOnPreDrawListener(this);
finalWidthDate = recyclingerViewDate.getMeasuredWidth();
itemWidthDate = getResources().getDimension(R.dimen.item_dob_width);
paddingDate = (finalWidthDate – itemWidthDate) / 2;
firstItemWidthDate = paddingDate;
allPixelsDate = 0 ;

LinearLayoutManager end dateLayoutManager = new LinearLayoutManager(getApplicationContext());
dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recycleViewDate.setLayoutManager(dateLayoutManager);
recycleViewDate.addOnScrollListener(new RecyclerView.OnScrollListener() {
@PublicVacuum
onScrollStateChanged(RecyclerView recyclerclingerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
synchronize (it) {
as (newState == RecyclerView.SCROLL_STATE_IDLE) {
calculatePositionAndScrollDate(recyclerView);
}
}

}

@Review
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
allPixelsDate += dx;
}
});
if (labelerDates == null) {
labelerDates = new ArrayList() ;
}
genLabelerDate();
dateAdapter = new DateAdapter(labelerDates, (int) firstItemWidthDate);
recyclerViewDate.setAdapter(dateAdapter);
dateAdapter.setSelecteditem(dateAdapter.getItemCount() – 1);
return true;
}
});
}

private void genLabelerDate() {
for (int i = 0; i = recyclingerView.getAdapter().getItemCount() – 2) {
expectedPositionDate;
}
scrollListPositionDate (recyclingerView, expectedPositionDate) ;

}

/* this is the most important moment as the scheduled datePositionDate {
private ArrayList dateDataList ;

private static endpoint int VIEW_TYPE_PADDING = 1 ;
private static endpoint int VIEW_TYPE_ITEM = 2 ;
private int paddingWidthDate = 0 ;

private int selectedItem = -1 ;

public DateAdapter(ArrayList dateData, int paddingWidthDate) {
this.dateDataList = dateData;
this.paddingWidthDate = paddingWidthDate ;

}

@General
public DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
last view = LayoutInflater.from(parent.getContext()).inflate(R.).layout.item,
parent, false);
returns a new DateViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,
parent, false) ;

RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
layoutParams.width = paddingWidthDate;
view.setLayoutParams(layoutParams);
return new DateViewHolder(view);
}
}.

@General
public void onBindViewHolder(DateViewHolder, int position) {
LabelerDate labelerDate = dateDataList.get(position) ;
if (getItemViewType(position) == VIEW_TYPE_ITEM) {
holder.tvDate.setText(labelerDate.getNumber()) ;
holder.tvDate.setVisibility(View.VISIBLE) ;

Log.d(TAG, default + position + , selected item) ;
if (position == selected item) {
Log.d(TAG, center + position) ;
holder.tvDate.setTextColor(Color.parseColor(#76FF03)) ;
holder.tvDate.setTextSize(35) ;

} others {
holder.tvDate.setTextColor(Color.WHITE);
holder.tvDate.setTextSize(18);
}
} others {
holder.tvDate.setVisibility(View.INVISIBLE);
}
}.

public void setSelecteditem(int selecteditem) {
this.selectedItem = selecteditem;
notifyDataSetChanged();
}

@Override
public int getItemCount() {
return dateDataList.size();
}

@Override
public int getItemViewType(int position) {
LabelerDate labelerDate = dateDataList.get(position);
if (labelerDate.getType() == VIEW_TYPE_PADDDING) {
return VIEW_TYPE_PADDING;
} otherwise {
return VIEW_TYPE_ITEM;
}.

}

public class DateViewHolder expands RecyclerView.ViewHolder {
public TextView tvDate ;

public DateViewHolder(View itemView) {super(itemView);tvDate = (TextView) itemView.findViewById(R.id.txt_date);}}}}

private class LabelerDate {
private type int;
private string number ;

public String getNumber() {
-number returned;
}

public invalid setNumber (row number) {
this.number = number;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}
}
}

Active_main.xml

ding.xml

dimens.xml

100dp

Solution 3:

Oh, my God. I searched for that answer for almost a week and then I found it. Custom layout managers? No, no, no, no, no, no, no, no, no, no, no, no. Decorator? No, no, no, no, no, no, no, no, no, no, no, no.

This is the easiest way:

The critical part is:

android: stuffingStart=150dp
android: stuffingEnd=150dp
android: clipToPadding=false

Then simply assign SnapHelper to your RecylcerView :

fall snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recycleView)

That’s all I’m saying. The simplest and most ideal solution to the problem.

Solution 4:

I’m using the SnapHelper here:

// init snaphelper
SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recycleView)

// init layoutManager
LinearLayoutManager layoutManager = new LinearLayoutManager (mainActivity);
layoutManager.setOrientation (LinearLayoutManager.HORIZONTAL);
recycleView.setLayoutManager (layoutManager) ;

// Init adapter
adatper.setSnapHelper;
adatper.setLayoutManager ;

adatper.initAdapter(new Float((DisplayHelper.getDisplayWidth(mainActivity) / 2) – (fooViewWidth / 2))).intValue()) ;
recyclerView.setAdapter(adatper) ;

As mentioned in TranHieu, the decision to include 2 elements in front of the pavement (in the start and end position) is a good one.

I don’t like to use the ViewTreeObserver because the code is hard to read. With this technique you also have to control the re-drawing of elements when recycling them.

If you use custom display classes, you can define their width directly in these classes.

For example, here’s my graples…

/**
* Made by Fire Gloves 25/09/15.
*/
@EViewGroup(R.layout.view_padding)
public class PaddingView expands FooView {

mCtx context ;

public PaddingView(context) {
super(context);
mCtx = context;
}

public invalid setWidth(int width) {
setLayoutParams(new LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT))));
}

}

In my adapter I write down the desired padding width, which is equal to (displayWidth / 2) – (realItemWidth / 2).

This is my adapter, do not pay attention to methods that do not match RecyclerView.Adapter, pay attention to the initAdapter method and the onCreateItemView method.

@EBean
public class FooAdapterRecycler expands RecyclerViewAdapterBase {

private final int TYPE_PADDING_VIEW = 0 ;
private final int TYPE_REAL_VIEW = 1 ;

@RootContext
Context ctx;
@Bean(Finder.class)
IF finder ;

SnapHelper snapHelper;
RecyclerView.LayoutManager ;

private int paddingWidth = 0 ;

/**
* preleva i dati dal finder
*/
public void initAdapter (int paddingWidth) {

/*******************************
* THIS CODE IS MANDATORY
*******************************/.

this.gasket width = joint width ;

// Add 1 item for the initial space
mItems = new ArrayList();
Foo foo = new Foo();
mItems.add(foo) ;

// extract real objects from the finder
mItems.addAll(finder.findAll()) ;

// Add 1 element for finite space
mItems = new ArrayList();
Foo foo2 = new Foo();
mItems.add(foo2) ;

}

@Override
public int getItemViewType(int position) {
if (position == 0 | position == getItemCount()-1) {
return TYPE_PADDING_VIEW;
} otherwise {
return TYPE_REAL_VIEW;
}
}.

@Surface
protected FooView onCreateItemView (Parent ViewGroup, int viewType) {

/*******************************
* THIS CODE IS MANDATORY
******************************/.

if (viewType == TYPE_PADDING_VIEW) {
PaddingView view = PaddingView_.build(ctx);
view.setWidth(paddingWidth);
return view;
} otherwise {
return FooView_.build(ctx);
}
}.

public invalid setSnapHelper(SnapHelper snapHelper) {
this.snapHelper = snapHelper;
}

public invalid setLayoutManager (RecyclerView.LayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
}

I use the AndroidAnnotations library, but that’s not necessary.

The hope that this will help

Solution No 5:

As mentioned in another answer, there is no direct way to do this.

That’s probably how you realize what you described in the question.

  1. Know the number of items appearing on the screen.
  2. Select the middle item per program each time you browse the display.
  3. Save a partially transparent image by placing it on the middle element of the recycled image. (You must calculate the coordinates based on the width of the format view or the width of the screen and the width of the overlapping image.
  4. Each time you scroll, update the selected value in the text view under the layout view.

The images should be superimposed so that they appear consistent and can be used as a single control.

Solution No 6:

USING THE CHECKLIST IS A SIMPLER SOLUTION

Here’s another solution with SnapHelper. Let’s start with @TranHieu’s answer here:

https://stackoverflow.com/a/34647005/3944251

and compressed @sector11 here:

https://stackoverflow.com/a/38411582/3944251

I have written the following code, which is also based on the two answers above, but it is simpler and offers a smoother solution using the SnapHelper presented in the Android 24.2.0 support library.

Here’s the MainActivity class. The rest is the same as @sector’s answer11.

import android.graphics.color;
import android.os.bundle;
import android.support.annotation.null;
import android support.v7.app.AppCompatActivity;
import android support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView ;

Import java.util.ArrayList ;

The MainActivity public class expands AppCompatActivity {

private static end string TAG = MainActivity.class.getSimpleName() ;

public float firstItemWidthDate;
public floatWidthDate;
public int allPixelsDate;
public int finalWidthDate;
private DateAdapter dateAdapter;
private ArrayList labelerDates ;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TagDates = new ArrayList();
getRecyclerviewDate() ;

}

public void getRecyclerviewDate() {
final RecyclerView recyclerViewDate = (RecyclerView) findViewById(R.id.rv_tasks_date);
recycleViewDate.postDelayed(new Runnable() {
@Override
public void run() {
//recyclerViewDate.smoothScrollToPosition(dateAdapter.getItemCount()-1)));
setDateValue();
}
}, 300);
ViewTreeObserver vtoDate = recyclerViewDate.getViewTreeObserver();
vtoDate.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {)

@General Boolean
onPreDraw() {
recyclingerViewDate.getViewTreeObserver().removeOnPreDrawListener(this);
finalWidthDate = recyclingerViewDate.getMeasuredWidth();
itemWidthDate = getResources().getDimension(R.dimen.item_dob_width);
firstItemWidthDate = (finalWidthDate – itemWidthDate) / 2;
allPixelsDate = 0 ;

LinearLayoutManager end dateLayoutManager = new LinearLayoutManager(getApplicationContext());
dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerViewDate.setLayoutManager(dateLayoutManager) ;

/* Create a linear helper and attach a recycling view to it. */
final LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recycleViewDate) ;

recyclerViewDate.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
allPixelsDate += dx;
recyclerView.post(new Runnable() {
public void run() {
setDateValue();
}
});
}
}) ;

genLabelerDate();
dateAdapter = new DateAdapter(labelerDates, (int) firstItemWidthDate);
recycleingerViewDate.setAdapter(dateAdapter);
dateAdapter.setSelect item(dateAdapter.getItemCount() – 1);
gives true;
}
});
}.

private void genLabelerDate() {
for (int i = 0; i {
private ArrayList dateDataList ;

private static endpoint int VIEW_TYPE_PADDING = 1 ;
private static endpoint int VIEW_TYPE_ITEM = 2 ;
private int paddingWidthDate = 0 ;

private int selectedItem = -1 ;

public DateAdapter(ArrayList dateData, int paddingWidthDate) {
this.dateDataList = dateData;
this.paddingWidthDate = paddingWidthDate ;

}

@DateAdapter.DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext())).inflate(R.layout.element, parent, false);if (viewType == VIEW_TYPE_PADDDING) {RecycleView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();layoutParams.width = paddingWidthDate;view.setLayoutParams(layoutParams);}reflate new DateViewHolder(view);}.

@Check
on BindViewHolder (DateAdapter.DateViewHolder holder, int position) {
LabelerDateDate = dateDataList.get(position) ;
if (getItemViewType(position) == VIEW_TYPE_ITEM) {
holder.tvDate.setText(labelerDate.getNumber()) ;
holder.tvDate.setVisibility(View.VISIBLE) ;

Log.d(TAG, default + position + , selected item) ;
if (position == selected item) {
Log.d(TAG, center + position) ;
holder.tvDate.setTextColor(Color.parseColor(#76FF03)) ;
holder.tvDate.setTextSize(35) ;

} others {
holder.tvDate.setTextColor(Color.WHITE);
holder.tvDate.setTextSize(18);
}
} others {
holder.tvDate.setVisibility(View.INVISIBLE);
}
}.

public void setSelecteditem(int selecteditem) {
this.selectedItem = selecteditem;
notifyDataSetChanged();
}

@Override
public int getItemCount() {
return dateDataList.size();
}

@Override
public int getItemViewType(int position) {
LabelerDate labelerDate = dateDataList.get(position);
if (labelerDate.getType() == VIEW_TYPE_PADDDING) {
return VIEW_TYPE_PADDING;
} otherwise {
return VIEW_TYPE_ITEM;
}.

}

public class DateViewHolder expands RecyclerView.ViewHolder {
public TextView tvDate ;

public DateViewHolder(View itemView) {super(itemView);tvDate = (TextView) itemView.findViewById(R.id.txt_date);}}}}

private class LabelerDate {
private type int;
private string number ;

public String getNumber() {
-number returned;
}

public invalid setNumber (row number) {
this.number = number;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}
}
}

Solution No 7:

You can use the LinearSnapHelper helper

Attach the unit to your reprocessor.

fall snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(this)

Then use
snapHelper.findSnapView (horizontalScrollView.layoutManager) to get the middle view?

Solution No 8:

Use the EcoGallery library for this function:
https://github.com/falnatsheh/EcoGallery

Solution No 9:

I needed something like this first, not this. But I was able to adapt @TranHieu’s solution to my needs, so I voted for his solution.

I wanted to create a fully horizontal scrolling screen that would appear after refreshing the user’s scrolling position to MostVisibleItem.

The setting :

private void setUpScrolling() {
mRecyclerVIew.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mRecyclerVIew.getViewTreeObserver().removeOnPreDrawListener(this);
CustomScrollListener listener = (CustomScrollListener) mScrollListener;
listener.width = mRecyclerVIew.getMeasuredWidth();
listener.dx = 0;
gives true;
}
});
}.

Listen to me. Listen to me:

closed class CustomScrollListener expands OnScrollListener {
closed class int mLastDx = 0 ;
closed class int width = 0 ;
int dx = 0 ;

@General
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (mLastDx!= dx) {
scrollToMostVisibleItem();
} otherwise {
dx = 0;
mLastDx = 0;
}
}
super.onScrollStateChanged(recycleView, newState);
}

@Review
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
this.dx += dx;
}

private void scrollToMostVisibleItem() {
int direction = (dx > 0) ? 1 : -1 ;
dx = Math.abs(dx) ;
int shiftCount = Math.round(dx / width) ;
int pixelShift = dx % width ;
if (pixelShift > width / 2) {
shiftCount++ ;
}

float targetScrollPixels = shiftCount * width;float finalScrollPixels = (targetScrollPixels – dx) * direction;if (finalScrollPixels != 0) {mRecyclerVIew.smoothScrollBy((int) finalScrollPixels, 0);mLastDx = (int) finalScrollPixels;dx = 0;}}.

Solution No 10:

In my case, I opted for a different approach.

you might find some deaf people here: RecyclerView – how to select the central element visible when scrolling1

In my opinion, my solution is simpler than the others.

Good luck!

You May Also Like

Vivo Y20s with 5000mAh Battery with 18W Flash Charge

Vivo launched the Vivo Y20 in Pakistan. The latest Y-series is equipped…

Text with gradient in Android –

Development issue/problem: How can I extend TextView to enable the drawing of…

S/MIME control isn’t available error [Solved]

If you are also encountering this error, here are some troubleshooting steps…

Microsoft releases Windows 10 Build 21296

Microsoft today presented Windows 10 build 21296 to Insiders on the Dev…