简体   繁体   中英

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

I try to add two image in recyclerview and want show horizontal. But when I do debug I see it only add last 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 . How can add this 3 image?

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

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

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

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

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

You are creating new instance of your supermarketList ArrayList when adding item inside your supermarketList list

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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