将标头添加到RecyclerView(Android)的最简单方法

时间:2020-02-18 18:26:41

标签: android android-recyclerview header

我正在尝试将标题添加到有问题的回收站视图。我目前尝试执行此操作的方式,在堆栈溢出时发现,但遇到了麻烦。

装饰

public class HeaderDecoration extends RecyclerView.ItemDecoration {

    private View mLayout;

    public HeaderDecoration(final Context context, RecyclerView parent, @LayoutRes int resId) {
        // inflate and measure the layout
        mLayout = LayoutInflater.from(context).inflate(resId, parent, false);
        mLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        // layout basically just gets drawn on the reserved space on top of the first view
        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) == 0) {
                c.save();
                final int height = mLayout.getMeasuredHeight();
                final int top = view.getTop() - height;
                c.translate(0, top);
                mLayout.draw(c);
                c.restore();
                break;
            }
        }
    }

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

这是装饰类,它建议不带其他修改地使用此命令:

recyclerView.addItemDecoration(new HeaderDecoration(this,
                               recyclerView,  R.layout.test_header));

我对此的尝试实现如下: Fragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.galway_fragment, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.galway_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        MyAdapter adapter = new MyAdapter(placeNames);

        recyclerView.addItemDecoration(new HeaderDecoration(this.getContext(), recyclerView,  R.layout.stamp_header));

        recyclerView.setAdapter(adapter);
...
}

stamp_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:background="@android:color/holo_blue_bright"
        android:text="HI THERE" />


</RelativeLayout>

发生的错误是, stamp_header 中的标头消息在运行时显示在Fragment中,但仅在屏幕的左上角显示,并且不匹配父级(全角),就像它应该那样做

0 个答案:

没有答案
相关问题