如何从即时贴视图在弹出窗口中显示信息

时间:2019-04-09 03:30:38

标签: java android firebase android-arrayadapter android-cardview

我正在开发与tinder类似的应用,但这不是约会应用。

我正在使用卡片查看功能,遇到了一些我无法解决的错误。

这是我已成功编码(暂时)的应用的名片查看功能。

主布局(卡的顶部)包含配置文件图像和用户名。 第二个是包含文本字段的弹出窗口。

用户将在卡视图中看到其他用户的详细信息。

我可以选择通过触摸“更多信息”按钮来查看有关另一个用户的更多信息。 选择更多信息按钮后,弹出窗口将设置为动画

用户可以通过触摸图标以减少“更多信息”显示来返回默认视图。

目前,执行滑动后,我在卡视图的弹出窗口中显示的信息面临问题。 问题是,执行刷卡操作并显示新用户卡后,弹出窗口中的文本字段需要无缝更新。

用户信息通过Firebase实时数据库

卡片视图


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@android:color/white"
    android:clipToPadding="false"
    android:outlineProvider="bounds"
    android:paddingLeft="1sp"
    android:paddingTop="1sp"
    android:paddingRight="1sp"
    android:paddingBottom="1sp">

  <android.support.v7.widget.CardView
      android:id="@+id/cw1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_gravity="center"
      android:elevation="0dp"
      app:cardCornerRadius="2dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.54"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">


    <android.support.constraint.ConstraintLayout

        android:id="@+id/job12"
        android:layout_width="match_parent"

        android:layout_height="match_parent"
        android:animateLayoutChanges="false"
        android:background="@android:color/black"
        android:clickable="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498">

      <ImageView
          android:id="@+id/imgRandomxx"
          android:layout_width="137dp"
          android:layout_height="2dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:layout_weight="1"
          android:background="@android:color/black"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.502"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.0" />

      <ImageView
          android:id="@+id/imagexx"
          android:layout_width="0dp"
          android:layout_height="641dp"
          android:layout_marginStart="1dp"
          android:layout_marginEnd="1dp"
          android:layout_marginBottom="8dp"
          android:background="@android:color/black"
          android:scaleType="centerCrop"

          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.0" />

      <TextView
          android:id="@+id/textView2zz"
          android:layout_width="28dp"
          android:layout_height="23dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:background="@drawable/common_google_signin_btn_icon_dark"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.93"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.13999999" />

      <View
          android:id="@+id/item2_swipe_right_indicator"
          android:layout_width="133dp"
          android:layout_height="133dp"
          android:layout_gravity="right"
          android:layout_marginStart="8dp"


          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:alpha="0"
          android:background="@drawable/sswwepleft"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="1.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.312" />

      <View
          android:id="@+id/item2_swipe_left_indicator"
          android:layout_width="127dp"
          android:layout_height="140dp"
          android:layout_marginStart="8dp"

          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:alpha="0"
          android:background="@drawable/swweeprright"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.007"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.316" />

      <TextView
          android:id="@+id/namexx"
          android:layout_width="136dp"
          android:layout_height="35dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:background="@android:color/transparent"
          android:gravity="start"
          android:paddingLeft="20sp"
          android:textAllCaps="true"
          android:textColor="@android:color/white"
          android:textSize="30sp"
          app:fontFamily="@font/calibril"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.006"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.85"
          tools:text="hello" />

      <TextView
          android:id="@+id/textViewdditanssjob"
          android:layout_width="39dp"
          android:layout_height="29dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:fontFamily="@font/calibril"
          android:maxLength="4"
          android:maxLines="1"
          android:textColor="@android:color/white"
          android:textSize="18sp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.018"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.939" />

      <TextView
          android:id="@+id/textView15"
          android:layout_width="104dp"
          android:layout_height="37dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:fontFamily="@font/calibril"

          android:text="km away"
          android:textColor="@android:color/white"
          android:textSize="18sp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.116"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.95" />

      <TextView
          android:id="@+id/scoretestViewxx"
          android:layout_width="106dp"
          android:layout_height="45dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:gravity="center"
          android:text="0"
          android:textAllCaps="true"
          android:textColor="@android:color/white"
          android:textSize="35sp"
          android:textStyle="bold"
          app:fontFamily="@font/wcmanonegrabta"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"

          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.042" />

      <TextView
          android:id="@+id/textViewxxyy"
          android:layout_width="209dp"
          android:layout_height="30dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:gravity="center"
          android:text="about me"
          android:textColor="@android:color/black"
          android:textSize="19sp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.442"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="1.0" />

      <Button
          android:id="@+id/button4infoor"
          android:layout_width="45dp"
          android:layout_height="45dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:background="@drawable/infoicon"
          android:clickable="false"
          android:onClick="animateViewFromBottomToTop"
          android:stateListAnimator="@null"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.968"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.87" />


    </android.support.constraint.ConstraintLayout>


  </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

