android – “LayoutManager已经附加到RecyclerView”错误

前端之家收集整理的这篇文章主要介绍了android – “LayoutManager已经附加到RecyclerView”错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在布局中有多个RecyclerViews,但是我收到以下错误:“LayoutManager已经附加到RecyclerView”

Java代码是:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_squad,container,false);

    Activity parentActivity = getActivity();
    final ObservableScrollView scrollView = (ObservableScrollView) view.findViewById(R.id.squad_scrollview);

    final RecyclerView gkRecyclerView = (RecyclerView) view.findViewById(R.id.gk_recycler);
    final RecyclerView coachRecyclerView = (RecyclerView) view.findViewById(R.id.coach_recycler);

    coachRecyclerView.setAdapter(new SquadRecyclerAdapter(parentActivity,getSquadDummyData(0)));
    coachRecyclerView.setLayoutManager(new MyLinearLayoutManager(parentActivity,LinearLayoutManager.VERTICAL,false));
    coachRecyclerView.setHasFixedSize(false);

    gkRecyclerView.setAdapter(new SquadRecyclerAdapter(parentActivity,getSquadDummyData(1)));
    gkRecyclerView.setLayoutManager(new MyLinearLayoutManager(parentActivity,false));
    gkRecyclerView.setHasFixedSize(false);

    scrollView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.container));

    if (parentActivity instanceof ObservableScrollViewCallbacks) {
        scrollView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
    }

    return view;
}

XML布局代码是:

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/squad_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin_medium"
    >

    <LinearLayout
        android:id="@+id/squad_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/seasons_scrollview"
        android:divider="@drawable/nav_bar_divider"
        android:elevation="@dimen/card_elevation"
        android:orientation="vertical"
        android:showDividers="middle">

        <LinearLayout
            android:id="@+id/coach_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/margin_small"
            android:paddingLeft="@dimen/margin_standard"
            android:paddingRight="@dimen/margin_standard"
            android:paddingTop="@dimen/margin_small">

            <TextView
                android:id="@+id/squad_coach_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Coach"
                android:textSize="@dimen/text_size_standard" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/coach_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/margin_small"
                android:paddingBottom="@dimen/margin_small"
                android:scrollbars="none">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/gk_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/margin_small"
            android:paddingLeft="@dimen/margin_standard"
            android:paddingRight="@dimen/margin_standard"
            android:paddingTop="@dimen/margin_small">

            <TextView
                android:id="@+id/squad_gk_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Goalkeepers"
                android:textSize="@dimen/text_size_standard" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/gk_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/margin_small"
                android:paddingBottom="@dimen/margin_small"
                android:scrollbars="none">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/def_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/margin_small"
            android:paddingLeft="@dimen/margin_standard"
            android:paddingRight="@dimen/margin_standard"
            android:paddingTop="@dimen/margin_small">

            <TextView
                android:id="@+id/squad_def_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Defense"
                android:textSize="@dimen/text_size_standard" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/mid_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/margin_small"
            android:paddingLeft="@dimen/margin_standard"
            android:paddingRight="@dimen/margin_standard"
            android:paddingTop="@dimen/margin_small">

            <TextView
                android:id="@+id/squad_mid_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Midfielders"
                android:textSize="@dimen/text_size_standard" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/for_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/margin_small"
            android:paddingLeft="@dimen/margin_standard"
            android:paddingRight="@dimen/margin_standard"
            android:paddingTop="@dimen/margin_small">

            <TextView
                android:id="@+id/squad_for_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Forwards"
                android:textSize="@dimen/text_size_standard" />

        </LinearLayout>


    </LinearLayout>


</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>

MyLinearLayoutManager是我在线查找的自定义LinearLayoutManager,以解决SDK LinearLayoutManager的封装问题.

有什么办法可以在单个布局中有多个RecyclerViews?看来我每个布局不能附加多个LayoutManagers.

任何帮助将是非常欢迎:)

解决方法

我也有这个问题.我的活动使用Tabs,有三个片段,当我去第三个选项卡,并返回到第一个(或第二),这个错误被抛出.

搜索很多,我发现可能是垃圾收集器,因为我使用了强大的参考.

由于构造函数LinearLayoutManager使用活动作为参数(而不是片段),所以Tab键活动在选项卡更改期间保持活动状态.

从类中删除mLinearLayoutManager中的本地字段,并使用弱引用,我可以摆脱这个问题:

之前:

public class MyFragment1 extends Fragment
    private LinearLayoutManager linearLayoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        (...)
    }

    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
        (...)
        mRecyclerView.setLayoutManager(linearLayoutManager);
    }
}

我改为:

public class MyFragment1 extends Fragment {
    // private LinearLayoutManager linearLayoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // linearLayoutManager = new LinearLayoutManager(getActivity());
        (...)
    }

    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
        (...)
        mRecyclerView.setLayoutManager(
           new LinearLayoutManager(getActivity()));
    }
}
原文链接:https://www.f2er.com/android/311090.html

猜你在找的Android相关文章