Gridview and RecyclerView synchronized scrolling - android

I am trying to build a layout with recyclerview and gridview side by side. I need to sync scroll of both views. Currently, I managed to sync recyclerView with gridView:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
gridView.scrollBy(0, dy);
}
});
The problem: when gridView is scrolled - cell that are outside of screen do not appear, it's empty.
Second problem: how to sync gridview scrolling with recyclerview?
Thank you for your answers!

Related

Horizontal RecyclerView truncating items when their heights are different

I have an EpoxyRecyclerView with several horizontal Carousels. These carousels contain items of varying heights. The problem here is when I scroll the carousel the items that are longer are sometimes truncated as depicted in the image below. How can I get around this problem? I have tried to replace Carousel with an EpoxyRecyclerView but the behavior is still the same.
(Image credits to SO post)
EpoxyRecyclerView inherits from AndroidX RecyclerView v1.2.1
I have searched all over SO for solutions and just found one that has worked but doesn't seem to be an efficient one.
LinearLayoutManager layoutManager = new LinearLayoutManager(context,
LinearLayoutManager.HORIZONTAL, false);
layoutManager.setMeasurementCacheEnabled(false);
YourRecyclerView.setLayoutManager(layoutManager);
YourRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
layoutManager.requestLayout();
}
});

How to find the item that shows up on top of the RecyclerView while scrolling [duplicate]

This question already has answers here:
Get visible items in RecyclerView
(11 answers)
Closed 3 years ago.
I have been looking for a proper API for what I need to do and I am all but certain that there has to be an API. But I am not finding it.
Here is my problem:
I am displaying an ArrayList inside a recycler view. What I need is to know what item in the ArrayList is showing at the top of the recycler view as I scroll up and down. Here is how I set up my recycler view (nothing special):
private ArrayList<Transaction> filteredTransactions = new ArrayList<>();
...
recyclerView = view.findViewById(R.id.feed_recycle_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager((getActivity()));
adapter = new FeedAdapter(filteredTransactions);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
I came across
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
that provides the listener I need, but I can't figure out how to get the item that appears on top of the view from it.
Thanks in advance
findFirstCompletelyVisibleItemPosition() will return the adapter position of the first fully visible view.
You can do it like this:
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()).findFirstCompletelyVisibleItemPosition();
// Get the item from the list
mList.get(itemPoition)
}
You can use the following methods of the LinearLayoutManager
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
Read the official document
There are some methods which are available in Linear Layout manager like
findFirstCompletelyVisibleItemPosition()
int findFirstVisibleItemPosition()
int findLastCompletelyVisibleItemPosition()
findLastVisibleItemPosition()
Go through the documentation it will surely help you.

How to find out which item on focus after scrolling the RecyclerView Android?

How do I get the position of a list item that's on focus after scrolling the RecyclerView? Will this work?
recyclerview1.addOnScrollListener(new RecyclerView.OnScrollListener())
If yes, then how?
Yes there are two ways of doing this.
One using onBindViewHolder , in onBindViewHolder add
Log.d("onBindViewHolder", "position =>"+position);
count++; //for the position
but remember in this way count call several time for each news and you must count only in first call.
Other way around is using layoutManagers i.e LinearLayoutManager or GridLayoutManager by doing this
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
OR for scrollListener you can do this
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//you can use other methods as per your requirements
Toast.makeText(mContext, "Scrolled to"+layoutManager.findLastCompletelyVisibleItemPosition(), Toast.LENGTH_SHORT).show();
})
Hope this helps you.
I just faced this problem, and i got I put the recyclerView in scrollView So please never put the recyclerview inside the scrollView that may also cause mLayoutManager.findFirstVisibleItemPosition() that always return a fixed value. and also check recyclerview.setNestedScrollingEnabled(false);

How to reset position ScrollView inside RecycleView everytime RecycleView Scrolled

I have a RecycleView scroll Horizontal and every item of it has a ScrollView scroll Vertical.
When I scroll down the ScrollView,it will remember that position and doesn't reset to the top when i scroll the RecycleView to another item,like this :
Could anyone give me an advice to make the ScrollView reset to the top when i scroll the RecycleView ?
you can call this :
scrollView.fullScroll(ScrollView.FOCUS_TOP)
or this :
scrollView.scrollTo(0,0);
and you can use this in your onBindeViewHolder in your adapter when you are initializing the viewHolder or you can attach scroll Listener to your RecyclerView and detect scrolling with the scroll Listener
maybe something like this !
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int fVitem = linearLayoutManager.findFirstVisibleItemPosition();
int lVitem = linearLayoutManager.findLastVisibleItemPosition();
for (int i = fVitem; i <= lVitem; i++) {
View view = linearLayoutManager.findViewByPosition(i);
ScrollView scrollView;
if (view.getTag() != null) scrollView = (ScrollView) view.getTag();
else {
scrollView = view.findViewById(R.id.myScrollView);
view.setTag(scrollView);
}
scrollView.scrollTo(0, 0);
}
}
});
but i think if you use obBindViewHolder would be better for performance
Call this, when you detect scrolling the RecyclerView
yourScrollView.fullScroll(ScrollView.FOCUS_UP);

Change firstCompletelyVisibleItemPosition() textcolor when recyclerview scrolling

I want to implement a recyclerview where the first visible items text color should change when scrolling. ie, the first visible item of recycler view have different color than other row items. I already tried several techniches but none of those worked. below is snap of my code
mListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!getUserVisibleHint()){
return;
}
int firstVisiblePosition=layoutManager.findFirstCompletelyVisibleItemPosition();
if (firstVisiblePosition==mLastReadPosition)return;
if(firstVisiblePosition!=-1){
View view =layoutManager.findViewByPosition(firstVisiblePosition);
listAdapter.setViewRead(view,firstVisiblePosition);
if (mLastReadView!=null){
listAdapter.setViewUnRead(mLastReadView);
}
mLastReadView=view;
mLastReadPosition=firstVisiblePosition;
}
}
this work when I scroll reversely,but not working on forward scrolling. I already searched for libraries in GitHub, If you know any libraries please suggest. Or help me for resolve this>?

Categories

Resources