片段

时间:2017-09-13 06:02:10

标签: java android android-fragments android-animation objectanimator

我希望addressCard向下滚动到另一个FirstRowCard后面,这两个都是片段。我有addressCard出现在另一个后面,但它没有向下滚动。我意识到我必须使用Object Animator并在参数中包含View

我最初使用的是:

View addressCard = (View) getView().findViewById(R.id.years_of_cooking_xp_cardView);
    ObjectAnimator addressCard_Animator = ObjectAnimator.ofFloat(addressCard, "translationY", 0f, 258f);

addressCard只显示没有动画。一般体系结构Main Activity创建一个名为topRow_fragment的片段。在topRow_fragment我创建了一个界面,因此当用户点击某个按钮时,它会创建显示在addressCard下方的topRow_fragment片段。当用户单击该按钮时,其他片段消失,这可以正常工作。我已经评论了一些我打算实现的部分,但是在初始动画完成之前,我会忽略它们。

我认为我没有正确识别与View相关联的addressCard,因此无法显示动画。任何帮助都会非常感谢,

这是班级:

public class topRow_fragment extends Fragment implements View.OnClickListener{

addressCard_fragment addressCard;
onTopLeftClicked mCallback;
int addressCardLaunch = 0;
String NextAddressCardAnimation = "SLIDE DOWN";
View layoutReturn;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    layoutReturn = inflater.inflate(R.layout.firstrow_fragment, container, false);
    ImageButton top_row_left_button = (ImageButton) layoutReturn.findViewById(R.id.imageLeft);
    top_row_left_button.setOnClickListener(this);
    return layoutReturn;
}

@Override
public void onClick(View v) {

    addressCard = new addressCard_fragment();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id. Below_top_row_fragment_container, addressCard);

    View addressCard = (View) getView().findViewById(R.id.years_of_cooking_xp_cardView);
    ObjectAnimator addressCard_Animator = ObjectAnimator.ofFloat(addressCard, "translationY", 0f, 258f);

    //CardView addressCard_View = (CardView) getView().findViewById(R.id.years_of_cooking_xp_cardView);
    //ObjectAnimator addressCard_Animator = ObjectAnimator.ofFloat(addressCard_View, "translationY", 0f, 258f);

    switch(v.getId()) {
        case R.id.imageLeft:
            if (addressCardLaunch == 0) {
                fragmentTransaction.commit();
                moveSecondRowAfterClick(0);
                addressCard_Animator.setDuration(250);
                addressCard_Animator.start();
                moveSecondRowAfterClick(0);
                addressCardLaunch = 1;
            }

            else if (addressCardLaunch == 1) {
                switch (NextAddressCardAnimation) {

                    case "SLIDE DOWN":
                        NextAddressCardAnimation = "SLIDE UP";
                        moveSecondRowAfterClick(1);
                        //addressCard_Animator.setInterpolator(new SlideDownAnimationInterpolator());
                        //addressCard_Animator.setDuration(250);
                        //addressCard_Animator.start();
                        break;

                    case "SLIDE UP":
                        NextAddressCardAnimation = "SLIDE DOWN";
                        moveSecondRowAfterClick(0);
                        //addressCard_Animator.setDuration(250);
                        //addressCard_Animator.start();
                        break;
                }
            }
    }
}

public interface onTopLeftClicked {
    void moveSecondRow(int slidingAnimationStatus);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity a;
    if (context instanceof Activity) {
        a = (Activity) context;

        try {
            mCallback = (onTopLeftClicked) a;
        }
        catch (ClassCastException e) {
            throw new ClassCastException(a.toString() + " must implement onTopLeftClicked Interface");
        }
    }
}

@Override
public void onDetach() {
    mCallback = null;
    super.onDetach();
}

public void moveSecondRowAfterClick(int slidingAnimationStatus) {
    mCallback.moveSecondRow(slidingAnimationStatus);
}}

Update01: 我在片段类,slideUpAnimation和slideDownAnimation中包含了方法,这解决了我所拥有的NullPointerException。然而,要在初始化或后续按钮点击时成功动画addressCard。这种方法更符合封装,而不是调用topRow_fragment类中的动画。我想知道这是否现在可以与我在相应的XML文件中放置在CardView上的视图约束相关。

调用片段的主要活动XML,topRow_fragmentaddressCard

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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="com.example.bryanjordan.settingsr11.MainActivity">

<android.support.constraint.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="8dp"
        android:id="@+id/constraintLayout"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent">

        <RelativeLayout
            android:id="@+id/Below_top_row_fragment_container"
            android:layout_width="367dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            tools:layout_editor_absoluteY="16dp">

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Above_top_row_fragment_container"
            android:layout_width="367dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="1dp">
        </RelativeLayout>


    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent">

        <RelativeLayout
            android:id="@+id/second_row_fragment_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_editor_absoluteY="25dp"></RelativeLayout>
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Update02:我已经了解了slideDownAnimationslideUpAnimation是如何工作的,这些都被正确调用,但View只是没有移动。这显然是关于赋值的问题 - 我认为它源于片段的调用方式。

首先,当用户点击按钮topRow_fragment时,会调用below_topRow_fragment。我一直在寻找使用:

View layoutTest = layoutReturn.findViewById(R.id.Below_top_row_fragment_container);
            View addressTestR2 = (View) layoutTest.findViewById(R.id.years_of_cooking_xp_cardView);

出现NullPointerException错误,但这似乎是正确的方向,因为我必须调用相应的子片段才能找到我想要的子片段然后分配它。

0 个答案:

没有答案