我有一个带有以下视图的CardView:
- RelativeLayout (view_1)
    - TextView
    - TextView

- RelativeLayout (view_2)
    - ImageView
    - ImageView

当我打开我的ListView view_2设置为Visible.GONE所以你无法看到它.
现在我使用了this blog的ExpandAnimation.这里有代码:

/**
 *
 */
public class ExpandAnimation extends Animation {
    /**
     *
     */
    private AnimationCallback callback = null;
    /**
     *
     */
    private View viewToAnimate = null;
    /**
     *
     */
    private RelativeLayout.LayoutParams viewToAnimateLayoutParams = null;
    /**
     *
     */
    private int marginStart = 0;
    /**
     *
     */
    private int marginEnd = 0;
    /**
     *
     */
    private boolean isVisibleAfter = false;
    /**
     *
     */
    private boolean isEndedAlready = false;

    /**
     *
     * @param callback
     * @param view
     * @param duration
     */
    public ExpandAnimation(AnimationCallback callback,View view,int duration) {
        this.setDuration(duration);

        this.callback = callback;

        this.viewToAnimate = view;
        this.viewToAnimateLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

        this.isVisibleAfter = (view.getVisibility() == View.VISIBLE);

        this.marginStart = this.viewToAnimateLayoutParams.bottomMargin;
        this.marginEnd = (this.marginStart == 0 ? (0 - view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    /**
     *
     * @param interpolatedTime
     * @param transformation
     */
    @Override
    protected void applyTransformation(float interpolatedTime,Transformation transformation) {
        super.applyTransformation(interpolatedTime,transformation);

        if (interpolatedTime < 1.0f) {
            this.viewToAnimateLayoutParams.bottomMargin = this.marginStart + (int) ((this.marginEnd - this.marginStart) * interpolatedTime);
            this.viewToAnimate.requestLayout();
        } else if (!this.isEndedAlready) {
            this.viewToAnimateLayoutParams.bottomMargin = this.marginEnd;
            this.viewToAnimate.requestLayout();

            if (this.isVisibleAfter) {
                this.viewToAnimate.setVisibility(View.GONE);
            }

            this.isEndedAlready = true;

            this.callback.onAnimationCompleted(-1);
        }
    }
}

我在AdapterView中使用它:

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,AnimationCallback {
    private TextView textViewValue = null;
    private TextView textViewTime = null;
    private RelativeLayout relativeLayoutExpand = null;
    private ImageView imageViewMood = null;
    private ImageView imageViewMeal = null;

    public ViewHolder(View itemView) {
        super(itemView);

        this.textViewValue = (TextView) itemView.findViewById(R.id.textview_value);
        this.textViewTime = (TextView) itemView.findViewById(R.id.textview_time);
        this.relativeLayoutExpand = (RelativeLayout) itemView.findViewById(R.id.list_row_expand);
        this.imageViewMood = (ImageView) itemView.findViewById(R.id.imageview_mood);
        this.imageViewMeal = (ImageView) itemView.findViewById(R.id.imageview_meal);

        itemView.setonClickListener(this);
    }

    @Override
    public void onClick(View view) {
        this.animate(this.relativeLayoutExpand);
    }

    public void animate(final View view) {
        ExpandAnimation expand = new ExpandAnimation(this,view,500);

        view.startAnimation(expand);
    }

    public void onAnimationCompleted(int position) {
    }
}

我现在只有一个问题:当我在列表中展开一个项目时,它第一次立即出现而没有动画,之后关闭和打开动画顺利进行.

有人知道为什么第一次打开没有动画但是之后的每次点击都会触发正确的动画吗?

第一次点击我也需要动画.

编辑

是因为最初我的观点可见度是GONE,因此高度未知?如果是:我怎么能解决这个问题?

编辑2

不,我的XML布局文件没有将视图设置为GONE:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/datatransfer_measure_list_row_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:paddingTop="10dp">

            <TextView
                android:id="@+id/datatransfer_measure_list_row_textview_value"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:lines="1"
                android:maxLines="1"
                android:singleLine="true"
                android:text="@string/text_value"
                android:textColor="@color/textcolorprimary"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/datatransfer_measure_list_row_textview_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/datatransfer_measure_list_row_textview_value"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:gravity="start"
                android:lines="1"
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="@color/textcolorprimary"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/datatransfer_measure_list_row_imageview_expand"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                android:contentDescription="@string/text_description"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_expand_more_black_24dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/datatransfer_measure_list_row_expand"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/datatransfer_measure_list_row_data"
            android:background="@color/primaryColor"
            android:paddingBottom="10dp"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:paddingTop="10dp">

            <RelativeLayout
                android:id="@+id/datatransfer_measure_list_row_mood"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:lines="1"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:text="@string/text_mood"
                    android:textColor="@color/textcolorsecundary"
                    android:textSize="30sp" />

                <ImageView
                    android:id="@+id/datatransfer_measure_list_row_imageview_mood"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:contentDescription="@string/text_description"
                    android:scaleType="fitCenter" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/datatransfer_measure_list_row_imageview_meal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/datatransfer_measure_list_row_mood"
                android:layout_centerInParent="true"
                android:contentDescription="@string/text_description" />

        </RelativeLayout>

    </RelativeLayout>

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

我在listadapter中设置了View.GONE:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
    ...

    viewHolder.textViewTime.setText(time);
    viewHolder.textViewValue.setText(value + " " + unitValue);
    viewHolder.relativeLayoutExpand.setVisibility(View.GONE); // <-- HERE

    ...
}

编辑3

也许重要的是要知道我的cardviews的容器是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="match_parent">

    <TextView
        android:id="@+id/datatransfer_measure_list_textview_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/primaryColorDark"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/white"
        android:textSize="18dp" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/datatransfer_measure_list_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/datatransfer_measure_list_textview_name">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/datatransfer_measure_list_swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/datatransfer_measure_list_row_parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:padding="10dp"
                android:textColor="@color/textcolorprimary" />
        </android.support.v4.widget.SwipeRefreshLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/datatransfer_measure_list_floating_action_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="16dp"
            android:clickable="true"
            android:src="@drawable/ic_search_white_24dp"
            app:backgroundTint="@color/primaryColorDark"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:layout_anchor="@id/datatransfer_measure_list_row_parent"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_behavior="de.hof.ime_dc.idia_move.views.buttons.ScrollFABBehavIoUr"
            app:rippleColor="@color/accentColor" />

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

我真的需要一个解决方案.它扰乱了许多所需的功能.

编辑4

我添加了一些日志输出:

=============================== EXPAND =========================================

08-02 12:27:22.471   V/ExpandAnimation﹕ View is visible: 8 (GONE)
08-02 12:27:22.471   V/ExpandAnimation﹕ marginStart: 0
08-02 12:27:22.471   V/ExpandAnimation﹕ marginEnd: 0
08-02 12:27:22.472   V/ExpandAnimation﹕ View is visible: 0 (VISIBLE)

============================== COLLAPSE ========================================

08-02 12:27:51.015   V/ExpandAnimation﹕ View is visible: 0 (VISIBLE)
08-02 12:27:51.015   V/ExpandAnimation﹕ marginStart: 0
08-02 12:27:51.015   V/ExpandAnimation﹕ marginEnd: -156
08-02 12:27:51.015   V/ExpandAnimation﹕ View is visible: 8 (GONE)

=============================== EXPAND =========================================

08-02 12:27:56.007   V/ExpandAnimation﹕ View is visible: 8 (GONE)
08-02 12:27:56.007   V/ExpandAnimation﹕ marginStart: -156
08-02 12:27:56.007   V/ExpandAnimation﹕ marginEnd: 0
08-02 12:27:56.008   V/ExpandAnimation﹕ View is visible: 0 (VISIBLE)

============================== COLLAPSE ========================================

08-02 12:27:51.015   V/ExpandAnimation﹕ View is visible: 0 (VISIBLE)
08-02 12:27:51.015   V/ExpandAnimation﹕ marginStart: 0
08-02 12:27:51.015   V/ExpandAnimation﹕ marginEnd: -156
08-02 12:27:51.015   V/ExpandAnimation﹕ View is visible: 8 (GONE)

=============================== EXPAND =========================================

08-02 12:27:56.007   V/ExpandAnimation﹕ View is visible: 8 (GONE)
08-02 12:27:56.007   V/ExpandAnimation﹕ marginStart: -156
08-02 12:27:56.007   V/ExpandAnimation﹕ marginEnd: 0
08-02 12:27:56.008   V/ExpandAnimation﹕ View is visible: 0 (VISIBLE)

如您所见,展开的第一个marginStart为0,但必须为-156.

解决方法

你没有显示你的XML,但我认为你有android:visibility =“消失了”.如果是这样,您应该将其从布局文件中删除(因此默认情况下它是可见的).如果它应该消失,在通胀后调用setVisibility(View.GONE).这样你将获得相同的结果,但视图也可以获得其尺寸.

android – ListView展开项目动画仅在第二次点击后有效的更多相关文章

  1. ios – 在React Native中设置ListView的高度

    我需要设置ListView的宽度和高度.当设置宽度按预期工作时,设置高度无效,ListView总是伸展到屏幕的几乎底部.我用这种方式在render方法中创建ListView:这是它的风格:我还尝试通过此命令在方法中设置其高度:当我尝试使用此命令设置宽度时,它可以工作.但设定高度什么都不做.如何将ListView的高度设置为所需的值?

  2. swift – 使用“如果让…”与许多表达式

    Swift1.2更新从Swift1.2开始,如果允许允许展开多个可选项,那么现在可以写下这个,如下例所示:您甚至可以交换条件,如:以前在Swift1.2之前有效没有一个丑陋的力量包装,你可以这样做:实际上仍然很冗长这是因为可选类型的表单类型?实际上是可选的的缩写,这是一个大致如下的枚举:然后,您可以使用模式匹配作为任何其他枚举。

  3. android – Airbnb探索屏幕等工具栏中的自定义视图

    我想在应用程序(工具栏)的顶部栏区域添加多个输入字段,以便我的应用程序进行搜索.我看到Airbnb做到了最好!我在CoordinatorLayout中尝试了使用AppBarLayout的各种场景,但都失败了.是否有可能获得相同或类似的效果?

  4. QuickBlox WebRtc VideoChat Android

    几天我正在研究quickblox.i让对手观点在我的观点下面,如this.它工作正常,但是当我保持像skype这样的视图时:–对手视图在全屏幕上,我的视图位于对手视图的右上角,它只渲染一个最终渲染的视图.我在quickblox网站上看了quickbloxwebrtc示例.我看到了该示例中的代码,但它包含了一些会议对话,其中包含一些复杂的循环视图编码,对我来说,单个一对一的谈话是必需的,任何人都可以

  5. Android ListView中的弹出菜单问题

    )方法并把方法在您的适配器类中.注意:act是在创建构造函数适配器时必须绑定的Activity,例如:在Activity中,您可以编写代码:

