[英]One LinearLayoutManager is only for one RecyclerView?
I am building an app having three RecyclerView in one fragment to show horizontal list of items.我正在构建一个在一个片段中包含三个 RecyclerView 的应用程序,以显示项目的水平列表。 i created a LinearLayoutManager object and set it to all three RecyclerView .我创建了一个 LinearLayoutManager 对象并将其设置为所有三个 RecyclerView 。 but it crashes app, saying one LinearLayoutManager can attached to only one RecyclerView .why can't i attach to all although i need the same Properties.但它崩溃了应用程序,说一个 LinearLayoutManager 只能附加到一个 RecyclerView 。为什么我不能附加到所有,尽管我需要相同的属性。 code is ..代码是..
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
recViewTopSell.setLayoutManager(llm);
recViewBrands.setLayoutManager(llm);
recViewCategory.setLayoutManager(llm);
error at错误在
recViewBrands.setLayoutManager(llm);
recViewCategory.setLayoutManager(llm);
No it can't be reused like that.不,它不能像那样重复使用。 The LayoutManager
, LinearLayoutManager
in your case, contains state specific to RecyclerView it is used with.在您的情况下, LayoutManager
, LinearLayoutManager
包含特定于它所使用的 RecyclerView 的状态。
If there is a lot of setup involved for the three different LayoutMangers, consider a createLayoutManager()
method to call three times instead.如果三个不同的 LayoutManger 涉及很多设置,请考虑使用createLayoutManager()
方法调用三次。
Following Mattias answer do this:按照马蒂亚斯的回答做这个:
recViewTopSell.setLayoutManager(newLLM());
recViewBrands.setLayoutManager(newLLM());
recViewCategory.setLayoutManager(newLLM());
and then:然后:
private LinearLayoutManager newLLM() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
return linearLayoutManager;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.