RecycleView布局管理器列表项占据整个屏幕

时间:2016-05-16 12:17:49

标签: android xml android-layout android-recyclerview avd

所以我刚刚制作了一个包含RecycleView的简单片段。循环视图具有自定义的相对布局,包含两个TextView和一个CheckBox。问题是单个列表项占用整个AVD seceen的空间,而我为列表项设置的监听器不起作用。

here is a screenshot of the AVD screen (I am using android studio)

And when scrolled

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">
<CheckBox
    android:id="@+id/list_item_crime_solved_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="4dp"
    />
    <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"
        android:padding="4dp"
        tools:text="Crime Title"/>
    <TextView
        android:id="@+id/list_item_crime_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"
        android:padding="4dp"
        tools:text="Crime date"
        />
</RelativeLayout>

片段java代码:

package com.bignerdranch.android.criminalintent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class CrimeListFragment extends Fragment
{
    private RecyclerView mCrimeRecyclerView;
    private CrimeAdabter mCrimeAdabter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_crime_list, container, false);
        mCrimeRecyclerView = (RecyclerView) v.findViewById(R.id.crime_recycler_view);
        mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return v;
    }

    private void updateUI()
    {
        CrimeLab crimeLab = CrimeLab.get(getActivity());
        List<Crime> crimes = crimeLab.getCrimes();
        mCrimeAdabter = new CrimeAdabter(crimes);
        mCrimeRecyclerView.setAdapter(mCrimeAdabter);
    }


}

ViewHolder java代码:

private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    private TextView mTitleTextView;
    private TextView mDateTextView;
    private CheckBox mCrimeSolvedCheckBox;
    private Crime mCrime;

    public CrimeHolder(View itemView)
    {
        super(itemView);
        mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
        mDateTextView = (TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
        mCrimeSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
    }

    public void bindCrime(Crime crime)
    {
        mCrime = crime;
        mTitleTextView.setText(mCrime.getTitle());
        mDateTextView.setText(mCrime.getDate().toString());
        mCrimeSolvedCheckBox.setChecked(mCrime.isSolved());
    }

    @Override
    public void onClick(View v)
    {
        Intent i=new Intent(getActivity(),CrimeActivity.class);
        startActivity(i);
    }
}

适配器java代码:

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

    public CrimeAdabter(List<Crime> crimes)
    {
        mCrimes = crimes;
    }

    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.list_item_crime, parent, false);

        return new CrimeHolder(v);
    }

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

    }


    @Override
    public int getItemCount()
    {
        return mCrimes.size();
    }
}

1 个答案:

答案 0 :(得分:9)

这是因为您的根RelativeLayout的高度设置为 match_parent 。使其成为wrap_content或特定高度。