Detect centered item in recyclerView using Carousel Layout Manager - android

I am using RecyclerView with CarouselLayoutManager library and i want to detect the most elevated item.
I have the method below to know if the user scrolled the recyclerview and change the current most elevated recyclerview item.
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int overallXScroll = 0;
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
//if the scroll amount will change the recyclerView item order :
if(overallXScroll > 100){
Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();
overallXScroll = 0;
}
Log.i("check","overallXScroll->" + overallXScroll);
}
});
This method works very slow and with a delay , what can i do ?

Related

Get recyclerview item position

I have a layout, in which RecyclerView and a view is used. View is overlapping the RecyclerView at centre. I want to get the position of item at position of view when scroll or when recyclerview scroll stops.
This is how you can get the item position.
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int itemPoition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
}
});

RecyclerView get item when it leaves screen

How can I get object from recyclerview when it leaves screen while scrolling? For example I have note with id 11 how can I get this note id whenever it leaves screen?
One way you could do it:
Boolean upScrolling;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager mgr = (LinearLayoutManager) recyclerView.getLayoutManager();
int topPosition = mgr.findFirstVisibleItemPosition();
/// your code
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy < 0) {
upScrolling = true;
} else if (dy > 0) {
upScrolling = false;
}
}
});
After scrolling up, in the listener you get the 1st visible item. At the position your code you write the code to check if the 1st visible item is the one below the item you want. If this is the case then you get the time it went off the screen.
Edited to check if it scrolls up or down.

check if recyclerview item position has changed while scrolling

Hi I am trying to check whether recycler view current position has changed. and get that position while scrolling. Tried getting it inside onbind but it gets called only once.
Searched for answers and found this. But looking for some workaround to implementing it. Let me know
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final News news = episodeList.get(position);
Log.e(TAG, "onBindViewHolder: " + holder.getAdapterPosition() );
}
Above here all the positions are logged all together.
try this :
just use your own layout manager , because my recycler view scrolling horizontally
Save your X-Position as class variable and update each change within the onScrollListener. Ensure you don't reset overallXScroll (f.e. onScreenRotationChange)
private int overallXScroll = 0;
//...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
Log.i("check","overall X = " + overallXScroll);
}
});

How is it possible get scroll coordinates of a RecycleView?

Want to get scroll coordinates of a RecycleView like this:
int scrollX = recycleView.getScrollX();
But it always returns 0, even though view is already scrolled. Why it does not help? How is it possible get the shifted distance of the content?
Try with this
int scrollX = recycleView.computeHorizontalScrollOffset();
You can use OnScrollListener to achieve this:
private int xScroll = 0;
...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
xScroll += dx;
}
});

View flicking while hiding/showing on scroll in recycler view

I have linear layout at the bottom and I want to hide that view on scroll up and show on scroll down. I was able to achieve that with scroll listener on recycler view . But there is one problem, when you are scrolling slow view is flickering (showing and hiding fast).
This is my code
bottom = (LinearLayout) getActivity().findViewById(R.id.linerabottom);
recycleList.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
bottom.setVisibility(View.GONE);
} else {
bottom.setVisibility(View.VISIBLE);
}
}
});
Here is a video of the issue https://goo.gl/photos/TwUJjmPUA4kJCsaR8 .
Can you help me to find out what is the issue ?
Thank you.
This is normal, because your dy at some point of time fluctuates between dy >= 0 and dy < 0. If you want to achieve sort of quick return view, you should bound it to something like this:
recycleList.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mTotalDy += dy;
if (dy > 0 && mTotalDy >= bottom.getHeight()) {
bottom.setVisibility(View.GONE);
} else if(recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE && bottom.getVisiblity() == View.GONE) {
bottom.setVisibility(View.VISIBLE);
mTotalDy = 0;
}
}
});

Categories

Resources