【问题标题】:Expanding/Collapsing CardView with click or swipe gesture (Android)?通过单击或滑动手势(Android)展开/折叠 CardView?
【发布时间】:2015-10-07 18:22:57
【问题描述】:

我正在寻找一种解决方案,它可以让我展开卡片视图以查看更多信息,然后轻松折叠它。 Google Keep 有这样的卡片示例。有谁知道他们是怎么做到的?我会创建 2 个版本的卡片视图(一个是折叠的,一个是展开的),然后使用 Animator 类和手势方法在两个视图之间转换吗?我正在使用 Recyclerview 来保存我的卡片视图。

如果它完全相关,我发现了这个:http://developer.android.com/training/animation/layout.html

【问题讨论】:

  • 卡片视图有两个部分,顶部总是会显示,底部会隐藏或显示,对吧?然后您可以在每次触摸卡片(或任何其他事件)时更改底部的可见性,并使卡片视图高度wrap_content。如果这如您所愿,那么您可以通过在anim 文件夹内的 xml 文件中使用 <set><scale> 轻松地制作动画。这是你要求的吗?如果是,请告诉我,我会发布它的代码。

标签: android android-layout animation android-recyclerview android-cardview


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
   //here put the view which is always visible
<LinearLayout
android:layout_width="match_parent"
android:visibilty="gone"
android:id="@+id/expandableLayout"
android:layout_height="wrap_content">
   //here put which will collapse and expand
</LinearLayout>
</android.support.v7.widget.CardView>

在你的数组列表对象类中扩展一个布尔值

  if (listobj.isexpanded)
    {
        holder.expandableLayout.setVisibility(View.VISIBLE);
    }
    else {
        holder.expandableLayout.setVisibility(View.GONE);
    }
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (listobj.isexpanded)
            {
                holder.expandableLayout.setVisibility(View.GONE);
                listobj.isexpanded=false;
                notifyItemChanged(position);
            }
            else {
                holder.expandableLayout.setVisibility(View.VISIBLE);
                listobj.isexpanded=true;
                notifyItemChanged(position);
            }
        }
    });

试试这样的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 2018-03-10
    • 2016-05-17
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多