2

While trying to implement RecyclerView i'm getting large chunk of white space between items...

RecyclerView layout:-

?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/crime_reycler_view"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"/>

My RecyclerView item layout-

<?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">

 <CheckBox
      android:id="@+id/list_item_crime_solved_check_box"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"/>

 <TextView
      android:id="@+id/list_item_crime_title_text_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/list_item_crime_solved_check_box"
      android:textStyle="bold"
      tools:text="Crime Title"/>

 <TextView
     android:id="@+id/list_item_date_text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box"
     android:layout_below="@id/list_item_crime_title_text_view"
     tools:text="Crime Date"/>

</RelativeLayout>

My RecyclerView setup-

View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

    mCrimeRecyclerView = (RecyclerView) view
            .findViewById(R.id.crime_reycler_view);

    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    if (savedInstanceState != null)
    {
        mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
    }

My RecyclerView adapter

private class CrimeAdapter extends  RecyclerView.Adapter<CrimeHolder>
{
    private List<Crime> mCrimes;

    public CrimeAdapter (List<Crime> crime)
    {
        mCrimes = crime;
    }

    //This method is called to create a view
    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater
                .inflate(R.layout.list_item_crime, parent, false);

        return new CrimeHolder(view);
    }

    //binds them together
    @Override
    public void onBindViewHolder(CrimeHolder holder, int position)
    {
        Crime crime = mCrimes.get(position);
        holder.bindCrime(crime);
    }

    private void setCrimes (List<Crime> crimes)
    {
        mCrimes = crimes;
    }

    //RecycleView Calls this method to get the size of mCrimes
    @Override
    public int getItemCount() {
        return mCrimes.size();
    }
}

This is how it looks on moblile after adding items

Kamalnrf
  • 342
  • 4
  • 15

2 Answers2

0

You have to change your RecyclerView item's android:layout_height property to wrap_content instead of match_parent.

Dariush Salami
  • 145
  • 1
  • 6
0

because you set in My RecyclerView item layout is a root layout set is android:layout_height is match_parent

replace this code and enjoy him !

<?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="100dp">

 <CheckBox
      android:id="@+id/list_item_crime_solved_check_box"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"/>

 <TextView
      android:id="@+id/list_item_crime_title_text_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/list_item_crime_solved_check_box"
      android:textStyle="bold"
      tools:text="Crime Title"/>

 <TextView
     android:id="@+id/list_item_date_text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box"
     android:layout_below="@id/list_item_crime_title_text_view"
     tools:text="Crime Date"/>

</RelativeLayout>
hamid
  • 171
  • 1
  • 1
  • 9