Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current position in horizontal recyclerview?

I have a recyclerview and an imageview, I want to change imageview image on scroll but I don't know how to get the current position and by it get my image from the list then load it with Picasso to imageview!

here is my code:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_background"
        android:background="#80000000"
        android:scaleType="centerCrop"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_centerInParent="true"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp"/>

My Adapter :

public class Place_Adapter extends RecyclerView.Adapter<Place_View_Holder> {
    private List<Place> objects;
    private Activity context;

    public Place_Adapter(List<Place> objects,Activity context){
        this.context=context;
        this.objects=objects;

    }

    @Override
    public Place_View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_item, parent, false);
        Place_View_Holder place_view_holder=new Place_View_Holder(v);
        return place_view_holder;
    }

    @Override
    public void onBindViewHolder(Place_View_Holder holder, int position) {
        Picasso.with(context)
                .load(objects.get(position).getItem_Image())
                .placeholder(R.mipmap.image)
                .error(R.mipmap.image)
                .into(holder.image);
        holder.place_name.setText(objects.get(position).getPName());
        holder.place_in_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, MapsActivity.class));
            }
        });

    }

    @Override
    public int getItemCount() {
        return objects.size();
    }
}

And this is my Activity

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    ////here i want to change imageview image
                }
            }
        });
like image 581
Mohsen mokhtari Avatar asked May 03 '17 17:05

Mohsen mokhtari


People also ask

Can RecyclerView be horizontal?

We can make Horizontal RecyclerView or Horizontal Scrolling in RecyclerView in Android using LinearLayoutManager. If you've used LinearLayout, you might have noticed that you can set the layout orientation to both horizontal and vertical.

What is RecyclerView ViewHolder?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.

How to get selected item position in RecyclerView in Android kotlin?

class MyViewHolder extends RecyclerView. ViewHolder implements View. OnClickListener{ @Override public void onClick(View v) { // here you use position int position = getAdapterPosition(); ... } } } Save this answer.

What is the use of onBindViewHolder in Android?

bindViewHolder. This method internally calls onBindViewHolder to update the ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView.


2 Answers

Find the Position of Current Item from Recycleview

recyclerView.addOnScrollListener ( new RecyclerView.OnScrollListener() {  

              @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                        //Dragging
                    } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                        int position = linearLayoutManager.findFirstVisibleItemPosition();
                        Log.e("position", String.valueOf(position));

                    }

                }
            });
like image 156
Shweta Chandarana Avatar answered Oct 25 '22 14:10

Shweta Chandarana


 SnapHelper mSnapHelper = new PagerSnapHelper();
 mSnapHelper.attachToRecyclerView(recyclerView);
 LayoutManager recylerViewLayoutManager = new LayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false) ;;
 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                   //Dragging 
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    review_position = recylerViewLayoutManager.findFirstVisibleItemPosition();
                 /*
                    Here load the Image to image view with picaso
                 */
                 Picasso.with(itemView.getContext())
                    .load(url)
                    .into(yourImageView, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                        }
                    });
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            int firstVisibleItem = recylerViewLayoutManager.findFirstVisibleItemPosition();
               /* Log.e ("VisibleItem", String.valueOf(firstVisibleItem));*/

        }
    });

Here

SnapHelper mSnapHelper = new PagerSnapHelper();

This makes the horizontal recycler view to show and scroll entire one item at time so you cannot get stuck in middle like half visible and another half invisible

like image 22
GeekyInt Avatar answered Oct 25 '22 16:10

GeekyInt