【问题标题】:How to displayed information on pop up window from the card view如何在卡片视图的弹出窗口中显示信息
【发布时间】:2019-04-09 03:30:38
【问题描述】:

我正在开发一个类似于 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;

            }
        });


    }

我正在寻求建议,让用户配置文件数据在弹出窗口中无缝更新。

值存储在下面代码中的“用户名”变量中

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) {

                    }
                });




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

【问题讨论】:

  • 值如何存储在“用户名”变量中?我认为这个变量的值没有更新。可以写个日志语句或者调试一下,每次userName的值是多少。
  • 我正在使用下面的代码将值存储在“用户名”变量中
  • 我已经用 'userName' 变量的代码更新了我的问题
  • 我还有一个问题。 'useremail, userName, userJob' 的值是否每次单击不同的项目时都会发生变化?
  • 只有在触摸卡片视图时,我才努力让这个值立即更改为弹出窗口。

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


【解决方案1】:

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

解决方案之一是:我假设您正在使用 recyclerview/listview 来显示其他用户信息。如果您正在使用 recycler-view,那么您还必须拥有它的适配器和它的 viewHolder。 Adapter有一个函数'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,所有这些都将起作用。如果您有任何其他疑问,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 2013-01-06
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多