728x90
반응형

 

Glook_Adapter.java

package com.example.together.Group;

//import 부분은 지저분해서 지웠습니다.
//import android.widget.PopupMenu; 등 몇가지를 추가했던것 같은데..
//빨간줄 생기면 alt+enter로 추가하세요!


public class Glook_Adapter extends RecyclerView.Adapter<Glook_Adapter.CustomViewHoler> {


    private ArrayList<User_group> arrayList;
    private Context context;

    public Glook_Adapter(ArrayList<User_group> arrayList, Context context) {
        this.arrayList = arrayList;
        this.context = context;


    }

    @NonNull
    @Override
    public CustomViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.glook_list, parent, false);
        CustomViewHoler holer = new CustomViewHoler(view);
        return holer;

    }

    @Override
    public void onBindViewHolder(@NonNull final CustomViewHoler holder, int position) {
        Glide.with(holder.itemView)
                .load(arrayList.get(position).getiv_people())
                .into(holder.iv_people); */
        holder.uname.setText(arrayList.get(position).getuname());

	//커스텀 리스트뷰의 리스트 자체를 가져옴.
        holder.itemView.setTag(position);
        //롱클릭 시
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
            	
                //여기서부터 팝업메뉴!
                PopupMenu popup= new PopupMenu(context.getApplicationContext(), view);//v는 클릭된 뷰를 의미

                popup.getMenuInflater().inflate(R.menu.user_menu, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.m1:
                                Toast.makeText(view.getContext(), "그룹장위임",Toast.LENGTH_SHORT).show(); //토스트로 실험
                                break;
                            case R.id.m2:
                                Toast.makeText(view.getContext(), "추방",Toast.LENGTH_SHORT).show(); //토스트로 실험
                                break;
                            default:
                                break;
                        }
                        return false;
                    }
                });
                popup.show();
                //여기까지 팝업메뉴!

                return true;
            }
        });



    }


    @Override
    public int getItemCount() {
        return (arrayList != null ? arrayList.size() : 0);
    }


    public class CustomViewHoler extends RecyclerView.ViewHolder {
        TextView uname;


        public CustomViewHoler(@NonNull View itemView) {
            super(itemView);
            this.uname = itemView.findViewById(R.id.username);

        }
    }

}

public void onBindViewHolder 안에서 CustomViewHoler에서 선언한 itemView를 롱 클릭했을때, 팝업 메뉴가 나오도록 한다.

728x90
반응형

+ Recent posts