简体   繁体   English

RecyclerView 只显示一项

[英]RecyclerView show only one item

I have a problem, I want to display a list in my second Activity but it only displays the last item and not the previous ones.我有一个问题,我想在我的第二个 Activity 中显示一个列表,但它只显示最后一个项目,而不是前一个项目。 I need to display all my items on Activity so.我需要在 Activity 上显示我的所有项目。

What do I need to do to display them all?我需要做什么才能全部显示?

My 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);
}

My Adapter:我的适配器:

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());

    }
}
}

As @Mehul Kabaria said, the problem lies in the layout of your item views.正如@Mehul Kabaria 所说,问题在于您的项目视图的布局。 The root view in pokemon_bank.xml has the layout_width and layout_height set to match_parent . pokemon_bank.xml中的根视图将layout_widthlayout_height设置为match_parent

So actually all your items are displayed and if you scroll, you will see that they are there.所以实际上你所有的项目都会显示出来,如果你滚动,你会看到它们在那里。 But each item fills the whole screen which makes it look like only one item is displayed.但是每个项目都填满了整个屏幕,这使得它看起来好像只显示了一个项目。

Change the width and height dimensions of the root view in pokemon_bank.xml to wrap_content or a fixed size.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>

Depending on whether you use a LinearLayoutManager for a vertical or horizontal list, you can set either the width or the height to match_parent .根据您是否将 LinearLayoutManager 用于垂直或水平列表,您可以将宽度高度设置为match_parent

Edit:编辑:

Also, it looks like you have an error in the binding of your model data to the item view in the RecyclerView Adapter: This here uses static methods to get values from the class MyPokemonBank .此外,在将 model 数据绑定到 RecyclerView 适配器中的项目视图时,您似乎遇到了错误:这里使用 static 方法从MyPokemonBank获取值

public void display(MyPokemonBank myPokemonBank) {

    this.mPokemonName.setText(MyPokemonBank.getName());
    this.mPokemonType.setText(MyPokemonBank.getType());

}

Instead, they should use the parameter (lowercase myPokemonBank ):相反,他们应该使用参数(小写的myPokemonBank ):

public void display(MyPokemonBank myPokemonBank) {

    this.mPokemonName.setText(myPokemonBank.getName());
    this.mPokemonType.setText(myPokemonBank.getType());

}

Do not use match_parent height for your recycler view item view.不要将match_parent高度用于您的回收站视图项目视图。 Because One item fills the whole screen so you do not see another.因为一个项目填满了整个屏幕,所以你看不到另一个。

instead of this line:而不是这一行:

mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));

try this one hope will work for you试试这个希望对你有用

mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

You code is working fine for me.你的代码对我来说很好。 I suspect layout problems.我怀疑布局问题。 Can you post your R.layout.pokemon_bank ?你能发布你的R.layout.pokemon_bank吗?

It's worked fine for me with this layout:这种布局对我来说效果很好:

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

That is my second activity XML:那是我的第二个活动 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>

and pokemon_bank.xml和 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>

I have checked your layout code and problem is in your pokemon_bank.xml.我检查了您的布局代码,问题出在您的 pokemon_bank.xml 中。 You need to change from match_parent to wrap_content to your linear layout compat like this it will work fine.您需要从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>

You need to change from horizontal to vertical also into your main activity where you are setting layout manager like this mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));您需要从水平更改为垂直也进入您正在设置布局管理器的主要活动mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

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

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