捕捉助手项目位置

2022-09-03 03:20:31


我使用垂直列出我的项目并对齐中心项目。这个想法是随机选择,因此用户滑动屏幕或摇动设备,它滚动到随机位置。
项目的数量是20,但是我用于元素的数量,并使用位置初始化RecyclerView以创建某种无穷无尽的列表。
要滚动到设备晃动时的随机位置,我需要知道当前捕捉到的项目位置。
有什么办法可以做到这一点吗?RecyclerViewSnapHelperInteger.MAX_VALUERecyclerViewInteger.MAX_VALUE / 2

这是我的片段代码:

公共类 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) {

        }
    };

}

这是适配器:

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

答案 1

我在一个有 的项目上使用了这个,不确定它是否是你想要的。RecyclerViewSnapHelper

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

答案 2

我尝试将此代码与PagerSnapHelper一起使用来模仿寻呼机行为,这很有用,但是我发现了一些角落情况需要解决,如果您从最后一页快速移动到第一页并继续交换,直到看到边界,那么IDLE状态就不会发生,并且您丢失了索引。为了解决这个问题,我从IF中移出位置,并为这个角落情况添加一个额外的条件。

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

代码在 kotlin 希望它有帮助


推荐