单击 recyclerview 元素时的动画

时间:2021-02-26 21:48:04

标签: android android-recyclerview android-animation

当我点击 recyclerview 元素时,我需要制作一个类似于下拉列表的动画。在 item 2 xml 标记中,1 是主要标记,第二个在单击时可见。按照思路,应该是这样的:我点击了元素,它打开了一个dropout动画。再次按下时,它关闭了。还有动画。此刻,元素只是忽然出现,也忽然消失。如果有这种可能性,我不想使用第三方库。我想做类似this

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (!flag) {
            Intent intent = new Intent(mContext, MessageActivity.class);
            intent.putExtra("userid", user.getId());
            mContext.startActivity(intent);

        } else if(holder.info.getVisibility()==View.GONE) { 
            holder.info.setVisibility(View.VISIBLE);

        } else {
            holder.info.setVisibility(View.GONE);
        }
    }
);

在这部分代码中,我要么打开消息活动,要么打开有关用户的其他信息。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="10dp"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/info_about">
            ///Info about users(username and him photo)

        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/info_about"
            android:paddingTop="5dp"
            android:layout_marginTop="45dp"
            android:layout_centerHorizontal="true"
            android:orientation="vertical"
            android:visibility="gone"
            android:id="@+id/info">

            ///Info about users(call_number, email... When i clicked on item)
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

试试这个...

holder.binding.cardview.setOnClickListener(v -> {


                    if (holder.binding.expandableView.getVisibility()==View.GONE){
                        TransitionManager.beginDelayedTransition(holder.binding.cardview, new AutoTransition());
                        holder.binding.expandableView.setVisibility(View.VISIBLE);
                    } else {
                        TransitionManager.beginDelayedTransition(holder.binding.cardview, new AutoTransition());
                        holder.binding.expandableView.setVisibility(View.GONE);

                    }

                    if (holder.binding.playlayout.getVisibility()==View.GONE){
                        TransitionManager.beginDelayedTransition(holder.binding.cardview, new AutoTransition());
                        holder.binding.playlayout.setVisibility(View.VISIBLE);
                    } else {
                        TransitionManager.beginDelayedTransition(holder.binding.cardview, new AutoTransition());
                        holder.binding.playlayout.setVisibility(View.GONE);

                    }

                });
            }
相关问题