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

标签 android popup

我创建了一个类,用于在 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 类打开弹出窗口。但在这里我使用的是 fragment 。我设计了一个弹窗布局如下

<?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);
    }

关于android - 如何从适配器类打开弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383763/

相关文章:

android - View 不会与数据绑定(bind)

android - react-native android 应用程序在发布 apk 时崩溃

索引项目后Android工作室自动关闭

javascript - 如何在弹出窗口中启用window.status?

javascript - 弹出框中的 HTML 视频在 Chrome 中随机播放/不播放

java - 在android中制作多个http post

android - 谷歌地图自定义图 block 加载缓慢

javascript - 如何创建一个半透明的弹出框?

javascript - 更改已打开的弹出窗口的 url

paypal - Braintree Drop-In UI PayPal 按钮弹出窗口是否可以在 Adob​​e AIR 桌面应用程序内显示的网页上工作?