SnapHelper项目位置

时间:2017-03-18 18:18:37

标签: java android android-recyclerview


我使用纵向RecyclerView列出我的项目,使用SnapHelper来捕捉中心项目。这个想法是随机选择,所以用户刷屏或摇动设备,它滚动到随机位置。
项目数为20,但我使用Integer.MAX_VALUE表示RecyclerView中的元素数量,并使用位置Integer.MAX_VALUE / 2初始化RecyclerView来创建某种无穷无尽的列表。
要滚动到设备震动的随机位置,我需要知道当前的快照项目位置。
有没有办法做到这一点?

这是我的片段代码:

公共类PlaceListFragment扩展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());
    }
}

4 个答案:

答案 0 :(得分:37)

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

答案 1 :(得分:0)

我尝试将此代码与PagerSnapHelper一起使用,以模仿寻呼机的行为,这很有用,但我发现了一些特殊情况可以解决,如果您从最后一页快速移至第一页并继续交换直到看到边界,则空闲状态不会发生,并且您会丢失索引。解决我将自己从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")
             }
         }

科特林语中的代码希望对您有所帮助

答案 2 :(得分:0)

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                ....
            }
        })

答案 3 :(得分:0)

private fun recyclerViewScrollListener() = object: RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            // LinearLayoutManager
            val pos = layoutManager.findFirstCompletelyVisibleItemPosition()
            Log.e(TAG, "onScrollStateChanged: $pos")
        }
    }
}