java - 当我们单击相应项目时,如何在 recyclerview 项目顶部显示弹出窗口

标签 java android android-recyclerview

这是所需的屏幕设计:

here

根据上面的屏幕截图,我需要设计布局:当我单击 recyclerview 项目行的添加按钮时,添加需要显示的选项。

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cell"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorLightWhite">


    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="visible"
        android:layout_marginRight="@dimen/default_margin"
        android:layout_marginLeft="@dimen/default_margin"
        android:layout_marginTop="@dimen/default_margin"
        android:layout_marginBottom="@dimen/default_margin"
        android:weightSum="100">
        <EditText
            android:id="@+id/et_add_member"
            android:layout_width="wrap_content"
            android:layout_weight="95"
            android:layout_height="42dp"
            android:padding="@dimen/padding_8"
            android:hint="Person"
            android:background="@drawable/edit_box"/>
        <ImageView
            android:id="@+id/iv_addMember"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/default_margin"
            android:layout_centerVertical="true"
            android:layout_weight="5"
            android:src="@drawable/add_x" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/include_add"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_main"
        android:layout_alignTop="@+id/ll_main"
        android:layout_marginTop="35dp"
        android:layout_marginRight="@dimen/margin_10"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:orientation="vertical"
        android:visibility="gone">

        <include layout="@layout/include_members" />
    </LinearLayout>

</RelativeLayout>

这是显示弹出窗口的适配器代码:

holder.ivAddMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.include_add.setVisibility(View.VISIBLE);
            }
        });

include_add LinearLayout 是流行代码,但我无法使用此代码获得确切的 UI。

任何人都可以建议我如何在相应项目点击顶部显示弹出窗口,如何在先前单击项目位置关闭弹出窗口以及弹出窗口设计。

最佳答案

您必须在 res 文件夹中的菜单下创建 xml 文件,然后使用如下所示的函数在所需位置显示弹出窗口

public void showPopup() {
    PopupMenu popup = new PopupMenu(context, <PASS ID WHERE YOU WISH TO SHOW POPUP>);
    popup.getMenuInflater().inflate(R.menu.menu_option, popup.getMenu());
    Object menuHelper;
    Class[] argTypes;
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.member:
                    // call member class or function here
                    return true;
                case R.id.guest:
                    // call Guest class or function here
                    return true;
                case R.id.my_buddies:
                    // call My Buddies class or function here
                    return true;
            }
            return true;
        }
    });
    popup.show();
}

希望您能理解。

关于java - 当我们单击相应项目时,如何在 recyclerview 项目顶部显示弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918872/

相关文章:

java - 声明对象为 null 后向成员变量赋值时出错

java - 基于mask的字符串解析

android - 将体重值保存到 Google Fit 时为 "Application needs OAuth consent from the User"

java - 自定义 TextView 适配器不适用于 ListView

android - RecyclerView 行最大高度?

android - RecycleView 内的按钮在单击时没有反应

java - 使用求和聚合时出现空指针异常

java - 静态方法和实例方法的内存表示之间的区别

java - setImageBitmap() 比 image.setImageDrawable()、image.setImageResource() 慢

android - 回收站 View 中的项目重叠