简体   繁体   English

如何使用 RecyclerView 显示数组?

[英]How can I show an array using RecyclerView?

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private final int listSize;
    private String[] list;

    public MyAdapter(int listSize, String[] list) {
        this.listSize = listSize;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.rv_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return listSize;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView item;
        Button button;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.item_view);
            button = itemView.findViewById(R.id.button_item_view);
        }
        void bind(int index) {
            button.setText(String.valueOf(index));
            item.setText(list[index]);
        }
    }
}

In this adapter class I want to show a String array.在这个适配器类中,我想显示一个 String 数组。 The only problem is that the first element (with zero index) is missed on the screen, despite the fact that element exist beyond the screen (Sorry for such a big picture).唯一的问题是屏幕上遗漏了第一个元素(索引为零),尽管该元素存在于屏幕之外(抱歉有这么大的图片)。

在此处输入图片说明

The buttons must be numbered with zero, so should the array be printed starting with zero element.按钮必须用零编号,因此应该从零元素开始打印数组。 How can I resolve this problem?我该如何解决这个问题?

UPD: Here's the layout: UPD:这是布局:

rv_item rv_item

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:text="TextView"
        android:textSize="40sp" />

    <Button
        android:id="@+id/button_item_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|end"
        android:layout_marginEnd="48dp"
        android:padding="10dp"
        android:text="INFO" />
</FrameLayout>

activity_top, in which recyclerView widget is used activity_top,其中使用了recyclerView 小部件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".TopLevelActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listOfGoods"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/rv_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

I know that it could be the same, but I would like to corroborate, try to change this code:我知道它可能是相同的,但我想证实,尝试更改此代码:

@Override
public int getItemCount() {
    return listSize;
}

For this:为了这:

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

One more thing: try to debug this object this.list = list;还有一件事:尝试调试这个对象this.list = list; and check if the array contains zero index element.并检查数组是否包含零索引元素。 If the array contains that element, probably it should something about layout as mentioned @Rui Alves.如果数组包含该元素,则可能应该与@Rui Alves 提到的布局有关。

Do not give fixed height of recyclerView不要给出 recyclerView 的固定高度

Correct Way:正确方法:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listOfGoods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/rv_items" />

PS: I have taken your example and run the app on api 29 work fine cheers! PS:我以你的例子为例,在 api 29 上运行该应用程序,祝你好运! Also Do not make it listSize final , compiler will give error as you are not initializing也不要让它listSize final ,编译器会因为你没有初始化而出错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM