gpt4 book ai didi

android - RecycleView 仅显示第一项

转载 作者:IT老高 更新时间:2023-10-28 21:38:44 27 4
gpt4 key购买 nike

问题来了:我创建了世界上最简单的 RecyclerView,但它只显示第一项。我不明白为什么。感谢您的帮助。

item_layout.xml

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

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

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

最佳答案

您需要将 item_layout 高度更改为 wrap_content如果您使用 Android Support Library v 23.2.0及以上

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

关于android - RecycleView 仅显示第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com