水平RecyclerView

时间:2017-07-24 12:12:56

标签: android android-recyclerview recycler-adapter

enter image description here

我想要一个像这样的水平回收者视图。我尝试在布局管理器中更改OnScrolledBy的实现并添加onScrollListener,但没有任何问题。 这是我最接近的:

enter image description here

在此,我使用以下代码更改了layoutManager的实现:

  @Override public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
      RecyclerView.State state)
  {
    int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
    float midpoint = getWidth() / 2.f;
    float d0 = 0.f;
    float d1 = mShrinkDistance * midpoint;
    float s0 = 1.f;
    float s1 = 1.f - mShrinkAmount;
    for (int i = 0; i < getChildCount(); i++)
    {
      Log.d(TAG, "scrollHorizontallyBy: Child Count"+getChildCount());
      View child = getChildAt(i);
      Log.d(TAG, "scrollHorizontallyBy: Child "+i+" text::"+((TextView)getChildAt(i).findViewById(R.id.txtPeriodDate)).getText().toString());
      float childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
      Log.d(TAG, "scrollHorizontallyBy: Child MidPoint"+childMidpoint);
      float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
      Log.d(TAG, "scrollHorizontallyBy: Value of d"+d);
      float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
      Log.d(TAG, "scrollHorizontallyBy: Scale"+scale);
      if(Math.abs(1.f-scale)<=0.05)
      {
        View frontView = child.findViewById(R.id.llPeriodFrontLayout);
        frontView.setVisibility(View.GONE);
        frontView.refreshDrawableState();
        View backView = child.findViewById(R.id.llPeriodBackLayout);
        backView.setVisibility(View.VISIBLE);
        backView.refreshDrawableState();
/*        RecyclerView.LayoutParams layoutParams =
            (RecyclerView.LayoutParams) child.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, 0);
        currentRecyclerView.getLayoutManager().getChildAt(i).setLayoutParams(layoutParams);*/
      }else
      {
        View backView = currentRecyclerView.getLayoutManager().getChildAt(i).findViewById(R.id.llPeriodBackLayout);
        backView.setVisibility(View.GONE);
        backView.refreshDrawableState();
        View frontView = currentRecyclerView.getLayoutManager().getChildAt(i).findViewById(R.id.llPeriodFrontLayout);
        frontView.setVisibility(View.VISIBLE);
        frontView.refreshDrawableState();
        /*
        nonCentralItemHeight = currentRecyclerView.getLayoutManager().getChildAt(i).getHeight();
        nonCentralMargin =
            Math.round(centralItemHeight / 2.f) - Math.round(nonCentralItemHeight / 2.f);
        RecyclerView.LayoutParams layoutParams =
            (RecyclerView.LayoutParams) currentRecyclerView.getLayoutManager().getChildAt(i).getLayoutParams();
        layoutParams.setMargins(0, nonCentralMargin, 0, 0);
        currentRecyclerView.getLayoutManager().getChildAt(i).setLayoutParams(layoutParams);
        */
      }
/*      child.setScaleX(scale);
      child.setScaleY(scale);*/
    }

    return scrolled;
  }

这实际上是一个用于缩放回收器视图中的中心项目的代码,我已经调整过了。这是我的正常项目代码(黄色):

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/flPeriod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/dimen_margin_5"
    android:layout_marginRight="@dimen/dimen_margin_5"
    android:orientation="vertical"
    tools:ignore="MissingPrefix"
    >
    <LinearLayout
        android:id="@+id/llPeriodBackLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/selected_day_background"
        android:orientation="vertical"
        />
    <LinearLayout
        android:id="@+id/llPeriodFrontLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/filled_day"
        android:orientation="vertical"
        />
    <LinearLayout
        android:id="@+id/llPeriodTextLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        >
      <TextView
          android:id="@+id/txtPeriodDay"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:maxLength="3"
          android:text="@string/app_name"
          android:textAllCaps="true"
          android:textColor="@android:color/white"
          android:textSize="@dimen/text_size_19"
          fontPath="@string/font_semibold"
          />
      <TextView
          android:id="@+id/txtPeriodDate"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/app_name"
          android:textColor="@android:color/white"
          android:textSize="@dimen/text_size_19"
          fontPath="@string/font_semibold"
          />
    </LinearLayout>
</FrameLayout>

有人可以推荐图书馆或帮我解决这个问题吗?

0 个答案:

没有答案