我正在构建一个在一个片段中有三个RecyclerView的应用程序,以显示项目的水平列表。我创建了一个LinearLayoutManager对象,并将其设置为所有三个RecyclerView。但它崩溃了应用程序,说一个LinearLayoutManager只能连接到一个RecyclerView .why无法连接到所有,尽管我需要相同的属性。代码是..
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
recViewTopSell.setLayoutManager(llm);
recViewBrands.setLayoutManager(llm);
recViewCategory.setLayoutManager(llm);
错误位于
recViewBrands.setLayoutManager(llm);
recViewCategory.setLayoutManager(llm);
发布于 2016-04-21 18:29:22
不,它不能像那样重复使用。LayoutManager
,在本例中为LinearLayoutManager
,包含与其一起使用的特定于RecyclerView的状态。
如果这三个不同的LayoutMangers涉及很多设置,请考虑使用createLayoutManager()
方法调用三次。
发布于 2016-04-21 18:45:38
按照Mattias的回答这样做:
recViewTopSell.setLayoutManager(newLLM());
recViewBrands.setLayoutManager(newLLM());
recViewCategory.setLayoutManager(newLLM());
然后:
private LinearLayoutManager newLLM() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
return linearLayoutManager;
}
https://stackoverflow.com/questions/36766682
复制相似问题