如何从适配器类打开弹出窗口 [英] How to open a popup window from an adapter class

查看:60
本文介绍了如何从适配器类打开弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a class for showing employee details in a CardView inside a RecyclerView . New requirement is to show a popup window of card while clicking. So I added setOnClickListener and it works fine(I got card name in Log). But how do I open a popup window from this click? Actually am a newbie in 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类打开弹出窗口.但是我在这里使用片段.我设计了一个弹出窗口布局,如下所示

I searched a lot of tutorials but every tutorials are opening the popup window from Activity class. But here I am using fragment. I designed a popup window layout as follows

<?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>

我可以从适配器类上方作为弹出窗口打开此布局吗?

Can I open this layout as a popup window from above adapter class ??

推荐答案

如果要在特定位置打开弹出窗口而不是在showasdropdown中指定位置,请尝试此操作.

Once try this if you wants to open popup window at particular position than you need give position in 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);
    }

这篇关于如何从适配器类打开弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