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;
}
});
Related
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 ?
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);
}
});
I am working on android app and using recyclerview and scrollview in some activities. Now I want to show some layout when recyclerview/scrollview scrolls in up direction and hide the layout when it scrolls in down direction.
I want a function which can tell that recyclerview/scrollview is scrolling in up or down direction.
Please help if anyone know how to do this for both recyclerview and scrollview.
Thanks a lot in advanced.
Try this way:
private static int firstVisibleInListview;
firstVisibleInListview = yourLayoutManager.findFirstVisibleItemPosition();
In your scroll listener:
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
super.onScrolled(recyclerView, dx, dy);
int currentFirstVisible = yourLayoutManager.findFirstVisibleItemPosition();
if(currentFirstVisible > firstVisibleInListview)
Log.i("RecyclerView scrolled: ", "scroll up!");
else
Log.i("RecyclerView scrolled: ", "scroll down!");
firstVisibleInListview = currentFirstVisible;
}
A sure short answer for this issue
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int x = scrollY - oldScrollY;
if (x > 0) {
//scroll up
} else if (x < 0) {
//scroll down
} else {
}
}
});
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;
}
}
});
Which statement should i put in "Recyclerview_position_is_bottem" to satisfy condition that now recyclerview position is bottem to execute function
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (Recyclerview_position_is_bottem){
isScrolled=0;
skip = skip + 5;
remove = 1;
feedItems.add(null);
eventsRecyclerView.notifyItemInserted(feedItems.size());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Loadmore(eventsRecyclerView,1);
}
}, 500);
}
}
});
first / last visible child depends on the LayoutManager.
If you are using LinearLayoutManager or GridLayoutManager, you can use
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
For example:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may be prefered to layout more items than visible for caching.
You can use LayoutManager's object which you set on your recyclerView like this:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int totalItems = layoutManager.getItemCount();
int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
if (totalItems - 1 == lastVisibleItem){
isScrolled=0;
skip = skip + 5;
remove = 1;
feedItems.add(null);
eventsRecyclerView.notifyItemInserted(feedItems.size());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Loadmore(eventsRecyclerView,1);
}
}, 500);
}
}
});