Recyclerview无法使用snaphelper滚动到正确的位置

时间:2018-08-24 14:08:04

标签: java android android-recyclerview

我的recyclerview出现问题,我想使其自动滚动到某个位置,即使滚动时也不会将其放置到正确的位置。以下gif中的示例。

Example

这是我的代码的一部分:

final RecyclerView anioList = root.findViewById(R.id.dia_picker);
    final RecyclerView monthList = root.findViewById(R.id.mes_picker);

final ArrayList<DateBean> arrayList = new ArrayList<>();
final ArrayList<DateBean> arrayList2 = new ArrayList<>();

final PickerAdapter adapter = new PickerAdapter(getActivity(), arrayList);
final PickerAdapter adapter2 = new PickerAdapter(getActivity(), arrayList2);

 final LinearLayoutManager manager = new LinearLayoutManager(
                getActivity(),
                LinearLayoutManager.HORIZONTAL,
                false);

        final LinearLayoutManager manager2 = new LinearLayoutManager(
                getActivity(),
                LinearLayoutManager.HORIZONTAL,
                false);

final LinearSnapHelper snapHelper = new LinearSnapHelper();

final LinearSnapHelper snapHelper2 = new LinearSnapHelper();

snapHelper.attachToRecyclerView(anioList);
snapHelper2.attachToRecyclerView(monthList);

anioList.setLayoutManager(manager);
monthList.setLayoutManager(manager2);


anioList.setOnFlingListener(snapHelper);
monthList.setOnFlingListener(snapHelper2);

anioList.setAdapter(adapter);
monthList.setAdapter(adapter2);


monthList.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                        centerView = snapHelper2.findSnapView(manager2);
                        // Getting position of the center/snapped item.
                        int pos = manager2.getPosition(centerView);
                        adapter2.changeSize(pos);
                        mes = arrayList2.get(pos).getDate();
                        if (pos==2)
                            diaList.scrollToPosition((arrayList3.size()) - 3);

                    }

                }
            });

0 个答案:

没有答案