android - parent.getContext within RecyclerView.Adapter's onCreateViewHolder method -
i have custom fragment attached mainactivity. layout file of fragment contains recyclerview widget.
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </framelayout>
within recyclerview.adapter oncreateviewholder method looks this:
@override public myadapter.myholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout .list_item, parent, false); return new myholder(view); }
my question viewgroup parent method. viewgroup recyclerview widget why gives me parent.getcontext reference mainactivity , not fragment?
fragments not have context. when working in fragment , need context, need call getactivity()
. in specific case, context passed down activity fragment recyclerview - remember view takes context in it's constructor - , when call getcontext()
on recyclerview (the viewgroup) returns activity.
Comments
Post a Comment