Recycler view not showing the views when a background color is setted

Viewed 906

Hi I'm new to Android Studio, and I am trying to make a simple recycler view that contains a list of card view. The code runs just fine and every view is rendering fine when I run it, but then I decided to add background color to recycler view than everything just went wrong. The only thing I've changed is setting the background color of the recycler view. By the way, my recycler view lives in a fragment.

Here is what it looked like before I set the background color: enter image description here

The xml file for the recycler view before change:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/recycler_view_item" />

</android.support.constraint.ConstraintLayout>

And here is what it looks like now, all I did is setting the background color of the recycler view: enter image description here

Here is the new xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_dark"
        tools:listitem="@layout/recycler_view_item" />

</android.support.constraint.ConstraintLayout>

My guess is that somehow the background color is covering my views? By the way, I do notice an error occured in the logcat:

2018-12-21 22:02:05.515 20644-20644/com.steven97102gmail.todoassistant E/RecyclerView: No adapter attached; skipping layout

However, that error occurs even if I don't set the background, and it is working just fine.

Here is my class for frament:

public class RecycleFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter viewAdapter;
    private RecyclerView.LayoutManager viewLayouManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recyler_view, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // get and configure the recycle view
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        // layout manager for recyler view
        viewLayouManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(viewLayouManager);
        // creates the initial cards
        ArrayList<CardItem> card_lists = new ArrayList<>();
        for (int i = 0;i<=10;i++){
            CardItem card = new CardItem("TODO"+i,"12/" + i);
            card_lists.add(card);
        }
        // adapter for recycler view, initialize the recycler view
        viewAdapter = new RecyclerAdapter(card_lists);
        recyclerView.setAdapter(viewAdapter);
        // test add a new item
        card_lists.add(0,new CardItem("Test Insert","12/16"));
        viewAdapter.notifyItemInserted(0);
    }
}

Here is my class for recycler view's adapter:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
   private ArrayList<CardItem> card_item_list;

   // view holder is created from the recycler_card_item template
   public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
       public TextView title_tv, date_tv;
       public RecyclerViewHolder(LinearLayout layout) {
            super(layout);
            title_tv = layout.findViewById(R.id.card_item_title);
            date_tv = layout.findViewById(R.id.card_item_date);
       }
   }

   // take in a list of card items to initialize with
   public RecyclerAdapter(ArrayList<CardItem> card_list) {
        card_item_list = card_list;
   }

    @NonNull
    @Override
    // inflate and creates a viewholder objects, which is from the recycler care item template;
    public RecyclerAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       // create a new card item
        LinearLayout cardLayout = (LinearLayout) LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.recycler_card_item, viewGroup, false);
        RecyclerViewHolder vh = new RecyclerViewHolder(cardLayout);
        return vh;
    }

    @Override
    // bind the CardItem class with the viewholder to complete the card
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
       CardItem target_card = card_item_list.get(position);
       holder.title_tv.setText(target_card.title);
       holder.date_tv.setText(target_card.date);
    }

    @Override
    public int getItemCount() {
        return card_item_list.size();
    }
}
1 Answers

In your code, you're trying to retrieve a recyclerview at Activity level:

recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);

but the recyclerview is in your fragment. It is strange that you do not have any other errors, I would've expected the app to crash. Anyway, it should be solved in this way: change your onCreateView like this

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.recyler_view, container, false);
    recyclerView = root.findViewById(R.id.recycler_view);
    return root;
} 

the previous line, the one that retrieved the recyclerview from the activity, is not needed anymore.

Related