我尝试创建一个水平的recyclerView,但是recyclerView找不到上下文,这是我的Fragmnet代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context =context;
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ArrayList<CategoryModel> categoryModels = null;
RecyclerView recyclerView = container.findViewById(R.id.category_recycler);
// recyclerView.setHasFixedSize(true);
CategoryAdapter adapter = new CategoryAdapter(context,CategoryData.getCategory());
recyclerView.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
return inflater.inflate(R.layout.main_fragment, container, false);
}
这是Recycler视图适配器代码:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
Context context;
ArrayList<CategoryModel> categorys;
public CategoryAdapter(Context context, ArrayList<CategoryModel> categoryModels) {
this.categorys = categoryModels;
this.context = context;
}
@NonNull
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.product_category,parent,false);
return new ViewHolder(view);
}
答案 0 :(得分:0)
要在Context
内使用Fragment
,请使用getContext()
方法。
CategoryAdapter adapter = new CategoryAdapter(getContext(),CategoryData.getCategory());
来自official文档
返回此片段当前与之关联的上下文。
如果您仅使用Context
来放大onCreateViewHolder
类中CategoryAdapter
方法内的视图,则无需在Constructor中传递Context
。您可以从Context
方法的第一个参数ViewGroup
获得onCreateViewHolder
。
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_category,parent,false);
答案 1 :(得分:0)
在您的onCreate()
片段回调中,我看到以下行:
public void onCreate(Bundle savedInstanceState) { ... this.context =context; ... }
此行不执行任何操作; context
方法没有onCreate()
参数或类似方法,因此您只是将变量赋值给它自己。由于您的Context context
字段没有显式的初始化,因此默认值为null
。所以这和写作一样
public void onCreate(Bundle savedInstanceState) {
...
this.context = null;
...
}
稍后,在onCreateView()
中,您将使用以下行实例化适配器:
CategoryAdapter adapter = new CategoryAdapter(context,CategoryData.getCategory());
由于我上面所说的,这在功能上与
相同CategoryAdapter adapter = new CategoryAdapter(null, CategoryData.getCategory());
要解决此问题,您需要一些位置来获取一个Context
实例。片段通常是“附加”到上下文的,当它们附加后,您可以通过调用getContext()
来获取该上下文。肯定会在onActivityCreated()
回调中将一个片段附加到上下文,因此您将需要将适配器创建移至该回调,而不是onViewCreated()
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CategoryAdapter adapter = new CategoryAdapter(getContext(), CategoryData.getCategory());
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
RecyclerView recyclerView = getView().findViewById(R.id.category_recycler);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
答案 2 :(得分:0)
尝试这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
return inflater.inflate(R.layout.main_fragment, parent, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Inflate the layout for this fragment
ArrayList<CategoryModel> categoryModels = null;
RecyclerView recyclerView = view.findViewById(R.id.category_recycler);
// recyclerView.setHasFixedSize(true);
CategoryAdapter adapter = new CategoryAdapter(getContext(), CategoryData.getCategory());
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}