在画布上绘制视图时出现问题

时间:2017-02-01 12:57:24

标签: android canvas android-recyclerview

我正在使用itemdecorators创建一个recyclelerview的页脚。当我将textview作为footerView传递时,代码工作正常。但问题是我的观点的严重性被拙劣。还请注意,我要求查看视图,因为我想制作一个通用页脚。我该如何纠正这个问题。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;



public class FooterDecorator extends RecyclerView.ItemDecoration {
  private View mLayout;

  public FooterDecorator(final Context context, @LayoutRes int resId) {
    mLayout = LayoutInflater.from(context).inflate(resId, null, false);
    mLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  }

  public FooterDecorator(View view) {
    mLayout = view;
    mLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  }

  @Override
  public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    mLayout.layout(parent.getLeft(), 0, parent.getRight(), mLayout.getMeasuredHeight());
    for (int i = 0; i < parent.getChildCount(); i++) {
      View view = parent.getChildAt(i);
      if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) {
        c.save();
        c.translate(0, view.getBottom());
        mLayout.draw(c);
        c.restore();
        break;
      }
    }
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) {
      outRect.set(0, 0, 0, mLayout.getMeasuredHeight());
    }
  }
}

更新 我的布局资源文件 -

<TextView
    android:id="@+id/view_all_flights"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:gravity="center"
    android:paddingTop="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="Hello"
    android:textColor="@color/white"
    android:textStyle="bold"/>

请注意,我对该视图的点击监听器也不起作用。

0 个答案:

没有答案