所以我只是做了這個簡單的片段,其中包含一個RecycleView。回收視圖具有用兩個TextView和一個CheckBox定製的相對佈局。問題是單個列表項佔用了整個AVD seceen的空間,我爲列表項設置的監聽器不工作。RecycleView佈局管理器列表項正在整個屏幕
here is a screenshot of the AVD screen (I am using android studio)
的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();
}
}
更改相對高度wrap_content – Pavya
更改您的relativeLayout高度android:layout_height =「match_parent」to wrap_content –