RecyclerView #3 - 컨텍스트 메뉴 처리
RecyclerView #2 - 구분선 추가, 아이템간 간격 조절
RecyclerView #6 - ItemView를 클래스화 하기
RecyclerView #7 - ViewType 동적변경
CreateContextMenu 리스너 설정
public static class StdViewHolder extends RecyclerView.ViewHolder
implements View.OnCreateContextMenuListener{
}
public StdViewHolder(@NonNull View itemView) {
super(itemView);
this.textView = itemView.findViewById(R.id.textView);
itemView.setOnCreateContextMenuListener(this);
}
메뉴 구성
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem Edit = menu.add(Menu.NONE, R.id.add_menu, 1, "add");
MenuItem Delete = menu.add(Menu.NONE, R.id.delete_menu, 2, "delete");
Edit.setOnMenuItemClickListener(onMenuItemClickListener);
Delete.setOnMenuItemClickListener(onMenuItemClickListener);
}
메뉴 선택 처리
private final MenuItem.OnMenuItemClickListener onMenuItemClickListener = new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_menu:
return true;
case R.id.delete_menu:
return true;
}
return false;
}
};
전체코드
public class StdRecyclerAdapter extends RecyclerView.Adapter<StdRecyclerAdapter.StdViewHolder> {
Context mContext;
List<String> mdata;
RecyclerView recyclerView;
public StdRecyclerAdapter(Context context) {
this.mContext = context;
}
public void setData(List<String> data) {
mdata = data;
notifyDataSetChanged();
}
@NonNull
@Override
public StdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflate = LayoutInflater.from(mContext);
View view = inflate.inflate(R.layout.list_item, parent, false);
StdViewHolder vh = new StdViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(@NonNull StdViewHolder holder, int position) {
holder.textView.setText(mdata.get(position));
}
@Override
public int getItemCount() {
return mdata.size();
}
public static class StdViewHolder extends RecyclerView.ViewHolder
implements View.OnCreateContextMenuListener{
public TextView textView;
public StdViewHolder(@NonNull View itemView) {
super(itemView);
this.textView = itemView.findViewById(R.id.textView);
itemView.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem Edit = menu.add(Menu.NONE, R.id.add_menu, 1, "add");
MenuItem Delete = menu.add(Menu.NONE, R.id.delete_menu, 2, "delete");
Edit.setOnMenuItemClickListener(onMenuItemClickListener);
Delete.setOnMenuItemClickListener(onMenuItemClickListener);
}
private final MenuItem.OnMenuItemClickListener onMenuItemClickListener = new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_menu:
return true;
case R.id.delete_menu:
return true;
}
return false;
}
};
}
}
'안드로이드' 카테고리의 다른 글
RecyclerView #5 - 아이템 선택 처리하기 (4) | 2019.03.19 |
---|---|
RecyclerView #4 - 아이템 클릭 처리 (4) | 2019.03.18 |
RecyclerView #2 - 구분선 추가, 아이템간 간격 조절 (0) | 2019.03.18 |
RecyclerView #1- 구조 및 기본 사용법 (0) | 2019.03.13 |
코드 모음 (0) | 2019.03.08 |