3

我正在构建一个在一个片段中包含三个 RecyclerView 的应用程序,以显示项目的水平列表。我创建了一个 LinearLayoutManager 对象并将其设置为所有三个 RecyclerView 。但它使应用程序崩溃,说一个 LinearLayoutManager 只能附加到一个 RecyclerView 。为什么我不能附加到所有虽然我需要相同的属性。代码是..

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recViewTopSell.setLayoutManager(llm);
        recViewBrands.setLayoutManager(llm);
        recViewCategory.setLayoutManager(llm);

错误在

 recViewBrands.setLayoutManager(llm);
            recViewCategory.setLayoutManager(llm);
4

2 回答 2

0

No it can't be reused like that. The LayoutManager, LinearLayoutManager in your case, contains state specific to RecyclerView it is used with.

If there is a lot of setup involved for the three different LayoutMangers, consider a createLayoutManager() method to call three times instead.

于 2016-04-21T10:29:22.197 回答
-1

按照马蒂亚斯的回答这样做:

    recViewTopSell.setLayoutManager(newLLM());
    recViewBrands.setLayoutManager(newLLM());
    recViewCategory.setLayoutManager(newLLM());

接着:

    private LinearLayoutManager newLLM() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        return linearLayoutManager;
    }
于 2016-04-21T10:45:38.480 回答