如何在使用回收器视图时避免项目之间不必要的空间



在尝试实现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"/>

我的回收器查看项目布局-

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

我的回收器查看设置-

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

我的回收器查看适配器

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

这是添加项目后在 moblile 上的样子

您必须将

RecyclerView项的android:layout_height属性更改为wrap_content而不是match_parent

因为您在"我的回收器视图"中设置的项目布局是根布局设置是安卓: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>

最新更新