Android - 为什么 RecyclerView 没有显示在设备上?

Android - Why RecyclerView is not displaying on the device?

我刚刚创建了一个简单的 RecyclerView 项目,它将显示水果的名称。但是当我 运行 文件时,它没有显示任何内容。事实上,我的整个源代码都很好。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.fruits.myrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    String[] fruits = {"Apple", "Mango", "Pineapple", "Orange"};
    FruitsAdapter fruitsAdapter;
    RecyclerView recyclerView;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        fruitsAdapter = new FruitsAdapter( fruits );
        recyclerView = findViewById( R.id.recycler_view );
        recyclerView.setAdapter( fruitsAdapter );
    }
}

FruitsAdapter.java

package com.fruits.myrecyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class FruitsAdapter extends RecyclerView.Adapter<FruitsAdapter.FruitsView>{
    String[] fruits;

    public FruitsAdapter( String[] fruits){
        this.fruits = fruits;
    }

    public class FruitsView extends RecyclerView.ViewHolder{
        TextView textView;
        public FruitsView( View view ){
            super(view);
            textView = (TextView) view.findViewById( R.id.fruit_list );
        }

    }
    @Override
    public FruitsView onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater
                .from( parent.getContext() )
                .inflate( R.layout.fruites_list, parent, false );
        return new FruitsView( linearLayout );
    }

    @Override
    public void onBindViewHolder( @NonNull FruitsView holder, int position ) {
        holder.textView.setText( fruits[position] );
    }

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

fruites_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linear_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/fruit_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="30dp"
        android:gravity="center"
        android:text="Something"
        android:textColor="@android:color/holo_blue_dark"/>

</LinearLayout>

您缺少 LayoutManager,添加

recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));