为什么recyclerview不全部添加list.size?

时间:2019-07-08 06:17:44

标签: java android android-recyclerview

我尝试在recyclerview中添加两个图像,并希望水平显示。但是,当我进行调试时,只能看到最后一个imageview。它在最后一个图像之前添加了另外两个图像,但是每个步骤list0开始,因此它添加了最后一个图像并显示list.size等于1应该是3。如何添加这3张图片?

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>

4 个答案:

答案 0 :(得分:4)

  

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

supermarketList列表中添加项目时,您正在创建supermarketList ArrayList的新实例

使用此

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

代替这个

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

答案 1 :(得分:3)

如果您的图像编号是固定的,请使用以下代码图片...

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

然后使用supermarketList对象

谢谢。

答案 2 :(得分:1)

您确实犯了愚蠢的错误,只是删除了下面的代码行

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

在上面的代码中使用此代码

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

答案 3 :(得分:1)

根据您的代码,它将仅存储最后一张图像。因为您使用的是超市类的相同对象。您必须像这样为新项目创建新对象:-

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

或者还有另一种方法:

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