首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >仅显示第一项的RecycleView

仅显示第一项的RecycleView
EN

Stack Overflow用户
提问于 2016-03-10 10:46:09
回答 7查看 25.7K关注 0票数 46

问题是:我创建了世界上最简单的RecyclerView,但它只显示第一项。我不明白原因何在。谢谢你的帮助。

item_layout.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_detail"/>
</RelativeLayout>

activity_main.xml

代码语言:javascript
复制
<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bcit.moonlady.testrecycler.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/tv_hello"/>

   <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/rv_details"
        android:layout_below="@+id/tv_hello"/>
</RelativeLayout>

MainActivity.java

代码语言:javascript
复制
package com.bcit.moonlady.testrecycler;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.TextView;

public class MainActivity extends AppCompatActivity {
    String[] data = {"test1", "test2", "test3"};

    RecyclerView mRecView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecView = (RecyclerView)findViewById(R.id.rv_details);
        mRecView.setHasFixedSize(true);
        mRecView.setLayoutManager(new    LinearLayoutManager(getApplicationContext()));
        mRecView.setAdapter(new DetailAdapter());
   }

   private class DetailView extends RecyclerView.ViewHolder {
        TextView mTextView;

        public DetailView(View itemView) {
            super(itemView);
            mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
        }

        public void bindView(String string) {
            mTextView.setText(string);
        }
   }

   private class DetailAdapter extends RecyclerView.Adapter<DetailView> {
       @Override
       public DetailView onCreateViewHolder(ViewGroup parent, int viewType)          {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
            return new DetailView(v);
       }

       @Override
       public void onBindViewHolder(DetailView holder, int position) {
           String string = data[position];
           holder.bindView(string);
       }

       @Override
       public int getItemCount() {
           return data.length;
       }
   } 
}
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-03-10 12:35:38

如果使用的是Android Support Library v 23.2.0及更高版本,则需要将item_layout高度更改为wrap_content

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_detail"/>
</RelativeLayout>
票数 144
EN

Stack Overflow用户

发布于 2017-01-31 03:07:17

我也犯了这个常见的错误。只需更改父高度- LinearLayout应为"wrap_content“

代码语言:javascript
复制
<LinearLayout
android:layout_width="match_parent"
android:layout_height = "wrap_content"
..... >
..
..
</LinearLayout>
票数 16
EN

Stack Overflow用户

发布于 2017-07-18 13:02:57

将item_layout.xml中RelativeLayout的高度设置为wrap_content。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_detail"/>
</RelativeLayout>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35906652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档