如何在水平回收视图中获取当前位置?

时间:2017-05-03 17:17:29

标签: android android-recyclerview

我有一个recyclerview和一个imageview,我想在滚动上更改imageview图像,但我不知道如何获取当前位置,然后从列表中获取我的图像,然后将其加载到Picasso到imageview!

这是我的代码:

<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"/>

我的适配器:

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();
    }
}

这是我的活动

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
                }
            }
        });

2 个答案:

答案 0 :(得分:17)

    int op = 0;
    while(op != -1) {
        double num1, num2;
        Scanner operation = new Scanner(System.in);
        System.out.println("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide");
        System.out.print("Which operation would you like to perform?   ");
        op = operation.nextInt();

        if((op != 1) && (op != 2) && (op != 3) && (op != 4)){
            System.out.println("That wasn't an option...");
        }else{
            System.out.print("First number:   ");
            num1 = operation.nextDouble();
        System.out.print("Second number:   ");
            num2 = operation.nextDouble();


            if(op==1){
                Add(num1, num2);
            }else if(op==2){
                Sub(num1, num2);
            }else if(op==3){
                Mult(num1, num2);
            }else if(op==4){
                Div(num1, num2);
            }
    }

这里

 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));*/

        }
    });

这使得水平回收者视图能够显示并滚动整个项目,这样就不会卡在中间,如半可见,另一半看不见

答案 1 :(得分:0)

从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));

                    }

                }
            });