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

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

本文介绍了RecycleView布局管理器列表项占据整个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我刚刚制作了一个包含RecycleView的简单片段.回收视图具有定制的相对布局,其中包含两个TextViews和一个CheckBox.问题在于单个列表项占用了整个AVD服务对象的空间,而我为该列表项设置的侦听器无法正常工作.

So i just made this simple fragment that contains a RecycleView. The recycle view has a custom made relative layout with two TextViews and a CheckBox. The problem is that a single list item takes up the whole AVD seceen's space and the listener I set for the list item is not working.

这是AVD屏幕的屏幕截图(我正在使用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>

Fragment Java代码:

The Fragment java code:

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代码:

The ViewHolder java code:

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代码:

And the Adapter java code:

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

推荐答案

这是因为您的根RelativeLayout的高度设置为 match_parent .将其设置为wrap_content或特定高度.

It's because your root RelativeLayout's height is set to match_parent. Make it wrap_content or specific height.

这篇关于RecycleView布局管理器列表项占据整个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:35