简体   繁体   English

为什么recyclerview不添加所有list.size?

[英]Why the recyclerview don't add all list.size?

I try to add two image in recyclerview and want show horizontal. 我尝试在recyclerview添加两个图像,并希望显示水平。 But when I do debug I see it only add last imageview . 但是当我调试时,我看到它只添加了最后一个imageview It add other two image before last one but every step list start from 0 so it add last one and show list.size equals 1 that should be 3 . 它在最后一个之前添加了另外两个图像,但是每个步骤list0开始,所以它添加最后一个并显示list.size等于1应该是3 How can add this 3 image? 如何添加这3张图片?

Supermarket.java Supermarket.java

public class Supermarket {
    private int supermarketImage;

    public Supermarket(int supermarketImage) {
        this.supermarketImage = supermarketImage;

    }
    public int getSupermarketImage() {
        return supermarketImage;
    }

    public void setSupermarketImage(int supermarketImage) {
        this.supermarketImage = supermarketImage;
    }
}

StoresFragment.java StoresFragment.java

public class StoresFragment extends Fragment {

    RecyclerView mRecyclerView;
    List<Supermarket> supermarketList;
    Supermarket supermarket;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_stores, container, false);

        mRecyclerView = view.findViewById(R.id.recyclerview1);
        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(horizontalLayoutManager);



        supermarketList = new ArrayList<>();
        supermarket = new Supermarket(R.drawable.ayuzbir_logo);
        supermarketList.add(supermarket);
        supermarketList = new ArrayList<>();
        supermarket = new Supermarket(R.drawable.migros_logo);
        supermarketList.add(supermarket);
        supermarketList = new ArrayList<>();
        supermarket = new Supermarket(R.drawable.carrefour_logo);
        supermarketList.add(supermarket);


        StoresAdapter myAdapter = new StoresAdapter(getActivity(), supermarketList);
        mRecyclerView.setAdapter(myAdapter);
        return view;
    }
}

StoresAdapter.java StoresAdapter.java

public class StoresAdapter extends RecyclerView.Adapter<StoresAdapter.ViewHolder> {

    private Context mContext;
    private List<Supermarket> supermarketList;
    private ImageView mImage;

    public StoresAdapter(Context mContext, List<Supermarket> supermarketList) {
        this.mContext = mContext;
        this.supermarketList = supermarketList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.stores_item_view, parent, false);
        return new ViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
       mImage.setImageResource(supermarketList.get(position).getSupermarketImage());

    }

    @Override
    public int getItemCount() {
        return supermarketList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.imageView_store);
        }
}}

fragment_stores.xml fragment_stores.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@color/colorBackground"

    android:clickable="true"
    android:focusable="true"
    tools:context=".StoresFragment">

    <!-- TODO: Update blank fragment layout -->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_fr_stores"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textV_toolbar_title_fr_stores"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="16dp"
                android:text="Mağazalar"
                android:textColor="@android:color/white"
                android:textSize="18sp" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        app:layout_constraintVertical_bias="0.050000012" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Süpermarketler"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_fr_stores" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Elektronik"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerview1" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scrollbars="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView7"
        app:layout_constraintVertical_bias="0.120000005" />
</android.support.constraint.ConstraintLayout>

stores_item_view.xml stores_item_view.xml

<android.support.constraint.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="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="15dp"
        android:orientation="horizontal"
        app:cardCornerRadius="8dp"
        app:cardElevation="1dp"
        app:cardMaxElevation="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imageView_store"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ayuzbir_logo" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

Why the recyclerview don't add all list.size? 为什么recyclerview不添加所有list.size?

You are creating new instance of your supermarketList ArrayList when adding item inside your supermarketList list supermarketList列表中添加项目时,您正在创建supermarketList ArrayList的新实例

Use this 用这个

    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.ayuzbir_logo);
    supermarketList.add(supermarket);
    supermarket = new Supermarket(R.drawable.migros_logo);
    supermarketList.add(supermarket);
    supermarket = new Supermarket(R.drawable.carrefour_logo);
    supermarketList.add(supermarket);

Instead of this 而不是这个

    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.ayuzbir_logo);
    supermarketList.add(supermarket);
    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.migros_logo);
    supermarketList.add(supermarket);
    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.carrefour_logo);
    supermarketList.add(supermarket);

If your image numbers is fixed then use the following pics of code... 如果您的图像编号是固定的,请使用以下代码图片...

int[] images = {R.drawable.ayuzbir_logo, R.drawable.migros_logo, R.drawable.carrefour_logo};
supermarketList = new ArrayList<>();

for (int image : images) {
    supermarketList.add(new Supermarket(image));
}

And then use supermarketList object 然后使用supermarketList对象

Thanks. 谢谢。

you did silly mistake just remove below lines of code 你做了愚蠢的错误只是删除下面的代码行

    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.ayuzbir_logo);
    supermarketList.add(supermarket);
    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.migros_logo);
    supermarketList.add(supermarket);
    supermarketList = new ArrayList<>();
    supermarket = new Supermarket(R.drawable.carrefour_logo);
    supermarketList.add(supermarket);

in place above code use this one 在代码上面使用这个

    supermarketList = new ArrayList<>();
    supermarketList.add(0, new Supermarket(R.drawable.ayuzbir_logo));
    supermarketList.add(1, new Supermarket(R.drawable.migros_logo));
    supermarketList.add(2, new Supermarket(R.drawable.carrefour_logo));

according to your code it will store only the last image. 根据您的代码,它将只存储最后一张图片。 Because You are using the same object of your supermarket class. 因为您正在使用超市类的同一对象。 You have to create new object for new item like this :- 你必须为这样的新项目创建新对象: -

List<Supermarket> supermarketList;

for(int i = 0; i <= 3; i++){
Supermarket supermarket;
if(i == 0){
 supermarket = new Supermarket(R.drawable.ayuzbir_logo);
        supermarketList.add(supermarket);
}else if(i == 1){
supermarket = new Supermarket(R.drawable.migros_logo);
        supermarketList.add(supermarket);
}else if(i == 2){
supermarket = new Supermarket(R.drawable.carrefour_logo);
        supermarketList.add(supermarket);
}
}

or there is also another way to do this : 或者还有另一种方法可以做到这一点:

supermarketList = new ArrayList<>();
supermarketList.add(0, new Supermarket(R.drawable.ayuzbir_logo));
supermarketList.add(1, new Supermarket(R.drawable.migros_logo));
supermarketList.add(2, new Supermarket(R.drawable.carrefour_logo));

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

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