コード例 #1
0
  private void updateState(int scrollState) {
    String stateName = "Undefined";
    switch (scrollState) {
      case RecyclerView.SCROLL_STATE_IDLE:
        stateName = "Idle";
        break;

      case RecyclerView.SCROLL_STATE_DRAGGING:
        stateName = "Dragging";
        break;

      case RecyclerView.SCROLL_STATE_SETTLING:
        stateName = "Flinging";
        break;
    }
    mPositionText.setText("currentPosition:" + mRecyclerView.getCurrentPosition());
    mStateText.setText(stateName);
  }
コード例 #2
0
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final Activity activity = getActivity();

    mToast = Toast.makeText(activity, "", Toast.LENGTH_SHORT);
    mToast.setGravity(Gravity.CENTER, 0, 0);

    mRecyclerView = (RecyclerViewPager) view.findViewById(R.id.list);

    LinearLayoutManager layout =
        new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(layout);
    mRecyclerView.setAdapter(new LayoutAdapter(activity, mRecyclerView));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLongClickable(true);

    mPositionText = (TextView) view.getRootView().findViewById(R.id.position);
    mCountText = (TextView) view.getRootView().findViewById(R.id.count);

    mStateText = (TextView) view.getRootView().findViewById(R.id.state);
    updateState(RecyclerView.SCROLL_STATE_IDLE);

    mRecyclerView.setOnScrollListener(
        new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
            updateState(scrollState);
          }

          @Override
          public void onScrolled(RecyclerView recyclerView, int i, int i2) {
            //                mPositionText.setText("First: " +
            // mRecyclerView.getFirstVisiblePosition());
            int childCount = mRecyclerView.getChildCount();
            int width = mRecyclerView.getChildAt(0).getWidth();
            int padding = (mRecyclerView.getWidth() - width) / 2;
            mCountText.setText("Count: " + childCount);

            for (int j = 0; j < childCount; j++) {
              View v = recyclerView.getChildAt(j);
              // 往左 从 padding 到 -(v.getWidth()-padding) 的过程中,由大到小
              float rate = 0;
              ;
              if (v.getLeft() <= padding) {
                if (v.getLeft() >= padding - v.getWidth()) {
                  rate = (padding - v.getLeft()) * 1f / v.getWidth();
                } else {
                  rate = 1;
                }
                v.setScaleY(1 - rate * 0.1f);
              } else {
                // 往右 从 padding 到 recyclerView.getWidth()-padding 的过程中,由大到小
                if (v.getLeft() <= recyclerView.getWidth() - padding) {
                  rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
                }
                v.setScaleY(0.9f + rate * 0.1f);
              }
            }
          }
        });

    mRecyclerView.addOnLayoutChangeListener(
        new View.OnLayoutChangeListener() {
          @Override
          public void onLayoutChange(
              View v,
              int left,
              int top,
              int right,
              int bottom,
              int oldLeft,
              int oldTop,
              int oldRight,
              int oldBottom) {
            if (mRecyclerView.getChildCount() < 3) {
              if (mRecyclerView.getChildAt(1) != null) {
                View v1 = mRecyclerView.getChildAt(1);
                v1.setScaleY(0.9f);
              }
            } else {
              if (mRecyclerView.getChildAt(0) != null) {
                View v0 = mRecyclerView.getChildAt(0);
                v0.setScaleY(0.9f);
              }
              if (mRecyclerView.getChildAt(2) != null) {
                View v2 = mRecyclerView.getChildAt(2);
                v2.setScaleY(0.9f);
              }
            }
          }
        });
  }