SnapHelper Item Position
Asked Answered
C

4

21


I'm using vertical RecyclerView to list my items and SnapHelper to snap center item. The idea is to randomize selection, so user swipe screen or shake the device and it is scrolling to random position.
Number of items is 20, however I use Integer.MAX_VALUE for the number of elements in RecyclerView and initialize RecyclerView with position Integer.MAX_VALUE / 2 to create some kind of endless list.
To scroll to random position on device shake I need to know current snapped item position.
Is there any way to do it?

Here is my Fragment code:

public class PlaceListFragment extends Fragment {

private static final String TAG = "PlaceListFragment";
public static final String ARG_KEY1 = "key1";
private ArrayList<PlaceItem> places;

private RecyclerView recyclerView;

private SensorManager sensorManager;
private float accelValue;
private float accelLast;
private float shake;

SnapHelper snapHelper;

Vibrator vibe;


    public static PlaceListFragment newInstance() {

        Bundle args = new Bundle();

        PlaceListFragment fragment = new PlaceListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static PlaceListFragment newInstance(ArrayList<PlaceItem> places) {

        Bundle args = new Bundle();
        args.putParcelableArrayList(PlaceListActivity.KEY_PLACES, places);
        PlaceListFragment fragment = new PlaceListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        places = getArguments().getParcelableArrayList(PlaceListActivity.KEY_PLACES);

        accelValue = SensorManager.GRAVITY_EARTH;
        accelLast = SensorManager.GRAVITY_EARTH;
        shake = 0.00f;
        vibe = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_place_list, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.place_list);

        snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);
        recyclerView.setOnFlingListener(snapHelper);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new PlaceListAdapter(getActivity(), places));
        recyclerView.scrollToPosition(PlaceListAdapter.MIDDLE);

        sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);

        return v;
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private final SensorEventListener sensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            accelLast = accelValue;
            accelValue = (float) Math.sqrt((double) (x*x + y*y + z*z));
            float delta = accelValue - accelLast;
            shake = shake * 0.9f + delta;

            if (shake > 12) {
                vibe.vibrate(200);

            }

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

}

And here is adapter:

public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.PlaceAdapterHolder> {
    private final FragmentActivity context;
    public static final int HALF_MAX_VALUE = Integer.MAX_VALUE/2;
    public static int MIDDLE;
    private List<PlaceItem> placeItems;

    public static class PlaceAdapterHolder extends RecyclerView.ViewHolder {
        private ImageView image;
        private TextView textMain;
        private TextView textRating;


        public PlaceAdapterHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.icon);
            textMain = (TextView) itemView.findViewById(R.id.txt_main_line);
            textRating = (TextView) itemView.findViewById(R.id.txt_right_field);
        }

        public void bindPlace(PlaceItem placeItem) {
            String placeName = placeItem.getName() == null? "?":placeItem.getName();
            String firstLetter = placeName.substring(0, 1);
            ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
            int color = generator.getColor(placeName);
            TextDrawable drawable = TextDrawable.builder()
                    .beginConfig()
                    .toUpperCase()
                    .endConfig()
                    .buildRect(firstLetter, color);
            image.setImageDrawable(drawable);
            textMain.setText(placeItem.getName());
            textRating.setText(placeItem.getRating());
        }
    }

    public PlaceListAdapter(FragmentActivity context, List<PlaceItem> placeItems) {
        this.context = context;
        this.placeItems = placeItems;
        MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % placeItems.size();
    }

    @Override
    public PlaceListAdapter.PlaceAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_list_one_line_item, parent, false);
        return new PlaceAdapterHolder(view);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public void onBindViewHolder(PlaceListAdapter.PlaceAdapterHolder holder, final int position) {
        final PlaceItem placeItem = getItem(position);
        holder.bindPlace(placeItem);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = context.getSupportFragmentManager();
                PlaceDetailsFragment dialog = PlaceDetailsFragment.newInstance(getItem(position));
                dialog.show(fm, "DETAILS_DIALOG");
            }
        });
    }

    private PlaceItem getItem(int position)
    {
        return placeItems.get(position % placeItems.size());
    }
}
Canon answered 18/3, 2017 at 18:18 Comment(0)
C
65

I used this on a project that had a RecyclerView with SnapHelper, not sure if it is what you want.

mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    mAdapter = new DemoSlidesAdapter(getApplicationContext());
    mRecyclerView.setAdapter(mAdapter);

    final SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(mRecyclerView);

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == RecyclerView.SCROLL_STATE_IDLE) {
                View centerView = snapHelper.findSnapView(mLayoutManager);
                int pos = mLayoutManager.getPosition(centerView);
                Log.e("Snapped Item Position:",""+pos);
            }
        }
    });
Cabalistic answered 21/3, 2017 at 16:25 Comment(3)
Works perfect, thank you! Didn't think about it:) My solution was to use linearLayoutManager.findFirstCompletelyVisibleItemPosition();, linearLayoutManager.findLastCompletelyVisibleItemPosition(); and then find a middle of itCanon
Perfect! Thank you!And
perfect answer. I was also looking for it at #48063071Buckman
U
3

I try to use this code with a PagerSnapHelper to mimic the pager behaviour and it was useful but i found some corner cases to solve, if you move fast from the last page to the first one and keep swapping until see the boundarie then the IDLE state doesnt happen and you lose your index. to solve that I move out the position from the IF and add a extra condition for this corner case.

 override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
             super.onScrollStateChanged(recyclerView, newState)
             val centerView = snapHelper.findSnapView(mLayoutManager)
             val pos = mLayoutManager.getPosition(centerView!!)
             if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
                 Log.d("BINDING", "positionView SCROLL_STATE_IDLE: $pos")
             }
         }

Code is in kotlin hope it helps

Unroot answered 21/7, 2018 at 3:43 Comment(0)
F
1
private fun recyclerViewScrollListener() = object: RecyclerView.OnScrollListener() {

      override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
          super.onScrollStateChanged(recyclerView, newState)

          if (newState == RecyclerView.SCROLL_STATE_IDLE) {
              // layoutManager is LinearLayoutManager
              val pos = layoutManager.findFirstCompletelyVisibleItemPosition()
              Log.e(TAG, "onScrollStateChanged: $pos")
          }
      }
  }

recyclerView.setOnScrollListener(recyclerViewScrollListener())
Ferocious answered 27/7, 2021 at 5:20 Comment(0)
Q
-2
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                ....
            }
        })
Quantity answered 6/12, 2019 at 10:43 Comment(1)
Could you please explain why and how this code snippet provides an answer to the question? Thank you...Solenoid

© 2022 - 2024 — McMap. All rights reserved.