728x90
https://pasongsong.tistory.com/219
[수정] position 위치를 가져오는 Click
Click Linstener 만들기
public interface OnItemClickListener {
void onItemClick(View view, int pos);
}
// 리스너 객체 참조를 저장하는 변수
private OnItemClickListener mListener = null ;
// OnItemClickListener 리스너 객체 참조를 어댑터에 전달하는 메서드
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener ;
}
private ArrayList<One_food> food_list;
On Click 만들기
@Override
public void onBindViewHolder(@NonNull DayMealAdapter.ViewHolder holder, int position) {
holder.onBind(food_list.get(position));
holder.food_camera.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.w("TEST: ", "test camera" );
}
});
}
전체 코드
public class DayMealAdapter extends RecyclerView.Adapter<DayMealAdapter.ViewHolder> {
//btn
public interface OnItemClickListener {
void onItemClick(View view, int pos);
}
// 리스너 객체 참조를 저장하는 변수
private OnItemClickListener mListener = null ;
// OnItemClickListener 리스너 객체 참조를 어댑터에 전달하는 메서드
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener ;
}
private ArrayList<One_food> food_list;
//---------------
@NonNull
@Override
public DayMealAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DayMealAdapter.ViewHolder holder, int position) {
holder.onBind(food_list.get(position));
holder.food_camera.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.w("TEST: ", "test camera" );
}
});
}
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;
TextView food_day;
TextView food_na;
TextView food_dan;
TextView food_dang;
TextView food_ma;
TextView food_zin;
TextView food_ene;
TextView food_p;
TextView food_wei;
TextView food_fat;
TextView food_fe;
TextView food_k;
TextView food_ca;
TextView food_chol;
TextView food_tan;
TextView food_trans;
Button food_camera;
ViewHolder(View itemView) {
super(itemView) ;
// 뷰 객체에 대한 참조. (hold strong reference)
food_img = (ImageView) itemView.findViewById(R.id.IV_food);
food_day = (TextView) itemView.findViewById(R.id.text_day);
food_na = (TextView) itemView.findViewById(R.id.text_na);
food_dan = (TextView) itemView.findViewById(R.id.text_dan);
food_dang = (TextView) itemView.findViewById(R.id.text_dang);
food_ma = (TextView) itemView.findViewById(R.id.text_ma);
food_zin = (TextView) itemView.findViewById(R.id.text_zin);
food_ene = (TextView) itemView.findViewById(R.id.text_ene);
food_p = (TextView) itemView.findViewById(R.id.text_p);
food_wei = (TextView) itemView.findViewById(R.id.text_wei);
food_fat = (TextView) itemView.findViewById(R.id.text_fat);
food_fe = (TextView) itemView.findViewById(R.id.text_fe);
food_k = (TextView) itemView.findViewById(R.id.text_k);
food_ca = (TextView) itemView.findViewById(R.id.text_ca);
food_chol = (TextView) itemView.findViewById(R.id.text_chol);
food_tan = (TextView) itemView.findViewById(R.id.text_tan);
food_trans = (TextView) itemView.findViewById(R.id.text_trans);
food_camera = (Button) itemView.findViewById(R.id.btn_camera);
}
void onBind(One_food item){
food_img.setImageResource(item.getFood_resourceId());
food_day.setText(item.getDay());
food_na.setText(item.getNa());
food_dan.setText(item.getDan());
food_dang.setText(item.getDang());
food_ma.setText(item.getMa());
food_zin.setText(item.getZin());
food_ene.setText(item.getEne());
food_p.setText(item.getP());
food_wei.setText(item.getWei());
food_fat.setText(item.getFat());
food_fe.setText(item.getFe());
food_k.setText(item.getK());
food_ca.setText(item.getCa());
food_chol.setText(item.getChol());
food_tan.setText(item.getTan());
food_trans.setText(item.getTrans());
}
}
}
728x90
'Android' 카테고리의 다른 글
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 버튼 intent 사용하기 (0) | 2022.10.09 |
---|---|
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 Intent 사용하기 (0) | 2022.10.09 |
[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 |