Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecycleView displaying only the first item

Tags:

android

Here is the problem: I create the simplest RecyclerView in the world, but it displays only the first item. I cannot understand why. Thanks for any help.

item_layout.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_detail"/>
</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bcit.moonlady.testrecycler.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/tv_hello"/>

   <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/rv_details"
        android:layout_below="@+id/tv_hello"/>
</RelativeLayout>

MainActivity.java

package com.bcit.moonlady.testrecycler;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    String[] data = {"test1", "test2", "test3"};

    RecyclerView mRecView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecView = (RecyclerView)findViewById(R.id.rv_details);
        mRecView.setHasFixedSize(true);
        mRecView.setLayoutManager(new    LinearLayoutManager(getApplicationContext()));
        mRecView.setAdapter(new DetailAdapter());
   }

   private class DetailView extends RecyclerView.ViewHolder {
        TextView mTextView;

        public DetailView(View itemView) {
            super(itemView);
            mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
        }

        public void bindView(String string) {
            mTextView.setText(string);
        }
   }

   private class DetailAdapter extends RecyclerView.Adapter<DetailView> {
       @Override
       public DetailView onCreateViewHolder(ViewGroup parent, int viewType)          {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
            return new DetailView(v);
       }

       @Override
       public void onBindViewHolder(DetailView holder, int position) {
           String string = data[position];
           holder.bindView(string);
       }

       @Override
       public int getItemCount() {
           return data.length;
       }
   } 
}
like image 994
moonlady16 Avatar asked Mar 10 '16 02:03

moonlady16


2 Answers

You need to change your item_layout height as wrap_content if your are using Android Support Library v 23.2.0 and above

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_detail"/>
</RelativeLayout>
like image 58
ramji Avatar answered Nov 09 '22 19:11

ramji


I too made this common mistake. Just change your parent LinearLayout - height should be "wrap_content"

<LinearLayout
android:layout_width="match_parent"
android:layout_height = "wrap_content"
..... >
..
..
</LinearLayout>
like image 40
Priya Rajan Avatar answered Nov 09 '22 19:11

Priya Rajan