弹出窗口



 public  void animateViewFromBottomToTop( View view){



        // inflate the layout of the popup window
        LayoutInflater inflater2 = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);

        View popupView2 = inflater2.inflate(R.layout.popup_windowjj, null);


        Button btn= (Button) popupView2.findViewById(R.id.button4infoor3j);



        // create the popup window
        // int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        /// int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        int width = LinearLayout.LayoutParams.MATCH_PARENT;
        int height = (int)(getResources().getDisplayMetrics().heightPixels*0.50);

        boolean focusable = false; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow2 = new PopupWindow(popupView2, width, height, focusable);
        popupWindow2.setFocusable(false);
        popupWindow2.setOutsideTouchable(false);

        final String yourString = userName;

        final TextView popupText = (TextView) popupView2.findViewById(R.id.textView13j);

        popupText.setText(yourString);

        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken

        popupWindow2.setAnimationStyle(R.style.styleAnimationResourceCardviewpopup);
        popupWindow2.showAtLocation(popupView2, Gravity.BOTTOM, 0, 0);

        // dismiss the popup window when touched
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                popupWindow2.dismiss();
                return true;

            }
        });


    }

我正在寻找有关在弹出窗口中无缝更新用户个人资料数据的建议。

值从下面的代码存储在“ userName”变量中

flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
                @Override
                public void onItemClicked(int itemPosition, Object dataObject) {



                cardsxx obj = (cardsxx) dataObject;
                String userId = obj.getUserId();


                DatabaseReference userDb3 = usersDb4.child(obj.getUserId());


                userDb3.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()){
                            if (dataSnapshot.child("email").getValue() != null){
                                useremail = dataSnapshot.child("email").getValue().toString();
                                userName=dataSnapshot.child("name").getValue().toString();
                                userJob = dataSnapshot.child("industry").getValue().toString();

                            }
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });




            }
        });
``````````````````````````````````````````````````````

1 个答案:

答案 0 :(得分:0)

这里的问题是,当您单击“更多信息按钮”时,您无法传递最新的“用户名”值。

一种解决方案是:我假设您正在使用recyclerview / listview来显示其他用户信息。如果您使用的是recycler-view,则还必须具有它的适配器和它的viewHolder。适配器具有函数“ onBindViewHolder”,它将解析每个视图和数据对象,并且您需要使用此方法分配数据以进行视图。现在,您应该在此方法中实现“ button4infoor”的“ setOnClickListener”,并将其从.xml视图中删除。因此,下面提到的逻辑应该放在'onClick()'方法中

DatabaseReference userDb3 = usersDb4.child(obj.getUserId());


            userDb3.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        if (dataSnapshot.child("email").getValue() != null){
                            useremail = dataSnapshot.child("email").getValue().toString();
                            userName=dataSnapshot.child("name").getValue().toString();
                            userJob = dataSnapshot.child("industry").getValue().toString();
                            animateViewFromBottomToTop(useremail, userName, userJob);
                        }
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

并将用户相关的值传递给函数“ animateViewFromBottomToTop”并相应地设置视图。但是,如果您使用的是recyclerview / listview,那么所有这些都将起作用。让我知道您是否还有其他查询。