Source:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_message_list, container, false); emptyTextView = (TextView) view.findViewById(R.id.emptyList); if (filter != null && ((AppCompatActivity) getActivity()).getSupportActionBar() != null) ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp); else ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_24dp); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); linearLayoutManager = new LinearLayoutManager(getContext()); recyclerView.setLayoutManager(linearLayoutManager); if (adapter == null) { adapter = new RecyclerMessageAdapter(messageList, getActivity(), (filter != null)); } else { adapter.setFooterShowing(false); } recyclerView.addItemDecoration(new HeaderSpaceItemDecoration(getContext())); registerForContextMenu(recyclerView); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { mListener.recyclerViewScrolled(); final int near = 6; if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (!paused && !loadingMessages && firstMessageId > 0 && lastMessageId > 0) { int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition(); if (lastVisiblePosition > adapter.getItemCount(false) - near) { Log.i("scroll", "Starting request below"); loadMoreMessages(LoadPosition.BELOW); } if (linearLayoutManager.findFirstVisibleItemPosition() < near && !loadedToTop) { Log.i("scroll", "Starting request above"); loadMoreMessages(LoadPosition.ABOVE); } } } } }); recyclerView.setAdapter(adapter); return view; }

Target:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_message_list, container, false); emptyTextView = (TextView) view.findViewById(R.id.emptyList); if (filter != null && ((AppCompatActivity) getActivity()).getSupportActionBar() != null) ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp); else ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_24dp); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); linearLayoutManager = new LinearLayoutManager(getContext()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.addItemDecoration(new HeaderSpaceItemDecoration(getContext())); if (adapter == null) { adapter = new RecyclerMessageAdapter(messageList, getActivity(), (filter != null)); } else { adapter.setFooterShowing(false); } recyclerView.setAdapter(adapter); registerForContextMenu(recyclerView); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { mListener.recyclerViewScrolled(linearLayoutManager.findLastCompletelyVisibleItemPosition() == adapter.getItemCount() - 2); final int near = 6; if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (!paused && !loadingMessages && firstMessageId > 0 && lastMessageId > 0) { int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition(); if (lastVisiblePosition > adapter.getItemCount(false) - near) { Log.i("scroll", "Starting request below"); loadMoreMessages(LoadPosition.BELOW); } if (linearLayoutManager.findFirstVisibleItemPosition() < near && !loadedToTop) { Log.i("scroll", "Starting request above"); loadMoreMessages(LoadPosition.ABOVE); } } } } }); mListener.setLayoutBehaviour(linearLayoutManager,adapter); return view; }