如何从适配器类打开弹出窗口

2024-04-15

我创建了一个类,用于在 RecyclerView 内的 CardView 中显示员工详细信息。新的要求是点击时显示卡片的弹出窗口。所以我添加了 setOnClickListener 并且它工作正常(我在日志中得到了卡名)。但是如何通过单击打开弹出窗口呢?其实我是android新手

public class EmployeeCardAdapter extends RecyclerView.Adapter<EmployeeCardAdapter.ViewHolder> {

private Context context;
private EmployeeBean employeeBean;
private ArrayList<EmployeeBean> employeeList;



public EmployeeCardAdapter(Context context, ArrayList<EmployeeBean> employeeList){
    this.context = context;
    this.employeeList = employeeList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

   View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_card, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    EmployeeBean employeeBean = employeeList.get(position);
    viewHolder.employeeCardTitle.setText(employeeBean.getName());
    viewHolder.employeeName.setText(employeeBean.getName());
    viewHolder.employeeQualification.setText(employeeBean.getQualification());
    viewHolder.employeeExperiance.setText(employeeBean.getExperiance());

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


public static class  ViewHolder extends RecyclerView.ViewHolder{

    public TextView employeeCardTitle;
    public TextView employeeName;
    public TextView employeeQualification;
    public TextView employeeExperiance;

    public View view;
    public ClipData.Item currentItem;
    public ViewHolder(final View itemView) {
        super(itemView);
        employeeCardTitle = (TextView)itemView.findViewById(R.id.employee_card_title);
        employeeName = (TextView)itemView.findViewById(R.id.employee_name);
        employeeQualification = (TextView)itemView.findViewById(R.id.employee_Qualification);
        employeeExperiance = (TextView)itemView.findViewById(R.id.employee_experiance);

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



            }
        });
    }
}

}

我搜索了很多教程,但每个教程都是从 Activity 类打开弹出窗口。但这里我使用的是片段。我设计了一个弹窗布局如下

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

    <TextView
        android:id="@+id/employee_card_title"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/employee_card"
        android:text="contact det"
        android:gravity="center"
        android:textColor="@color/text_shadow_white"
        android:textStyle="bold"
        android:textSize="18dp"/>

        <ImageView
            android:id="@+id/employee_image"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ic_profile"
            android:layout_marginTop="4dp"
            android:gravity="center_horizontal"
            android:layout_below="@id/employee_card_title"
           />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_name_text_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/employee_image">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Name"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_name"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_qualification_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_name_text_layout_id">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="qualification"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_Qualification"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_experiance_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_qualification_layout_id">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Experiance"
            android:textSize="15dp"
            android:textColor="@color/text_dark_black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_experiance"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:textColor="@color/text_dark_black"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/card_experiance_layout_id"
    android:layout_marginBottom="4dp">

    <Button
        android:id="@+id/listen_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"/>

</LinearLayout>

我可以从上面的适配器类中以弹出窗口的形式打开此布局吗?


如果您想在特定位置打开弹出窗口而不是需要在 showasdropdown 中给出位置,请尝试此操作。

