private View findCenterView(LinearLayoutManager lm) {

    int minDistance = 0;
    View view = null;
    View returnView = null;
    boolean notFound = true;

    for (int i = lm.findFirstVisibleItemPosition();
        i <= lm.findLastVisibleItemPosition() && notFound;
        i++) {

      view = lm.findViewByPosition(i);

      int center =
          lm.getOrientation() == LinearLayoutManager.HORIZONTAL
              ? (view.getLeft() + view.getRight()) / 2
              : (view.getTop() + view.getBottom()) / 2;
      int leastDifference = Math.abs(mCenterPivot - center);

      if (leastDifference <= minDistance || i == lm.findFirstVisibleItemPosition()) {
        minDistance = leastDifference;
        returnView = view;
        setCenterView(returnView);
      } else {
        notFound = false;
      }
    }
    return returnView;
  }
  @Override
  public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final View view = getView();
    if (view == null) throw new AssertionError();
    final Context viewContext = view.getContext();
    mMultiSelectManager = getMultiSelectManager();
    final MessageEntriesAdapter adapter = getAdapter();
    final RecyclerView recyclerView = getRecyclerView();
    final LinearLayoutManager layoutManager = getLayoutManager();
    mNavigationHelper =
        new RecyclerViewNavigationHelper(recyclerView, layoutManager, adapter, this);

    adapter.setListener(this);

    final DividerItemDecoration itemDecoration =
        new DividerItemDecoration(viewContext, layoutManager.getOrientation());
    final Resources res = viewContext.getResources();
    final int decorPaddingLeft =
        res.getDimensionPixelSize(R.dimen.element_spacing_normal) * 3
            + res.getDimensionPixelSize(R.dimen.icon_size_status_profile_image);
    itemDecoration.setPadding(decorPaddingLeft, 0, 0, 0);
    itemDecoration.setDecorationEndOffset(1);
    recyclerView.addItemDecoration(itemDecoration);
    getLoaderManager().initLoader(0, null, this);
    showProgress();
  }
  @Override
  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

    LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();

    if (mCenterPivot == 0) {

      mCenterPivot =
          lm.getOrientation() == LinearLayoutManager.HORIZONTAL
              ? (recyclerView.getLeft() + recyclerView.getRight())
              : (recyclerView.getTop() + recyclerView.getBottom());
    }
    if (!mAutoSet) {

      if (newState == RecyclerView.SCROLL_STATE_IDLE) {

        View view = findCenterView(lm);

        int position = recyclerView.getChildPosition(view);

        Log.i("CenterLockListener", "list-position" + position);

        Toast.makeText(context, "list-position" + position, Toast.LENGTH_SHORT).show();

        int viewCenter =
            lm.getOrientation() == LinearLayoutManager.HORIZONTAL
                ? (view.getLeft() + view.getRight()) / 2
                : (view.getTop() + view.getBottom()) / 2;

        int scrollNeeded = viewCenter - mCenterPivot;

        if (lm.getOrientation() == LinearLayoutManager.HORIZONTAL) {

          recyclerView.smoothScrollBy(scrollNeeded, 0);
        } else {
          recyclerView.smoothScrollBy(0, (int) (scrollNeeded));
        }
        mAutoSet = true;
      }
    }
    if (newState == RecyclerView.SCROLL_STATE_DRAGGING
        || newState == RecyclerView.SCROLL_STATE_SETTLING) {

      mAutoSet = false;
    }
  }
 private int getOrientation(RecyclerView parent) {
   if (parent.getLayoutManager() instanceof LinearLayoutManager) {
     LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
     return layoutManager.getOrientation();
   } else {
     throw new IllegalStateException(
         "ColorDividerItemDecoration can only be used with a LinearLayoutManager.");
   }
 }