[英]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.