使用RecyclerView适配器 - 错误E / RecyclerView:没有连接适配器;跳过布局

时间:2016-12-07 13:47:23

标签: android xml android-fragments android-recyclerview recycler-adapter

我正在使用自定义回收站适配器text_view.xml中显示fragment_list.xml。但是,当我自定义fragment_list.xml添加RelativeLayout时,text_view不再显示。 我收到此错误消息:E/RecyclerView: No adapter attached; skipping layout

这是my_text_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="normal|bold"
android:textColor="@color/black"
android:textAlignment="center"
android:text="This is my @string/cast_intro_overlay_button_text">
</TextView>

我的fragment_item_list.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color">

<Button
    android:text="Back"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/buttonBack"
    android:layout_weight="0.08"
    android:textSize="24sp"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_marginBottom="14dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="@color/black"
    android:textAlignment="center"
    android:id="@+id/myTextView"
    android:text="Best Scores"
    android:textStyle="normal|bold"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp">
</TextView>

<android.support.v7.widget.RecyclerView android:id="@+id/list"
    android:name="com.example.lucadigiammarino.biogame.ItemFragment"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.example.lucadigiammarino.biogame.ItemFragment"
    tools:listitem="@layout/my_text_view"
    android:isScrollContainer="false"
    android:addStatesFromChildren="true"
    android:layout_below="@+id/myTextView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="37dp">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

这是我的MyPlayerAdapter class

class MyPlayerAdapter extends RecyclerView.Adapter{
ArrayList<Player> players;

/**
 * Constructor for MyPlayerAdapter class
 * @param players
 */
public MyPlayerAdapter(ArrayList<Player> players) {
    this.players = players;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
    MyViewHolder vh = new MyViewHolder((TextView) v);
    return vh;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    MyViewHolder vh = (MyViewHolder)holder;
    vh.mTextView.setText(players.get(position).name+", "+players.get(position).score);
}

@Override
public int getItemCount() {
    return players.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView mTextView;
    public MyViewHolder(TextView v) {
        super(v);
        mTextView = v;
    }
}


}

这是我OnCreateView()

FragmentItem class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_list, container, false);

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));
    }

    return view;
}

我真的不知道该把握手的位置。感谢帮助人员。

提前谢谢

1 个答案:

答案 0 :(得分:1)

这是因为您的视图对象不是RecyclerView的实例,而是RelativeLayout的实例。因此,if语句中的代码永远不会被执行。用以下代码替换您的代码:

View view = inflater.inflate(R.layout.fragment_item_list, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
    Context context = view.getContext();
    if (mColumnCount <= 1) {
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    } else {
    recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
    }
    recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));

在这里,您可以从扩展到视图实例的布局中获取recyclerView实例。