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>?
Related
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();
}
});
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.
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!
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);
I create parallax effect moving the image background depends on location of first element of recycler view only.
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if ((holder = recyclerView.findViewHolderForAdapterPosition(0)) != null) {
int offset = recyclerView.findViewHolderForAdapterPosition(0).itemView.getTop() / 10;
backgroundPhoto.setTop(offset);
}
The problem is : when the first item of recycler(header) scroll off the screen, background picture somehow jump ot initial position.
Once the view is scrolled off the screen the RecyclerView might still be able to access it but it's getTop() value will have some random value or 0 which will cause your parallax effect to jump.
You could keep a field in your class which saves the currently "scrolled distance" and add dx to it in the onScrolled(...) callback and use this value to calculate the parallax offset.