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

我创建了一个类,用于在RecyclerView中的CardView中显示员工详细信息.新要求是在点击时显示卡片的弹出窗口.所以我添加了setOnClickListener,它工作正常(我在Log中得到了卡片名称).但是如何通过此点击打开弹出窗口?其实我是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);
    }
上一篇:WPF制作带明细的环形图表


下一篇:oc之mac中NSPopUpButton