如何使用RecyclerView显示数组?

huangapple go评论88阅读模式
英文:

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 display a String array. The issue is that the first element (with an index of zero) is missing on the screen, even though the element exists beyond the screen.

The buttons should be numbered starting from zero, and the array should be printed starting from the zeroth element. How can I solve this problem?

UPD: Here's the layout:

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 the recyclerView widget is used

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

<details>
<summary>英文:</summary>

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. 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).
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/RDEa2.png
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&#39;s the layout:
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">

&lt;TextView
android:id=&quot;@+id/item_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical|start&quot;
android:text=&quot;TextView&quot;
android:textSize=&quot;40sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/button_item_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical|end&quot;
android:layout_marginEnd=&quot;48dp&quot;
android:padding=&quot;10dp&quot;
android:text=&quot;INFO&quot; /&gt;

</FrameLayout>


activity_top, in which recyclerView widget is used

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

&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/listOfGoods&quot;
android:layout_width=&quot;409dp&quot;
android:layout_height=&quot;729dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:listitem=&quot;@layout/rv_item&quot; /&gt;

</androidx.constraintlayout.widget.ConstraintLayout>


</details>
# 答案1
**得分**: 1
我知道可能是一样的,但我想要证实一下,尝试修改这段代码:
```java
@Override
public int getItemCount() {
return listSize;
}

为这段代码:

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

还有一件事:尝试调试这个对象 this.list = list; 并检查数组是否包含零索引元素。如果数组包含该元素,可能与布局有关,如 @Rui Alves 所提到的。

英文:

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

答案2

得分: 1

不要固定recyclerView的高度

正确的方式:

<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" />

附注:我采用了您的示例,并在API 29上运行应用程序,效果很好,欢呼!
同时不要将`listSize`设为`final`,否则编译器会报错,因为您没有初始化它。
英文:

Do not give fixed height of recyclerView

Correct Way:

&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/listOfGoods&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:listitem=&quot;@layout/rv_items&quot; /&gt;

P.S: I have taken your example and run the app on api 29 work fine cheers!
Also Do not make it listSize final, compiler will give error as you are not initializing

huangapple
  • 本文由 发表于 2020年9月8日 23:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63797579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定