我有一个recyclerview,其中有一个弹出菜单的图像。我想知道如何确定弹出菜单点击了哪个recyclerview项目

时间:2015-08-08 18:28:31

标签: android android-recyclerview recycler-adapter

这是我的recyclerview item_courses.xml:

 <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="20dp">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="121dp"

        android:stretchColumns="*">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/textView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_column="0"
                android:textColor="@color/testcolor"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_span="30"
                android:text="Course Name"
                android:paddingBottom="40dp"
                android:paddingRight="20dp"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_gravity="right"
                android:src="@drawable/btn_course_menu"

                android:layout_span="3"
                android:id="@+id/person_photo"
                android:onClick="CreatePopupMenu"
                android:layout_column="28" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">


                <TextView
                    android:id="@+id/textView2"
                    android:text="10 students"
                    android:layout_gravity="left"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/testcolor"
                    android:layout_column="0"
                    android:layout_span="13"
                    android:paddingTop="0dp"
                    android:paddingBottom="0dp"
                    />
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:gravity="right"
                    android:textColor="@color/testcolor"
                    android:layout_span="13"
                    android:layout_column="20" />

        </TableRow>

    </TableLayout>

</android.support.v7.widget.CardView>

这是我的recyclerview适配器

public class MyRecyclerViewAdapter extends RecyclerView
    .Adapter<MyRecyclerViewAdapter
    .DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<Person> mDataset;
private static MyClickListener myClickListener;

public static class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    TextView subject;
    TextView studentno;
    TextView classno;

    public DataObjectHolder(View itemView) {
        super(itemView);
        subject = (TextView) itemView.findViewById(R.id.textView);
        studentno = (TextView) itemView.findViewById(R.id.textView2);
        classno = (TextView) itemView.findViewById(R.id.textView3);

        Log.i(LOG_TAG, "Adding Listener");
        itemView.setOnClickListener(this);
    }



    @Override
    public void onClick(final View v) {
        myClickListener.onItemClick(getPosition(), v);

    }


}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(ArrayList<Person> myDataset) {
    mDataset = myDataset;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_courses, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.subject.setText(mDataset.get(position).getmText1());
    holder.studentno.setText(mDataset.get(position).getmText2());
    holder.classno.setText(mDataset.get(position).getmText3());

}

public void addItem(Person dataObj, int index) {
    mDataset.add(index, dataObj);
    notifyItemInserted(index);
}

public void deleteItem(int index) {
    mDataset.remove(index);
    notifyItemRemoved(index);
}

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

public interface MyClickListener {
    public void onItemClick(int position, View v); 
}

}

假设它们只是popum菜单中命名的一个项目(名为:popup_menu.xml)。

menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/option1"

    android:title="Option 1" />

所以当点击option1时。如何确定点击哪个Recyclerview项目?

2 个答案:

答案 0 :(得分:1)

弹出式菜单锚定到ImageView项目中的RecyclerView(星号):

popup menu anchored to image view

选择弹出菜单项时显示的RecyclerView项目的位置:

popup menu recyclerview item position

为实现这一目标,我们需要修改您的DataObjectHolder以执行两项操作:

  1. 单击时将弹出菜单锚定到图像视图。
  2. 处理弹出菜单项单击。
  3. 我们使用自定义监听器PopupMenuListener来传递RecyclerView 项,它在适配器中的位置是特定的,托管刚刚点击的弹出菜单。< / p>

    修改后的DataObjectHolder

    public static class DataObjectHolder extends RecyclerView.ViewHolder implements View
            .OnClickListener, PopupMenu.OnMenuItemClickListener {
        private final MyClickListener myClickListener;
        private final PopupMenuListener popupMenuListener;
        TextView subject;
        TextView studentno;
        TextView classno;
    
        View imageView;
    
        public interface MyClickListener {
            void onItemClick(int position, View v);
        }
    
        public interface PopupMenuListener {
            void onPopupMenuClicked(MenuItem menuItem, int adapterPosition);
        }
    
        public DataObjectHolder(View itemView, MyClickListener myClickListener, PopupMenuListener popupMenuListener) {
            super(itemView);
    
            this.myClickListener = myClickListener;
            this.popupMenuListener = popupMenuListener;
    
            subject = (TextView) itemView.findViewById(R.id.textView);
            studentno = (TextView) itemView.findViewById(R.id.textView2);
            classno = (TextView) itemView.findViewById(R.id.textView3);
    
            imageView = itemView.findViewById(R.id.person_photo);
            // Configure image view to show a popup menu when it is clicked:
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showPopupMenu(view);
                }
            });
    
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
    
        }
    
    
    
        @Override
        public void onClick(final View v) {
            myClickListener.onItemClick(getAdapterPosition(), v);// IMPORTANT: 'getPosition()' is deprecated.
        }
    
      private void showPopupMenu(View view) {
          PopupMenu popup = new PopupMenu(view.getContext(), view);
          MenuInflater inflater = popup.getMenuInflater();
          inflater.inflate(R.menu.popup_menu, popup.getMenu());
          popup.setOnMenuItemClickListener(this);
          popup.show();
      }
    
        /*
         * Popup menu callback.
         */
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.option1:
                    // Call popup menu listener passing the menu item that was clicked as well as the recycler view item position:
                    popupMenuListener.onPopupMenuClicked(item, getAdapterPosition());
                    return true;
                default:
                    return false;
            }
        }
    
    }
    

    只需实施PopupMenuListener并将其传递给DataObjectHolder的构造函数,我们就可以了!

答案 1 :(得分:0)

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter. XInfoHolder > {



public MyAdapter(Context ctx,ArrayList<Data> dataInfo) {

 ..........
 ........
}

@Override
public PersonInfoHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    //* creating view from the layout for child element in the listview by using inflater*//*
    View itemView = ........
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(PersonInfoHolder personInfoHolder, int i) {   ...
....

}

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

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

    protected ImageView popupMenu;

    public XInfoHolder(View itemView) {
        super(itemView);
        popupMenu = (ImageView) itemView.findViewById(R.id.pop_up);
        popupMenu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {


        if (getLayoutPosition() == 0/something else && v.getId() == R.id.pop_up) {
            //show your popup all other place view is invisible
            //do whatever else here
        } 
        }
    }
}

}

这就是我为我的项目解决这个问题的方法。谢谢,祝你好运。