简体   繁体   English

Snapchat 像回收站视图中的“添加朋友”按钮 android

[英]Snapchat like Add Friend button in recycler view android

I am trying to create a Snapchat Add Friend button like this -我正在尝试创建一个像这样的 Snapchat 添加朋友按钮 -

在此处输入图像描述

Below is the code for my XML layout of the recylerView item -下面是我的 recyclerView 项目的 XML 布局的代码 -

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="position"
            type="java.lang.Integer" />

        <variable
            name="viewModel"
            type="com.app.thingleapplication.viewModel.SearchFriendsViewModel" />
    </data>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="3dp"
        android:elevation="3dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/data_holder"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/guideline2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">


                <TextView
                    android:id="@+id/search_result_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:fontFamily="@font/avenirltstd_black"
                    android:maxLines="2"
                    android:paddingStart="10dp"
                    android:paddingLeft="10dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:text="@{viewModel.getItemAt(position).userName}"
                    android:textSize="18sp"
                    app:layout_constraintBottom_toTopOf="@id/search_result_email"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.1"
                    app:layout_constraintVertical_chainStyle="packed" />

                <TextView
                    android:id="@+id/search_result_email"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_action_email"
                    android:drawablePadding="5dp"
                    android:ellipsize="end"
                    android:fontFamily="@font/avenirltstd_book"
                    android:gravity="center_vertical"
                    android:lines="1"
                    android:paddingStart="10dp"
                    android:paddingTop="5dp"
                    android:paddingEnd="10dp"
                    android:paddingBottom="5dp"
                    android:text="@{viewModel.getItemAt(position).email}"
                    android:textSize="16sp"
                    app:layout_constraintBottom_toTopOf="@id/search_result_phone"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/search_result_name" />

                <TextView
                    android:id="@+id/search_result_phone"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_action_phone"
                    android:drawablePadding="5dp"
                    android:ellipsize="end"
                    android:fontFamily="@font/avenirltstd_book"
                    android:gravity="center_vertical"
                    android:lines="1"
                    android:paddingStart="10dp"
                    android:paddingTop="5dp"
                    android:paddingEnd="10dp"
                    android:paddingBottom="5dp"
                    android:text="@{viewModel.getItemAt(position).phone}"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />


            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent=".75" />


            <FrameLayout
                android:id="@+id/add_button"
                android:layout_width="80dp"
                android:layout_height="30dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/add_friend_button_border"
                android:elevation="5dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/guideline2"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/button_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/add_button"
                    android:textColor="@color/white"
                    android:visibility="visible"
                    android:textSize="16sp" />
                <ProgressBar
                    android:id="@+id/progress"
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_gravity="center"
                    android:indeterminate="true"
                    android:visibility="gone"
                    android:theme="@style/progress" />


            </FrameLayout>

            <FrameLayout
                android:id="@+id/sent_button"
                android:layout_width="80dp"
                android:layout_height="30dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/sent_button_background"
                android:elevation="5dp"
                android:visibility="@{viewModel.getSentVisibility(position)}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/guideline2"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/button_sent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/sent_button"
                    android:textColor="@color/white"
                    android:textSize="16sp" />

            </FrameLayout>


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</layout>

How do i toggle the visibility of the progressbar inside my add_button frame layout in onBindViewholder.如何在 onBindViewholder.add_button 框架布局中切换进度条的可见性。 I tried it...it works but due to recyclerViews recycling...some another view's progress bar also starts showing up in recyclerview and the "ADD" text is gone.我试过了……它可以工作,但是由于 recyclerViews 回收……另一个视图的进度条也开始出现在 recyclerview 中,并且“添加”文本消失了。 Can anyone help me fix this?谁能帮我解决这个问题?

You didn't post any code except your layout, and the code is where the problem is.除了布局之外,您没有发布任何代码,而代码就是问题所在。 Yes recycler view can be a pain but all you have to do is specifically set the visibility of the control every time, not just when making it visible.是的,recycler view 可能会很痛苦,但您所要做的就是每次都专门设置控件的可见性,而不仅仅是在使其可见时。 You also want to specifically set it invisible everywhere else.您还想专门将其设置为在其他任何地方不可见。 You MUST do this because, as you know, the controls get reused.您必须这样做,因为如您所知,控件会被重用。 That means their settings such as visibility get reused as well.这意味着它们的设置(例如可见性)也会被重用。

You want to see something odd, try this with an edittext.你想看到一些奇怪的东西,用编辑文本试试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM