linearLayout onClickListener "перекрывает" родительскую группу просмотра onLongClickListener - PullRequest
0 голосов
/ 05 ноября 2019

enter image description here

enter image description here

В моем приложении у меня есть чат, который работает как WhatsApp. У меня есть 2 вида профилей сообщений - отправитель и получатель.

У меня проблема с сообщениями типа контакта - они, как вы можете видеть, как для отправителя, так и для получателя, имеют верхнюю линейную разметку.

В этом верхнем макете есть прослушиватель кликов, который отлично работает и запускает новое намерение как для профиля отправителя, так и для профиля получателя.

Моя проблема заключается в том, что когда сообщение о контакте находится в профиле «отправителя», longClickListener вообще не работает - работает только обычный прослушиватель щелчков. Только если я закомментирую код прослушивателя щелчков, длинный слушатель щелчков начнет работать.

Я пытаюсь выяснить, в чем разница между отправителем и получателем, который отключает возможность длинного щелчка.

- вот мой метод getContactView(), который связывает элемент строки со всеми соответствующими данными. Я выделил место, где добавляется clickListener -

private void getContactView(RecyclerView.ViewHolder holder, final Message item,
                                MessageDetail messageDetail, int position) {
        try {
            ContactViewHolder contactHolder = (ContactViewHolder) holder;
            adjustPadding(contactHolder.getSpace(), position);
            contactHolder.getViewSender().setVisibility(View.GONE);
            contactHolder.getViewReceiver().setVisibility(View.GONE);
            ContactMessage contactMessage = messageDetail.getContact();
            String contactName = contactMessage.getName();
            String time = item.getMsgTime();

            String domain = com.twoverte.utils.Constants.MESSAGE_DOMAIN;
            String phone = contactMessage.getPhoneNumber().get(0).replace("+","");
            Roster r = VerteDatabaseManager.ROSTER.getRoster(phone + domain);

            if (r != null) {
                contactHolder.getInviteRec().setText("Message");
            }

            // <-- this is the point where the click listener for the top layout is being set. if I 
            //disable this lines of code, the longClickListener will work but I do need these logics. 
            contactHolder.getSendContactLay().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<Contact> contacts = new ArrayList<>();
                    Contact c = new Contact();
                    c.setContactName(contactMessage.getName());
                    c.setContactNos(String.valueOf(contactMessage.getPhoneNumber()).replace("]","").replace("[",""));
                    contacts.add(c);

                    ((Activity) context).startActivityForResult(new Intent(context,
                                    PickContactActivity.class).putParcelableArrayListExtra(Constants.USERNAME,
                            (ArrayList<Contact>) contacts),
                            Constants.SELECT_CONTACT_REQ_CODE);
                }
            });

            contactHolder.getInviteRec().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (r != null) {
                        context.startActivity(new Intent(context, ChatViewActivityImplementation.class)
                                .putExtra(LibConstants.JID, r.getJid())
                                .putExtra(Constants.CHAT_TYPE, r.getRosterType()));
                    }else{
                        if (phone != null) {
                            Uri uri = Uri.parse("smsto:" + phone);
                            Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
                            smsIntent.putExtra("sms_body", context.getResources().getString(R.string.mail_message));
                            smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(smsIntent);
                        }
                    }
                }
            });

            contactHolder.getAddContactRec().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_INSERT);
                    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                    intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
                    ((Activity)context).startActivityForResult(intent, INSERT_CONTACT);
                }
            });

            contactHolder.getMessageSendTxt().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (r != null) {
                        context.startActivity(new Intent(context, ChatViewActivityImplementation.class)
                                .putExtra(LibConstants.JID, r.getJid())
                                .putExtra(Constants.CHAT_TYPE, r.getRosterType()));
                    }else{
                        Uri uri = Uri.parse("smsto:" + phone);
                        Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
                        smsIntent.putExtra("sms_body", context.getResources().getString(R.string.mail_message));
                        smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(smsIntent);
                    }
                }
            });



            if (time != null) {
                time = chatMsgTime.getDaySentMsg(context, Long.parseLong(time));
            }
            if (item.getIsSender() == Constants.CHAT_FROM_SENDER) {
                contactHolder.getViewSender().setVisibility(View.VISIBLE);
                contactHolder.getTxtSendTime().setText(time);
                contactHolder.getTxtSendName().setText(contactName);
                setStatus(item, contactHolder.getImgSenderStatus());
                if (item.getFavourite() != null && item.getFavourite())
                    contactHolder.getStarredSentImage().setVisibility(View.VISIBLE);
                else
                    contactHolder.getStarredSentImage().setVisibility(View.GONE);
                contactHolder.getViewSender().setVisibility(View.VISIBLE);
                contactHolder.getTxtSendTime().setText(time);
                contactHolder.getTxtSendName().setText(contactName);
                setStatus(item, contactHolder.getImgSenderStatus());
                if (item.getFavourite() != null && item.getFavourite())
                    contactHolder.getStarredSentImage().setVisibility(View.VISIBLE);
                else
                    contactHolder.getStarredSentImage().setVisibility(View.GONE);
                new ContactReplyViewUtils().showSenderReplyWindow(contactHolder, item, context,
                        messageDetail);

                if(forwardUtils.isSendForwardUtils(userJID,messageDetail.getCreatedBy(),item.getGroupChatSender()!= null && item.getGroupChatSender().length()>0
                        ?item.getGroupChatSender() : item.getChatUser()))
                    contactHolder.getForwardSendLayout().setVisibility(View.VISIBLE);
                else
                    contactHolder.getForwardSendLayout().setVisibility(View.GONE);

            } else {
                contactHolder.getViewReceiver().setVisibility(View.VISIBLE);
                contactHolder.getTxtRevTime().setText(time);
                contactHolder.getTxtRevContact().setText(contactName);

                if(forwardUtils.isReceiveForwardUtils(userJID,messageDetail.getCreatedBy(),item.getGroupChatSender()!= null && item.getGroupChatSender().length()>0
                        ?item.getGroupChatSender() : item.getChatUser()))
                    contactHolder.getForwardReceiveLayout().setVisibility(View.VISIBLE);
                else
                    contactHolder.getForwardReceiveLayout().setVisibility(View.GONE);

                if (item.getFavourite() != null && item.getFavourite())
                    contactHolder.getStarredReceivedImage().setVisibility(View.VISIBLE);
                else
                    contactHolder.getStarredReceivedImage().setVisibility(View.GONE);
                new ContactReplyViewUtils().showReceiverReplyWindow(contactHolder, item, context,
                        messageDetail);
            }
            chatUtils.setSelectedChatItem(contactHolder.getViewRowItem(), item,
                    selectedMessages, context);
            setListenersForContactMessages(contactHolder, item, position);
            senderItemClick(contactHolder.getViewSender().getRootView(), item, position);
            receiverItemClick(contactHolder.getViewReceiver(), item, position);
        } catch (Exception e) {
            LogMessage.e(Constants.TAG, e);
        }
    }

