728x90
1. Build.gradle 수정
implementation 'com.android.support:recyclerview-v7:28.0.0'
https://pasongsong.tistory.com/123
2. Item layout 구성
하나 만들어 줍니다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/IV_food"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toTopOf="@+id/BTN_insert_food"
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"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/BTN_insert_food"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="319dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. Class 만들기
package com.example.k_food_application;
public class One_food {
int food_resourceId;
public One_food(int food_resourceId){
this.food_resourceId = food_resourceId;
}
public int getFood_resourceId(){
return food_resourceId;
}
public void setFood_resourceId(int food_resourceId){
this.food_resourceId = food_resourceId;
}
}
4. Adapter 구현
https://developer.android.com/guide/topics/ui/layout/recyclerview?hl=ko
package com.example.k_food_application;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class DayMealAdapter extends RecyclerView.Adapter<DayMealAdapter.ViewHolder> {
private ArrayList<One_food> food_list;
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
@NonNull
@Override
public DayMealAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_food, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DayMealAdapter.ViewHolder holder, int position) {
holder.onBind(food_list.get(position));
}
public void setFoodList(ArrayList<One_food> list){
this.food_list = list;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return food_list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView food_img;
public ViewHolder(@NonNull View itemView) {
super(itemView);
food_img = (ImageView) itemView.findViewById(R.id.IV_food);
}
void onBind(One_food item){
food_img.setImageResource(item.getFood_resourceId());
}
}
}
5. Main 수정
//recycler View 연결
RecyclerView one_food = (RecyclerView) findViewById(R.id.Recyc_food);
DayMealAdapter one_food_Adapter = new DayMealAdapter();
one_food.setAdapter(one_food_Adapter);
one_food.setLayoutManager(new LinearLayoutManager(this));
ArrayList<One_food> meal_items = new ArrayList<>();
meal_items.add(new One_food(R.drawable.ic_camera));
one_food_Adapter.setFoodList(meal_items);
728x90
'Android' 카테고리의 다른 글
[Android Studio] 안드로이드 스튜디오 JAVA Recycler View class 만들어서 추가하기 (0) | 2022.10.09 |
---|---|
[Android Studio] 안드로이드 스튜디오 Java class 생성 (0) | 2022.09.19 |
[Android Studio] 안드로이드 스튜디오 Java firebase DB 연동 (0) | 2022.09.19 |
[Android Studio] 안드로이드 스튜디오 Java Intent, 창 변환 (0) | 2022.09.19 |
[Android Studio] 안드로이드 스튜디오 Java image Button 생성하기 (0) | 2022.05.25 |