Android 5.0 - 将页眉/页脚添加到RecyclerView
我花了一些时间试图找出一种方法来添加一个标题RecyclerView,但没有成功。这是我到目前为止所得到的:
@Overrideprotected void onCreate(Bundle savedInstanceState){
...
layouManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layouManager);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
headerPlaceHolder = inflater.inflate(R.layout.view_header_holder_medium, null, false);
layouManager.addView(headerPlaceHolder, 0);
...}在LayoutManager似乎是处理的配置对象RecyclerView的项目。因为我无法找到任何addHeaderView(View view)方法,我决定去与LayoutManager的addView(View view, int position)方法,并增加我的头鉴于第一位置,像一个头。
Aaand这是事情变得更加丑陋的地方:
java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.RecyclerView$ViewHolder android.support.v7.widget.RecyclerView$LayoutParams.mViewHolder' on a null object reference at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497) at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807) at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803) at com.mathieumaree.showz.fragments.CategoryFragment.setRecyclerView(CategoryFragment.java:231) at com.mathieumaree.showz.fragments.CategoryFragment.access$200(CategoryFragment.java:47) at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:201)
在几次NullPointerExceptions尝试调用addView(View view)Activity创建的不同时刻(也尝试在设置完一切后添加视图,甚至是Adapter的数据)后,我意识到我不知道这是否是正确的方法(并且它看起来不是)。
PS:此外,一个可以处理GridLayoutManager除此之外的解决方案LinearLayoutManager将非常感谢!
相关分类