Tôi có một đoạn tùy chỉnh được gắn vào MainActivity của tôi. Tệp bố cục của đoạn chứa tiện ích recyclerview.parent.getContext trong phương thức onCreateViewHolder của RecyclerView.Adapter
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>
Trong RecyclerView.Adapter tôi phương pháp onCreateViewHolder trông như thế này:
@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);
}
Câu hỏi của tôi là về mẹ ViewGroup từ phương pháp này. ViewGroup này là tiện ích RecyclerView của tôi nhưng tại sao lại cho tôi parent.getContext một tham chiếu đến MainActivity của tôi chứ không phải cho đoạn của tôi?
Cảm ơn bạn đã giải thích siêu! Tôi đã thông minh hơn trước. –