Development issue/problem:

I use Scroll for Infinite Carousel to collect time and have discovered that it is not the best approach (last question).

Now I’ve found the recycler view, but I can’t get the current scroll offset in the X direction of the recycler view? (Let’s say each element is 100px wide and the second element is only visible at 50%, so the scroll offset is 150px).

FIXED

I just found a way to keep track of the X position by updating the value with the onScrolled reminder, but I prefer to keep track of the actual value instead of keeping track of it all the time!

private int overallXScroll = 0 ;
//….
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy) ;

overallXScroll = overallXScroll + dx ;

Log.i (check, global scrolling ofX-> + global scrolling ofX) ;

}
});

How can I solve this problem?

Solution 1:

RecyclerView already has a method for obtaining horizontal and vertical scroll offset

mRecyclerView.computeHorizontalScrollOffset()

This works for RecyclerViews that contain cells with the same height (for vertical scroll offset) and width (for horizontal scroll offset).

Solution 2:

Solution 1: setOnScrollListener

Save the X position as a class variable and update any change in the onScrollListener. Make sure you have not reset XScroll’s global scrolling (e.g. onScreenRotationChange).

private int overallXScroll = 0 ;
//….
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy) ;

overallXScroll = overallXScroll + dx;
Log.i(check,overall X = + overallXScroll) ;

}
});

Solution 2: Calculate the current position.

In my case I have a horizontal list of time values (0:00, 0:30, 1:00, 1:30 … 23:00, 23:30). I calculate the time from a point in the middle of the screen (calculation point). So I need the exact position of the X-scrolling of my RecycleViews

  • Each time element has the same width of 60dp (for info: 60dp = 30 minutes, 2dp = 1 minute).
  • The first element (header element) has an extra filling to put 0 minutes in the middle.

private int mScreenWidth = 0 ;
private int mHeaderItemWidth = 0 ;
private int mCellWidth = 0 ;

Override
protected by blank onCreate (StoredInstanceState Bundle) {
super.onCreate(storedInstanceState) ;

LinearLayoutManager mLLM = (LinearLayoutManager) getLayoutManager();
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
this.mScreenWidth = displaymetrics.widthPixels ;

mCellWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources()
.getDisplayMetrics())

final int mOffset = (this.mScreenWidth / 2) – (mCellWidth / 2) ;

mHeaderItemWidth = mOffset + mCellWidth ;

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()) {

@Review
public null and void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy) ;

View firstVisibleItem = mLLM.findViewByPosition(mLLM.findFirstVisibleItemPosition() ;

int leftScrollXCalculated = 0;
if (firstItemPosition == 0){

leftScrollXCalculated = Math.abs(firstVisibleItem.getLeft());
}
plus

leftScrollXCalculated = (mHeaderItemWidth – mCellWidth) + firstItemPosition * mCellWidth + firstVisibleItem.getLeft();
}

Log.i(asdf, calculated X left = + leftScrollXCalculated) ;

}
});

}

Solution 3:

Thank you @umar-qureshi for pointing this out to us correctly! It seems that you can use Offset, Extent and Range to set a percentage for scrolling, so that

Percentage = 100 * Offset / (Range – Degrees)

For example (will be placed in OnScrollListener) :

int offset = recyclingerView.computeVerticalScrollOffset() ;
int extent = recyclingerView.computeVerticalScrollExtent() ;
int range = recyclingerView.computeVerticalScrollRange() ;

int percent = (int)(100.0 * offset / (float) (range – range)) ;

Log.i(RecyclerView, Scroll Percentage: + Percentage + %) ;

Solution 4:

the CustomRecyclerView public class expands RecyclerView {

public CustomRecyclerView(Context) {
super(context);
}

public CustomRecyclerView(Context, attribute set attributes) {
super(context, attributes);
}

public CustomRecyclerView(Context, AttributeSet attrs, int defStyle) {
super(Context, attrs, defStyle);
}

public int getHorizontalOffset() {
return super.computeHorizontalScrollOffset();
}
}

if you need compensation:

recycleView.getHorizontalOffset()

Solution No 5:

If you need to know the current page and the offset of the width of RecyclerView, you can try something like this:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerclingerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Review
public null and void onScrolled(RecyclerView recyclerclingerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy) ;

final int scrollOffset = recyclingerView.computeHorizontalScrollOffset();
final int width = recyclingerView.getWidth();
final int currentPage = scrollOffset / width;
final float pageOffset = (float) (scrollOffset % width) / width ;

// The following lines are just an example of how
can be used as (currentPage == 0) {
leftIndicator.setAlpha(pageOffset);
}

as (adapt.getItemCount())

If currentPage has index 0, the left indicator (arrow) is transparent, just like the right indicator if there is only one page (in the example, at least one element of the adapter must always be displayed, so there is no check for an empty value).

This way you have almost the same functionality as if you were using the page reminder of ViewPager.OnPageChangeListener.

Solution No 6:

By canceling RecyclerView onScrolled(int dx,int dy), you can get the scroll offset. But if you add or remove an element, the value may not be correct.

public The TempRecyclerView class extends RecyclerView {
private int offsetX = 0 ;
private int offsetY = 0 ;
public TempRecyclerView(Context) {
super(context) ;
}.

public TempRecyclerView(Context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public TempRecyclerView(Context, @Nullable AttributeSet attrs,int defStyle){
super(context, attrs, defStyle);
}
/**
* Called when the scroll position of this RecyclerView changes.
Subclasses use this method to respond to navigation in the
Adapter Record, rather than using an explicit listener.
*

This method is always called up in the presence of the listeners. If class
has to do some extra maintenance or bookkeeping after scrolling, but class
has to do some extra maintenance or bookkeeping before the auditors start working, then this is a good place to do that.

*/
@Override
public void onScrolled(int dx, int dy) {
// super.onScrolled(dx, dy);
offsetX+=dx;
offsetY+=dy;
}
}.

Good luck!

recyclerview computeverticalscrolloffset,android-recyclerview set scroll offset,android-recyclerview get scroll y,how to get last position of recyclerview in android,how to get position of recyclerview in android,how to get current visible position in recyclerview android,scrolltopositionwithoffset,custom layout manager android,recyclerview layoutmanager,recyclerview layoutmanager kotlin,computeverticalscrolloffset android,android-recyclerview scroll to position,android-recyclerview get current scroll position

You May Also Like

Steps To Start And Join Live Voice Chat On Telegram

To attract more users to its cloud-based instant messaging solution, Telegram announced…

4 Browsers that are Super Awesome

Facebook Twitter Pinterest Reddit Messenger WhatsApp Xbox One supports a wide range…

URBN Power Bank Review, Made In India Power Bank, 18W, QC 3.0 + PD

The Indian smartphone accessory brand URBN has recently launched its latest thin…

What You Need to Do If You See a Netflix VPN Error

A global pandemic changed millions of lives in a few months. Many…