英文:
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'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 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>
# 答案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:
<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" />
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论