, вот мой xml, как показано на первом изображении -

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

    <ViewStub
        android:id="@+id/date_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/row_chat_view_date" />

    <include layout="@layout/row_favourite_message_item" />

    <LinearLayout
        android:id="@+id/row_chat_contact"
        style="@style/ChatView">
        <include layout="@layout/view_chat_space" />

        <LinearLayout
            android:id="@+id/view_rev_contact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <ViewStub
                android:id="@+id/sender_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout="@layout/row_chat_sender_name"
                android:layout_marginLeft="50dp"
                android:layout_marginBottom="8dp"
                />


      <!--      <RelativeLayout
                android:id="@+id/view_rev_contact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">-->

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/view_chat_receive_img_lay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/row_text_left_arrow_cardview"
                android:layout_toRightOf="@+id/row_text_left_arrow_cardview"
                android:background="@drawable/shape_chat_receiver_bg"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:padding="8dp">



                <include
                    layout="@layout/row_reply_received_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


                <include
                    android:id="@+id/view_forward_contact_received"
                    layout="@layout/forward_text_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:id="@+id/view_rev_contact_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="220dp"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_avator_rev_image"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/ic_contact_img"
                        android:gravity="center"
                        android:textColor="@color/color_white"
                        android:textSize="24sp"
                        app:font_name="@string/font_roboto_medium" />

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_contact_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minWidth="140dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:gravity="center_vertical|start"
                        android:maxLength="20"
                        android:maxLines="1"
                        android:minEms="3"
                        android:textColor="@color/color_chat_sender"
                        android:textSize="16sp"
                        app:font_name="@string/font_roboto_regular" />

                    <ImageView
                        android:id="@+id/ic_star_received"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:paddingBottom="2dp"
                        android:contentDescription="@null"
                        android:src="@drawable/ic_star"
                        />

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_contact_rev_time"
                        style="@style/ChatTimeViewDark"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:gravity="bottom"
                        android:layout_marginLeft="5dp"
                        android:layout_marginStart="5dp"
                        app:font_name="@string/font_roboto_light" />
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/view_editor"
                    android:layout_marginTop="4dp"
                    android:background="@color/ef_grey" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="@color/ef_grey" />

                    <TextView
                        android:id="@+id/invite_rec_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minHeight="30dp"
                        android:layout_weight="3"
                        android:padding="@dimen/margin_10_dp"
                        android:gravity="center"
                        android:textColor="@color/color_black"
                        android:text="Invite"/>

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="@color/ef_grey" />

                    <TextView
                        android:id="@+id/add_contact_rec_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="@dimen/margin_10_dp"
                        android:minHeight="30dp"
                        android:gravity="center"
                        android:textColor="@color/color_black"

                        android:text="Add Contact"/>
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="@color/ef_grey" />
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/view_editor"
                    android:background="@color/ef_grey" />
            </LinearLayout>


            <android.support.v7.widget.CardView
                android:id="@+id/row_text_left_arrow_cardview"
                android:layout_alignTop="@+id/view_chat_receive_img_lay"
                android:layout_width="20dp"
                android:layout_height="20dp"
                app:cardCornerRadius="5dp"
                android:layout_marginLeft="18dp"
                android:layout_marginRight="10dp"
                >

                <ImageView
                    android:id="@+id/row_text_left_arrow"
                    android:contentDescription="@string/title_app_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/user_avatar"
                    android:background="@color/white"
                    android:scaleType="centerCrop"
                    />
            </android.support.v7.widget.CardView>

