平滑无休止的回收视图滚动

时间:2018-10-18 13:58:45

标签: android android-recyclerview android-adapter

我想实现recyclerview,当我丰富数据时,它将在末尾添加一些数据。我添加了onScrollListener来查看视图,如果无法向右滚动,则要在右边添加新视图。但是,当我添加它时,我的滚动停止了,这意味着我需要再次滚动才能看到新视图。可以立即显示新视图的热门图片吗?

MainScreenActivity.class

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

private DataView leftDataView;
private TextView displayedDate;

private Calendar calendar;

List<ChartDefiner> list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    calendar = Calendar.getInstance();

    displayedDate = findViewById(R.id.displayed_date);
    leftDataView = findViewById(R.id.leftDataView);

    scaleDetector = new ScaleGestureDetector(this, new ScaleListener());

    mRecyclerView = findViewById(R.id.recycler_view);

    mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            scaleDetector.onTouchEvent(e);
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (!recyclerView.canScrollHorizontally(-1)) {
                Log.d(Consts.TAG, "Cant scroll left");

            }

            if (!recyclerView.canScrollHorizontally(1)) {
                Log.d(Consts.TAG, "Cant scroll right");
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                list.add(getDataForCurrentDate(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR)));
                presenter.setDate(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
                mAdapter.notifyDataSetChanged();
            }
        }
    });

    mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);

    addForecastButton = findViewById(R.id.add_forecast_button_main);
    addMedicationButton = findViewById(R.id.add_medication_button_main);

    addForecastButton.setOnClickListener(this);
    addMedicationButton.setOnClickListener(this);

    presenter.setDate(1, 1, 2018);
    calendar.set(presenter.getYear(), presenter.getMonth(), presenter.getDay());

    if (list.size() == 0) {
        list.add(getDataForCurrentDate(presenter.getDay(), presenter.getMonth(), presenter.getYear()));
    }

    mAdapter = new MyAdapter(list, leftDataView, displayedDate);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
}

0 个答案:

没有答案