dev90
dev90

Reputation: 7549

How to set background color of current item in scroll view

I have a horizontal recyclerview which can show 1.5 items at a time, means 1 complete item on screen, and partial view of second item.

I want to change the background color or set alpha of the main item or the item at the current position.

If i use following code it sets alpha for second item.

  if(position == 1){
            holder.itemView.setAlpha(0.5f);
        }else{
            holder.itemView.setAlpha(1.0f);
        }

but i want to know a dynamic way to set alpha 0.5f for rest of the items, and 1.0f for the current index.

Upvotes: 0

Views: 70

Answers (1)

Goutham
Goutham

Reputation: 347

Any visible child depends on the LayoutManager. If you are using LinearLayoutManager or GridLayoutManager, you can use these functions

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

For example:

GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager()); int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();

you can use these functions..

Upvotes: 1

Related Questions