public void showPopup(View view) {
        View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(popupView, 0, 0);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从适配器类打开弹出窗口 的相关文章

  • Android版本App更新代码

    我读到如果我们想更新Google Play中的应用程序 版本代码应该高于以前的apk文件 我有一个版本代码为 20 且版本名称为 1 0 的应用程序 那么要更新app 应该如何增加版本号呢 应该增加10吗 或者仅仅 1 就足够了 即版本代码
  • Android 从 C++ 端播放原始音频

    我需要能够在 Android 系统的 C 端以自定义文件格式传输音频 我正在致力于移植自定义媒体播放器 并且需要能够打开自定义文件并从中传输音频 这很重要 因为我认为从性能角度来看将整个播放器移植到 JAVA 是不可行的 并且通过 JNI
  • 如何在 M1 Mac 上运行的模拟器上运行旧版 Android 版本(例如 API 级别 21)?

    虽然现在有一个适用于 M1 mac 的 Android Studio 和支持arm架构的Android模拟器镜像 https stackoverflow com questions 64907154 android studio emula
  • 片段内容下方是否存在持久性 BottomSheet?

    Using a 持久底表 https material google com components bottom sheets html bottom sheets persistent bottom sheets 在一个协调器布局 htt
  • 如何使用数据绑定将点击侦听器设置为 LinearLayout

    我目前正在尝试将点击侦听器设置为LinearLayout查看在 xml使用数据绑定的布局文件 我已经设法让它在其他视图上很好地工作 比如Button or TextView 但由于某种原因 它不能与LinearLayout 这是我尝试的基本
  • 如何实现 ALTER TABLE 的示例[重复]

    这个问题在这里已经有答案了 我已经多次问过这个问题 但尚未得到完整的答案 如何实现 ALTER TABLE 语句以向数据库添加列 有人可以给我举个例子吗 请阅读SQLite ALTER TABLE 参考 http sqlite org la
  • 如何调试“com.android.okhttp”

    在android kitkat中 URLConnection的实现已经被OkHttp取代 如何调试呢 OkHttp 位于此目录中 external okhttp android main java com squareup okhttp 当
  • 在android中从JSON生成listview

    我对 Android 完全陌生 目前正在尝试从从我的服务器中提取的 JSON 数组生成列表视图 我已经阅读了很多教程 但没有运气 有一种独特的方法可以做到这一点 请您指出一些适合开始的资源 我读过了this http www josecgo
  • Android studio - 如何保存先前活动中选择的数据

    这是我的代码片段 这Textview充当按钮并具有Onclicklistner在他们 当cpu1000时Textview单击它会导致cpu g1000其代码如下所示的类 public class Game 1000 extends AppC
  • SQLite FTS4 使用特殊字符进行搜索

    我有一个 Android 应用程序 它使用 FTS4 虚拟表在 SQLite 数据库中搜索数据 它工作正常 但是当表中的数据包含特殊字符 如 或 时 SQLite MATCH 函数不会给出任何结果 我现在迷路了 谢谢 注意 默认的分词器真的
  • 如何在单个查询中搜索 RealmObject 的 RealmList 字段

    假设我有一堂课 public class Company extends RealmObject private String companyId private RealmList
  • 警报对话框中的 Webview 不显示内容

    我正在开发一个 Android 应用程序 我需要在网络视图和警报对话框上显示一个网站 该站点显示在网络视图中 但不显示在警报对话框中 到目前为止 这是我的代码 WebView WebView myWebView WebView v find
  • Android Gradle 问题 - Flutter / Dart

    我的 Gradle 同步有问题 我使用 IntelliJ 和 Android Studio 构建 Flutter Dart 应用程序 我添加了 2 个新的依赖项 现在 Gradle 出现了问题 在 Android Studio 中一切正常
  • Android应用主题更换流畅

    我正在开发一个提供白天和夜间主题的项目 我正在更改主题 夜间主题 AppCompatDelegate setDefaultNightMode AppCompatDelegate MODE NIGHT YES 日主题 AppCompatDel
  • react-native android fontFamily 不生效

    问题一 我在index android js的欢迎样式中添加了fontFamily 但没有效果 fontFamily 真的可以在 Android 上使用吗 欢迎 字体大小 20 fontFamily roboto thin 文本对齐 居中
  • 蓝牙发送和接收文本数据

    我是 Android 开发新手 我想制作一个使用蓝牙发送和接收文本的应用程序 我得到了有关发送文本的所有内容逻辑工作 但是当我尝试在手机中测试它时 我看不到界面 这是Main Activity Code import android sup
  • 找不到数据库路径是不可能的

    我对 android 开发很陌生 现在我正在尝试通过扩展 SQLiteOpenHelper 的类创建数据库 我确信数据存储在我的 Nexus 7 我用来测试应用程序的设备 上的某个位置 但是我找不到数据库的路径 我四处寻找其他类似的问题 所
  • 如何使用 isInEditMode() 在编辑器中查看自定义视图的布局

    我必须编辑一个具有自定义视图的软件 当我尝试编辑布局 xml 时 Eclipse 告诉我 在自定义视图中使用 View isInEditMode 可以在显示时跳过代码 在日食中 但我不知道how and where我必须使用isInEdit
  • 如何模拟ARM处理器运行环境并加载Linux内核模块?

    我尝试加载我的vmlinux into gdb并使用 ARM 内核模拟器 但我不明白为什么我会得到Undefined target command sim 这是外壳输出 arm eabi gdb vmlinux GNU gdb GDB 7
  • 在线性布局内的 ScrollView 内并排对齐 TextView

    我有一个带有滚动视图的线性布局 我想保留它的当前格式 但只需将 textView2a 和 textView3a 并排放置 而不会破坏我当前的布局格式 我已经包含了我最近的尝试 但它们似乎不正确 提前致谢 Java菜鸟 当前有效的 XML

随机推荐