[英]RecyclerView show only one item
我有一个问题,我想在我的第二个 Activity 中显示一个列表,但它只显示最后一个项目,而不是前一个项目。 我需要在 Activity 上显示我的所有项目。
我需要做什么才能全部显示?
我的活动:
mRecyclerView = (RecyclerView)findViewById(R.id.home_recyclerview_pokemonname);
mPokemonList = new ArrayList<>();
mPokemonList.add(new MyPokemonBank("Pikachu", "Electrik"));
mPokemonList.add(new MyPokemonBank("Dracaufeu", "Feu"));
mPokemonList.add(new MyPokemonBank("Miaouss", "Normal"));
mAdapter = new MyPokemonAdapter(mPokemonList);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(mAdapter);
}
我的适配器:
public class MyPokemonAdapter extends RecyclerView.Adapter<MyPokemonAdapter.MyViewHolder> {
ArrayList<MyPokemonBank> mPokemonList;
MyPokemonAdapter(ArrayList<MyPokemonBank> mPokemonList){
this.mPokemonList = (ArrayList<MyPokemonBank>) mPokemonList;
}
@NonNull
@Override
public MyPokemonAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.pokemon_bank, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyPokemonAdapter.MyViewHolder holder, int position) {
holder.display(mPokemonList.get(position));
}
@Override
public int getItemCount() {
return mPokemonList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView mPokemonName;
private TextView mPokemonType;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mPokemonName = (TextView)itemView.findViewById(R.id.name);
mPokemonType = (TextView)itemView.findViewById(R.id.type);
}
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(MyPokemonBank.getName());
this.mPokemonType.setText(MyPokemonBank.getType());
}
}
}
正如@Mehul Kabaria 所说,问题在于您的项目视图的布局。 pokemon_bank.xml
中的根视图将layout_width
和layout_height
设置为match_parent
。
所以实际上你所有的项目都会显示出来,如果你滚动,你会看到它们在那里。 但是每个项目都填满了整个屏幕,这使得它看起来好像只显示了一个项目。
将pokemon_bank.xml
中根视图的宽度和高度尺寸更改为wrap_content
或固定大小。
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
根据您是否将 LinearLayoutManager 用于垂直或水平列表,您可以将宽度或高度设置为match_parent
。
编辑:
此外,在将 model 数据绑定到 RecyclerView 适配器中的项目视图时,您似乎遇到了错误:这里使用 static 方法从MyPokemonBank
获取值
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(MyPokemonBank.getName());
this.mPokemonType.setText(MyPokemonBank.getType());
}
相反,他们应该使用参数(小写的myPokemonBank
):
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(myPokemonBank.getName());
this.mPokemonType.setText(myPokemonBank.getType());
}
不要将match_parent
高度用于您的回收站视图项目视图。 因为一个项目填满了整个屏幕,所以你看不到另一个。
而不是这一行:
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
试试这个希望对你有用
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
你的代码对我来说很好。 我怀疑布局问题。 你能发布你的R.layout.pokemon_bank
吗?
这种布局对我来说效果很好:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
那是我的第二个活动 XML:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_recyclerview_pokemonname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>
和 pokemon_bank.xml
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
我检查了您的布局代码,问题出在您的 pokemon_bank.xml 中。 您需要从match_parent
更改为wrap_content
到您的线性布局兼容,这样它会正常工作。
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
您需要从水平更改为垂直也进入您正在设置布局管理器的主要活动mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.