Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SnapHelper Item Position


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());
    }
}
like image 962
Serg P Avatar asked Mar 18 '17 18:03

Serg P


People also ask

What is SnapHelper in Android?

SnapHelper is a helper class that is used to snap any child of our RecyclerView. With the help of this class, we can display the specific number of RecyclerView items on our screen, and we can avoid the RecyclerView children's display inside our RecyclerView.

How to use SnapHelper in RecyclerView in Android?

You can now just use a SnapHelper. If you want a center-aligned snapping behavior similar to ViewPager then use PagerSnapHelper: SnapHelper snapHelper = new PagerSnapHelper(); snapHelper. attachToRecyclerView(recyclerView);

What is snapping in Android?

1. calculateDistanceToFinalSnap. Override this method to snap to a particular point within the target view or the container view on any axis. This method is called when the SnapHelper has intercepted a fling and it needs to know the exact distance required to scroll by in order to snap to the target view.


2 Answers

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);
            }
        }
    });
like image 161
Thought Avatar answered Oct 10 '22 10:10

Thought


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

like image 36
Juan Sebastian Gomez Torres Avatar answered Oct 10 '22 11:10

Juan Sebastian Gomez Torres