Skip to main content
edited tags
Source Link
Reaz Masud
  • 24.3k
  • 15
  • 83
  • 102

I spent a moment trying to figure out a way to add a header to a RecyclerView, unsuccessfully. This

This is what I got so far:

@Override protected 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); ... } 

The LayoutManager seems to be the object handling the disposition of the RecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's addView(View view, int position) method and to add my header view in the first position to act likeas a header.

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)   at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)   at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)   at android.os.Handler.handleCallback(Handler.java:739)   at android.os.Handler.dispatchMessage(Handler.java:95)   at android.os.Looper.loop(Looper.java:135)   at android.app.ActivityThread.main(ActivityThread.java:5221)   at java.lang.reflect.Method.invoke(Native Method)   at java.lang.reflect.Method.invoke(Method.java:372)   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

I spent a moment trying to figure out a way to add a header to a RecyclerView, unsuccessfully. This is what I got so far:

@Override protected 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); ... } 

The LayoutManager seems to be the object handling the disposition of the RecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's addView(View view, int position) method and to add my header view in first position to act like a header.

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)   at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)   at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)   at android.os.Handler.handleCallback(Handler.java:739)   at android.os.Handler.dispatchMessage(Handler.java:95)   at android.os.Looper.loop(Looper.java:135)   at android.app.ActivityThread.main(ActivityThread.java:5221)   at java.lang.reflect.Method.invoke(Native Method)   at java.lang.reflect.Method.invoke(Method.java:372)   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

I spent a moment trying to figure out a way to add a header to a RecyclerView, unsuccessfully.

This is what I got so far:

@Override protected 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); ... } 

The LayoutManager seems to be the object handling the disposition of the RecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's addView(View view, int position) method and to add my header view in the first position to act as a header.

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) at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196) at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Question Protected by AskNilesh
deleted 115 characters in body
Source Link
Alexander Farber
  • 23.4k
  • 79
  • 263
  • 449

Could somebody help me? Or at least give me some idea / new direction to explore?

Thanks in advance!

VieuMa

PS: Also, a solution that could handle the GridLayoutManager in addition to the LinearLayoutManager would be really appreciated!

Could somebody help me? Or at least give me some idea / new direction to explore?

Thanks in advance!

VieuMa

PS: Also, a solution that could handle the GridLayoutManager in addition to the LinearLayoutManager would be really appreciated!

PS: Also, a solution that could handle the GridLayoutManager in addition to the LinearLayoutManager would be really appreciated!

improved formatting
Source Link
Vasily Kabunov
  • 6.8k
  • 13
  • 55
  • 56

I spent a moment trying to figure out a way to add a header to a RecyclerViewRecyclerView, unsuccessfully. This is what I got so far  :

The LayoutManagerLayoutManager seems to be the object handling the disposition of the RecyclerViewRecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's LayoutManager's addView(View view, int position) method and to add my header view in first position to act like a header.

PS: Also, a solution that could handle the GridLayoutManagerGridLayoutManager in addition to the LinearLayoutManagerLinearLayoutManager would be really appreciated!

I spent a moment trying to figure out a way to add a header to a RecyclerView, unsuccessfully. This is what I got so far  :

The LayoutManager seems to be the object handling the disposition of the RecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's addView(View view, int position) method and to add my header view in first position to act like a header.

PS: Also, a solution that could handle the GridLayoutManager in addition to the LinearLayoutManager would be really appreciated!

I spent a moment trying to figure out a way to add a header to a RecyclerView, unsuccessfully. This is what I got so far:

The LayoutManager seems to be the object handling the disposition of the RecyclerView items. As I couldn't find any addHeaderView(View view) method, I decided to go with the LayoutManager's addView(View view, int position) method and to add my header view in first position to act like a header.

PS: Also, a solution that could handle the GridLayoutManager in addition to the LinearLayoutManager would be really appreciated!

edited tags
Link
MathieuMaree
  • 7.5k
  • 6
  • 28
  • 31
Loading
Source Link
MathieuMaree
  • 7.5k
  • 6
  • 28
  • 31
Loading