Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

一、问题描述

????记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!

????在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.testlong);,然后对变量longSetting调用setOnLongClickListener函数实现注册长按事件,但是在一个fragment控件中如此设置长按无法弹出popupwindows控件。在fragment控件无法实现长按事件弹出popwindows控件的代码如下:

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/LongSet"
    android:orientation="vertical"
    android:layout_height="match_parent">
        <ListView
            android:id="@+id/chat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

java代码:

View view = inflater.inflate(R.layout.chat,container,false);
LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.LongSet);
longSetting.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPop();
            return true;
        }
    });

二、解决方法

????解决方法也很迷!!!经过实验发现在根布局中再镶嵌一个布局把所有的控件装进去,然后将这个镶嵌的控件实例化,然后对该布局设置长按事件弹出popupwindows控件即可!!!相关代码如下:

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/LongSet"
android:orientation="vertical"
android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/testlong"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/chat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

java代码:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.chat,container,false);
    LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.testlong);
    longSetting.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPop();
            return true;
        }
    });
    ListView lv = (ListView)view.findViewById(R.id.chat);
    mdata = new LinkedList<RecvData>();
    recvItem = new RecvItem(mdata,getActivity());
    lv.setAdapter(recvItem);
    RecvMsgFromServer recvMsgFromServer = new RecvMsgFromServer(handler);
    recvMsgFromServer.start();
    return view;
}

public void showPop()
{
    View contentView = getActivity().getLayoutInflater().inflate(R.layout.pop,null);
    mPopWindow = new PopupWindow(contentView,ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT, true);
    mPopWindow.setContentView(contentView);
    TextView btn_send = (TextView)contentView.findViewById(R.id.send);
    TextView btn_cancle = (TextView)contentView.findViewById(R.id.exit);
    btn_cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPopWindow.dismiss();
        }
    });
    btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPopWindow.dismiss();
            alertDialog = new AlertSendMessage(getActivity(),handler).getAlert();
            alertDialog.show();
        }
    });
    ColorDrawable colorDrawable = new ColorDrawable(0xb0000000);
    mPopWindow.setBackgroundDrawable(colorDrawable);
    mPopWindow.setAnimationStyle(R.style.PopDh);
    View rootview = getActivity().getLayoutInflater().inflate(R.layout.chat,null);
    mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
}

成功截图如下:
Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

上一篇:[转帖]替代Windows还有多远?国产统一操作系统UOS上手体验


下一篇:windows server 2012 R2系统的服务器 自带的.NET 4.5卸载后,重启服务器黑屏的解决方法