2016-07-24 4 views
0

메모 앱을 만들려고하고 있으며 이미 리사이클러에 나열 할 항목 (메모)을 가지고 있습니다. 각 항목에 ImageButton이 있으며 클릭하면 popUpMenu가 나타나길 원합니다.Android recyclerView 어댑터 팝업 메뉴가 작동하지 않습니다.

개별 항목에 대한 imageButton을 클릭 할 수는 있지만 각 개별 항목에 popUpMenu가 표시되지 않습니다.

더 좋거나 다른 방법이 있다면 좋을 것입니다.

부품 XML (row_notes.xml)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="2dp" 
    card_view:cardElevation="0dp" 
    android:clickable="true" 
    android:layout_margin="5dp"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:background="@drawable/note_shape" 
      android:padding="3dp"> 

      <TextView 
       android:textColor="@color/primaryText" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="Note" 
       android:id="@+id/rowNoteTitle" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:paddingLeft="10dp" 
       android:textSize="20dp" 
       android:layout_gravity="top|left|center_vertical" 
       android:layout_marginTop="2dp" /> 

      <ImageButton 
       android:layout_width="20dp" 
       android:layout_height="match_parent" 
       android:id="@+id/rowNoteBtn" 
       android:layout_gravity="right|center_vertical" 
       android:background="@drawable/img_btn_shape" 
       android:src="@drawable/ic_dots_vertical_white_24dp" /> 

      <TextView 
       android:textColor="@color/secondaryText" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="Note amount" 
       android:id="@+id/rowNoteAmount" 
       android:layout_alignParentTop="true" 
       android:textSize="12dp" 
       android:layout_gravity="left|bottom" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="7dp" /> 

     </FrameLayout> 

     <ProgressBar 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="5dp" 
      android:id="@+id/progressBar" 
      android:max="100" 
      android:progressDrawable="@drawable/progress_color" 
      android:background="#ffffff" /> 

    </LinearLayout> 

리사이클 어댑터 (nRecyclerAdapter.java) POPUPMENU 코드 여기 :

public class nRecyclerAdapter extends RecyclerView.Adapter<nViewHolder> { 

private Context context; 
private ArrayList<Note> notes; 

public nRecyclerAdapter(Context context, ArrayList<Note> notes) { 
    this.context = context; 
    this.notes = notes; 
} 

@Override 
public nViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_notes, parent, false); 
    nViewHolder holder = new nViewHolder(v); 

    return holder; 
} 

@Override 
public void onBindViewHolder(final nViewHolder holder, final int position) { 

    holder.noteTitle.setText(notes.get(position).getNoteTitle()); 
    holder.noteAmount.setText(notes.get(position).getNoteAmount()); 

    holder.optionBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      //Toast.makeText(context, notes.get(position).getNoteTitle()+" click button works",Toast.LENGTH_SHORT).show(); 
      PopupMenu popupMenu = new PopupMenu(context,holder.optionBtn); 
      popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
       @Override 
       public boolean onMenuItemClick(MenuItem menuItem) { 
        String option = menuItem.getTitle().toString(); 
        Toast.makeText(context, notes.get(position).getNoteTitle(),Toast.LENGTH_SHORT).show(); 
        if(option.matches("Edit")){ 
         Toast.makeText(context, notes.get(position).getNoteTitle()+" Edit",Toast.LENGTH_SHORT).show(); 
        }else if(option.matches("Delete")){ 
         Toast.makeText(context, notes.get(position).getNoteTitle()+" Delete",Toast.LENGTH_SHORT).show(); 
        } 
        return true; 
       } 
      }); 
      popupMenu.show(); 
     } 
    }); 

    //listener 
    holder.setItemClickListener(new noteClickListener() { 
     @Override 
     public void onNoteItemClick(View v, int position) { 
      Toast.makeText(context, notes.get(position).getNoteTitle(),Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

@Override 
public int getItemCount() { 
    return notes.size(); 
} 

public void updateData(ArrayList<Note> mNotes){ 
    notes.clear(); 
    notes.addAll(mNotes); 
    notifyDataSetChanged(); 
} 

public void addItem(String title, String amount){ 
    notes.add(new Note(title,amount)); 
    notifyDataSetChanged(); 
} 

public void removeItem(int position){ 
    notes.remove(position); 
    notifyDataSetChanged(); 
} 

}

ViewHolder (nViewHolder .java) :

public class nViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {  

TextView noteTitle; 
TextView noteAmount; 
noteClickListener noteClickListener; 
ImageButton optionBtn; 

public nViewHolder(View itemView) { 
    super(itemView); 

    optionBtn = (ImageButton) itemView.findViewById(R.id.rowNoteBtn); 
    noteTitle = (TextView) itemView.findViewById(R.id.rowNoteTitle); 
    noteAmount = (TextView) itemView.findViewById(R.id.rowNoteAmount); 

    optionBtn.setOnClickListener(this); 
    itemView.setOnClickListener(this); 
} 

public void setItemClickListener(noteClickListener nc){ 
    this.noteClickListener = nc; 
} 

@Override 
public void onClick(View view) { 
    this.noteClickListener.onNoteItemClick(view,getLayoutPosition()); 
} 

}

OnClickListener를 클래스 (noteClickListener.java) :

import android.view.View; 

public interface noteClickListener { 
    void onNoteItemClick(View v, int position); 
} 

답변

1

당신은 어떤 /res/menu/popupmenu.xml 만들어야합니다 아래, 그리고

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:title="Menu Item" /> 
</menu> 

당신의 line PopupMenu popupMenu = new PopupMenu(context,holder.optionBtn);popupMenu.inflate(R.menu.popupmenu);

으로 메뉴를 부 풀릴 필요가 있습니다.
관련 문제