  6. android – 在listview中添加viewpager作为滚动标题

    我试图在列表标题中添加Viewpager(使用支持库4),但它没有显示任何内容.这是我的代码请帮忙.它将在列表标题中不作为项目,因此它不应该是一个问题.解决方法listadapter之后的listview页眉和页脚显示.如果你尝试setadapter,并且看不到viewpager.检查viewpager的宽度和高度.如果viewpager的宽度或高度值为0.在viewgroup中创建LienarL

  7. android – 如何在AlertDialog中显示CalendarView?

    我正在尝试在警报对话框中显示CalendarView,但所有显示的都是月/年和星期几.这些是布局文件的内容:这是我用于将布局添加到AlertDialog的代码:任何帮助都会非常感激,因为我完全被难倒了.该应用程序没有给我任何错误继续.解决方法要正确显示日历,需要最小高度.其余代码工作正常.

  8. android – Listview滚动错误5.1

    刚注意到ListView中一个非常奇怪的错误,似乎只能重现5.1,我真的很想知道没有人提起它.很容易重现:查找具有足够项目的ListView,滚动到列表的3/4,现在快速向上滚动,您会注意到ListView滚动一直到底部!现在IMO这是一个非常严重的错误,我想尽快找到解决方法/修复,所以任何人都有一个?更新:ListView.java没有从SDK21更改,但AbsListView.java没有.解决方法您可以使用回收站视图而不是列表视图,因为Google可能不再支持它.试试YouTube上的幻灯片视频.它

