使用RecyclerView时如何避免项目之间不需要的空间

时间:2016-04-22 08:14:58

标签: android android-recyclerview

在尝试实施RecyclerView时,我在项目之间获得了大量的空白......

RecyclerView布局: -

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

我的RecyclerView项目布局 -

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

我的RecyclerView设置 -

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);
    }

我的RecyclerView适配器

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

2 个答案:

答案 0 :(得分:0)

您必须将RecyclerView项的android:layout_height属性更改为wrap_content,而不是match_parent

答案 1 :(得分:0)

因为您在我的RecyclerView项目布局中设置了根布局 设置为 android:layout_height 为match_parent

替换此代码并享受他!

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