单击RecyclerView时显示其他信息

问题描述

我有一个RecyclerView,其中包含从Firebase实时数据库提取名称列表,以及电话号码,电子邮件和地址。我已经为RecyclerView中显示的卡片创建了布局。我希望他们仅显示名称和图像,直到单击为止。单击后,它们将展开并显示电话号码,电子邮件和地址。我已经完成了所有这些工作,但是我面临的最后一个问题是RecyclerView的layout.xml中的RelativeLayout的setVisibility,因为它与属于我的应用程序不断崩溃的活动的XML不同。我曾尝试过多种解决方案来解决与我的问题类似的问题,但找不到与我面临相同问题的人。

现在,如果我使用此代码

mUserInfoLayout = (RelativeLayout) findViewById(R.id.user_additional_info_layout);
mUserInfoLayout.setVisibility(View.INVISIBLE);

我收到此错误

Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference

但是,如果我使用下面提供的代码,则布局保持可见。

我的MainActivity.java OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAuth = FirebaseAuth.getInstance();

    //Firebase
    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    mUserDatabase.keepSynced(true);

    //mUserSingleMainLayout = (RelativeLayout) findViewById(R.id.user_single_main_layout);


    mUsersList = (RecyclerView) findViewById(R.id.users_list);
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new linearlayoutmanager(this));


    View usersingleView = getLayoutInflater().inflate(R.layout.users_single_layout,null);
    mUserInfoLayout = (RelativeLayout) usersingleView.findViewById(R.id.user_additional_info_layout);
    mUserInfoLayout.setVisibility(View.INVISIBLE);

    //Buttons
    mUserAdd = (FloatingActionButton) findViewById(R.id.user_add_btn);

    mUserAdd.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent createuserIntent = new Intent(MainActivity.this,createuserActivity.class);
            startActivity(createuserIntent);

        }
    });


}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/user_add_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="20dp"
    app:fabSize="normal"
    app:rippleColor="@color/colorPrimary"
    android:src="@drawable/ic_add_white_24dp"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/users_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"/>


</RelativeLayout>

我的user_single_layout.xml在RecyclerView中使用的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/user_single_image"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:src="@drawable/account_circle" />

<TextView
    android:id="@+id/user_single_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="100dp"
    android:layout_marginTop="20dp"
    android:text="@string/display_name_users"
    android:textColor="@color/colorBlack"
    android:textSize="18sp" />


  <RelativeLayout
     android:id="@+id/user_additional_info_layout"
     android:animateLayoutChanges="true"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@id/user_single_name"
     android:layout_marginTop="10dp"
     android:layout_alignParentStart="true"
     android:layout_marginStart="100dp">

    <ImageView
        android:id="@+id/user_phone_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/user_single_phone"
        android:src="@drawable/ic_phone_black_24dp"/>

    <ImageView
        android:id="@+id/user_email_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/user_single_email"
        android:layout_below="@+id/user_phone_image"
        android:src="@drawable/ic_email_black_24dp"/>

    <ImageView
        android:id="@+id/user_address_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/user_single_address"
        android:src="@drawable/ic_map_marker_black_24dp" />

    <TextView
        android:id="@+id/user_single_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/user_phone_image"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:text="Phone" />

    <TextView
        android:id="@+id/user_single_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_below="@+id/user_single_phone"
        android:layout_toRightOf="@+id/user_email_image"
        android:layout_marginTop="10dp"
        android:text="Email" />
    <TextView
        android:id="@+id/user_single_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/user_single_email"
        android:layout_toRightOf="@id/user_address_image"
        android:text="Address" />

  </RelativeLayout>

</RelativeLayout>

在user_single_layout.xml中,我要在MainActivity.java中以编程方式使“ @ + id / user_additional_info_layout” RelativeLayout不可见

解决方法

编辑

我的答案集中在Fragment生命周期上。活动生命周期不同,并且不包含我建议的方法。

问题是您尝试访问的视图为空。该视图尚未在onCreate方法中放大,因此尚不可用。

在onCreate()中不应执行与视图相关的逻辑。

您需要使用onViewCreated()方法访问具有以下签名的视图:

@Override
public void onViewCreated(View view,Bundle savedInstanceState)
{
    
}

因此,我建议您将所有与视图相关的逻辑都移到该方法中。

,

首先,将您的代码移至recyclerView适配器。 其次,使relativeLayout可见性消失并将其更改为linearLayout。 第三,使用此代码进行扩展。

private fun animateView(
view: View,duration: Long,initialHeight: Int,targetHeight: Int,onAnimationEnd: () -> Unit = {}
  ) {
      view.apply {
       val animator = ValueAnimator.ofInt(initialHeight,targetHeight)
         animator.addUpdateListener {
          layoutParams.height = it.animatedValue as Int
          requestLayout()
          if (it.animatedValue as Int > 2) {
              //Make the view visible in java 
              //yourview.setVisibility(View.Visible)
              show()
          }
      }
       animator.addListener(object : Animator.AnimatorListener {
           override fun onAnimationEnd(animation: Animator) {
              layoutParams.height = targetHeight
              onAnimationEnd.invoke()
           }

           override fun onAnimationStart(animation: Animator) {

           }

           override fun onAnimationCancel(animation: Animator) {}
           override fun onAnimationRepeat(animation: Animator) {}
       })
       animator.interpolator = AccelerateDecelerateInterpolator()
       animator.duration = duration
       animator.start()
   }
  }
  fun View.expandAnimation(duration: Long) {
     measure(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
     val initialHeight = 0
     val targetHeight = measuredHeight

     layoutParams.height = 0
     animateView(this,duration,initialHeight,targetHeight)
    }

 fun View.collapseAnimation(duration: Long) {
     val initialHeight = measuredHeight
     val targetHeight = 0

    animateView(this,targetHeight) {
       hide()
    }
  }

 fun View.hide() {
    this.apply {
       visibility = View.GONE
   }
 }

 fun View.show() {
    this.apply {
       visibility = View.VISIBLE
   }
  }

您可以轻松地将此代码转换为Java 对不起,我没有时间自己进行转换