  9. android – 在ListView中禁用项目焦点

    当我点击它时,如何从ListView中的Item禁用焦点,以便没有关注点击?解决方法以下是禁用ListView焦点的答案.在适配器中覆盖isEnabled方法并返回false.

  10. 无法使用android.support.v7.widget.AppCompatTextView实例化以下类

    最近我在Android工作室的应用程序中将我的sdk从25改为26,我在我的所有xml中都遇到了这个奇怪的错误.目前错误并没有以我能看到的任何方式影响我的应用程序,但是每次我必须编辑或更改xml中的某些内容时都很烦人.这里的错误:无法实例化以下类–android.support.v7.widget.AppCompatTextView我的朋友:编辑:添加了一些我的xml代码解决方法我也收到了这个错误

随机推荐

  1. bluetooth-lowenergy – Altbeacon库无法在Android 5.0上运行

    昨天我在Nexus4上获得了Android5.0的更新,并且altbeacon库停止了检测信标.似乎在监视和测距时,didEnterRegion和didRangeBeaconsInRegion都没有被调用.即使RadiusNetworks的Locate应用程序现在表现不同,一旦检测到信标的值,它们就不再得到更新,并且通常看起来好像信标超出了范围.我注意到的一点是,现在在logcat中出现以下行“B

  2. android – react-native动态更改响应者