<!--            <ImageView
                android:id="@+id/row_text_left_arrow"
                style="@style/Receiver_bubble"
                android:layout_alignBottom="@+id/view_chat_receive_img_lay" />-->

        </RelativeLayout>
     </LinearLayout>

        <View
            android:id="@+id/view_favourite_separator"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="15dp"
            android:background="@color/color_light_gray"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/view_send_contact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|center_vertical">


            <LinearLayout
                android:id="@+id/view_chat_send_img_lay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_chat_sender_bg"
                android:orientation="vertical"
                android:padding="5dp">

                <include
                    android:id="@+id/view_text_sent_reply"
                    layout="@layout/row_reply_text_item"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <include
                    android:id="@+id/view_forward_contact_send"
                    layout="@layout/forward_text_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:id="@+id/send_contact_lay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minWidth="200dp"
                    android:layout_gravity="end"
                    android:orientation="horizontal">

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_avator_send_image"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/ic_contact_img"
                        android:gravity="center"
                        android:textColor="@color/color_black"
                        android:textSize="24sp"
                        app:font_name="@string/font_roboto_medium" />

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_contact_send_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:minWidth="140dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:gravity="center_vertical|start"
                        android:maxLength="20"
                        android:maxLines="1"
                        android:minEms="3"
                        android:textColor="@color/color_black"
                        android:textSize="16sp"
                        app:font_name="@string/font_roboto_regular" />

                    <ImageView
                        android:id="@+id/ic_star_sent"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:contentDescription="@null"
                        android:src="@drawable/ic_star"
                        android:paddingBottom="3dp"
                        android:visibility="gone"
                        />

                    <com.twoverte.views.CustomTextView
                        android:id="@+id/text_contact_send_time"
                        style="@style/ChatTimeViewLight"
                        android:layout_gravity="bottom|end"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="1dp"
                        android:textColor="@color/color_black"
                        app:font_name="@string/font_roboto_regular" />

                    <ImageView
                        android:id="@+id/image_contact_status"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:scaleType="centerInside"
                        android:layout_gravity="bottom"
                        android:layout_marginLeft="5dp"
                        android:layout_marginStart="5dp"
                        android:layout_marginTop="1dp"
                        android:contentDescription="@string/title_content_discription"
                        android:minWidth="20dp"
                        android:src="@drawable/ic_timer" />

                </LinearLayout>

                <View
                    android:layout_width="270dp"
                    android:layout_marginTop="4dp"
                    android:layout_height="1dp"
                    android:background="@color/ef_grey" />

                <TextView
                    android:id="@+id/message_send_txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="20dp"
                    android:padding="10dp"
                    android:textColor="@color/color_black"
                    android:gravity="center"
                    android:text="Message"/>


            </LinearLayout>

            <ImageView
                android:id="@+id/row_text_right_arrow"
                style="@style/Sender_bubble"
                android:layout_alignBottom="@+id/view_chat_send_img_lay"
                android:layout_toEndOf="@+id/view_chat_send_img_lay"
                android:layout_toRightOf="@+id/view_chat_send_img_lay" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

Что осталось от моей активности - в моемАктивность Я использую setOnItemLongClickListener для объекта Recyclerview.

Есть идеи, почему такое поведение происходит только как профиль "отправителя"?

...