5
我有一個附加到我的MainActivity的自定義片段。片段的佈局文件包含recyclerview小部件。RecyclerView.Adapter的onCreateViewHolder方法中的parent.getContext
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>
在我的RecyclerView.Adapter的onCreateViewHolder方法是這樣的:
@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);
}
我的問題是關於此方法的ViewGroup中父。此ViewGroup是我的RecyclerView小部件,但爲什麼給我parent.getContext對我的MainActivity的引用,而不是我的片段?
謝謝你的超級解釋!我比以前更聰明瞭。 –