Я пытаюсь добавить два изображения в recyclerview
и хочу показать горизонталь.Но когда я делаю отладку, я вижу только добавление последней imageview
.Он добавляет два других изображения перед последним, но каждый шаг list
начинается с 0
, поэтому он добавляет последнее и показывает 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);
}
}}
frag_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>