zoukankan      html  css  js  c++  java
  • 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);
    }
    

    成功截图如下:
    1.PNG

  • 相关阅读:
    小白必读:闲话HTTP短连接中的Session和Token
    网络编程懒人入门(六):深入浅出,全面理解HTTP协议
    IM系统的MQ消息中间件选型:Kafka还是RabbitMQ?
    致我们再也回不去的 Github ...
    了不起的WebRTC:生态日趋完善,或将实时音视频技术白菜化
    网络编程懒人入门(六):史上最通俗的集线器、交换机、路由器功能原理入门
    盘点微信的前世今生,微信成功的必然和偶然
    微信七年回顾:历经多少质疑和差评,才配拥有今天的强大
    写给小白的实时音视频技术入门提纲
    jenkins使用jacoco插件检测代码覆盖率(八)
  • 原文地址:https://www.cnblogs.com/aWxvdmVseXc0/p/12113136.html
Copyright © 2011-2022 走看看