I am using a custom recycler adapter to display a text_view.xml
into a fragment_list.xml
. However, when I customise my fragment_list.xml
adding a RelativeLayout
the text_view
is not displayed anymore.
I am getting this error message: E/RecyclerView: No adapter attached; skipping layout
This is 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>
My 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>
This is my 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;
}
}
}
And this is my OnCreateView()
of my 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;
}
I really don't know where to put my hands anymore. Appreciate help guys.
Thank you in advance