    我正在使用react-native进行Android开发.我有一个视图,如果用户长按,我想显示一个可以拖动的动画视图.我可以使用PanResponder实现这一点,它工作正常.但我想要做的是当用户长按时,用户应该能够继续相同的触摸/按下并拖动新显示的Animated.View.如果您熟悉Google云端硬盘应用,则它具有类似的功能.当用户长按列表中的任何项目时,它会显示可拖动的项目.用户可以直接拖

  3. android – 是否有可能通过使用与最初使用的证书不同的证书对其进行签名来发布更新的应用程序

    是否可以通过使用与最初使用的证书不同的证书进行签名来发布Android应用程序的更新?我知道当我们尝试将这样的构建上传到市场时,它通常会给出错误消息.但有没有任何出路,比如将其标记为主要版本,指定市场中的某个地方?解决方法不,你不能这样做.证书是一种工具,可确保您是首次上传应用程序的人.所以总是备份密钥库!

  4. 如何检测Android中是否存在麦克风?

    ..所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.谢谢.解决方法AndroidAPI参考:hasSystemFeature

  5. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

  6. android – 获得一首歌的流派

    我如何阅读与歌曲相关的流派?我可以读这首歌,但是如何抓住这首歌的流派,它存放在哪里?解决方法检查此代码:

  7. android – 使用textShadow折叠工具栏

    我有一个折叠工具栏的问题,在展开状态我想在文本下面有一个模糊的阴影,我使用这段代码:用:我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.是否可以为折叠文本投射阴影?

  8. android – 重用arm共享库

    我已经建立了armarm共享库.我有兴趣重用一个函数.我想调用该函数并获得返回值.有可能做这样的事吗?我没有任何头文件.我试过这个Android.mk,我把libtest.so放在/jni和/libs/armeabi,/lib/armeabi中.此时我的cpp文件编译,但现在是什么?我从objdump知道它的名字编辑:我试图用这个android.mk从hello-jni示例中添加prebuild库:它工作,但libtest.so相同的代码显示以下错误(启动时)libtest.so存在于libhello-j

  9. android – 为NumberPicker捕获键盘’Done’

    我有一个AlertDialog只有一些文本,一个NumberPicker,一个OK和一个取消.(我知道,这个对话框还没有做它应该保留暂停和恢复状态的事情.)我想在软键盘或其他IME上执行“完成”操作来关闭对话框,就像按下了“OK”一样,因为只有一个小部件可以编辑.看起来处理IME“Done”的最佳方法通常是在TextView上使用setonEditorActionListener.但我没有任何Te

  10. android – 想要在调用WebChromeClient#onCreateWindow时知道目标URL

    当我点击一个带有target=“_blank”属性的超链接时,会调用WebChromeClient#onCreateWindow,但我找不到新的窗口将打开的新方法?主页url是我唯一能知道的东西?我想根据目标网址更改应用行为.任何帮助表示赞赏,谢谢!

返回
顶部