来自 2 个不同输入的自定义动画 (Expand/Collapse)

Custom Animation (Expand/Collapse) from 2 Different Inputs

我是新的 Android 开发人员,我正在制作一个 App,它存储一些 过期日期 并将它们显示在主要活动。

SelectionActivity 上,我有一个自定义适配器来显示一些 EditTexts 和 2 个 Buttons .对于 View.

,我有 onClick ANIMATION 到 Expand/Collapse

问题:因为我使用内部按钮来存储数据并将数据传递给另一个activity,我还需要能够Expand/Collapse 对应的视图 点击 。我怎样才能做到这一点?

我曾尝试在 onBindViewHelper 中使用相同的折叠动画,但效果一般,仅隐藏约束布局而未减小其高度大小。

如果您有任何反馈或需要任何其他 activity,请不要犹豫,尽情享受吧!

FormAdapter

public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ViewHolder> {

public static final String FORM_PREFERENCES = "FormPreferences";
private Context mContext;
private ArrayList<Document> mDocuments;

public FormAdapter(Context mContext, ArrayList<Document> mDocuments) {
    this.mContext = mContext;
    this.mDocuments = mDocuments;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_form, parent, false));
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    final Document document = mDocuments.get(position);
    holder.mTitle.setText(document.getName());
    holder.mTitleExpanded.setText(document.getName());
    holder.mEditText.setTag(R.id.date_et, position);
    holder.mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            int position = (int) holder.mEditText.getTag(R.id.date_et);
            Log.d("pos", String.valueOf(position));

            SharedPreferences mSP = v.getContext().getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor mEditor = mSP.edit();

            String data = holder.mEditText.getText().toString();

            switch (position){
                case 0:

                    Log.d("SOAT", data);
                    mEditor.putString("soat", data);
                    mEditor.commit();

                    break;
                case 1:

                    Log.d("RTM", data);
                    mEditor.putString("rtm", data);
                    mEditor.commit();

                    break;
                case 2:

                    Log.d("SRC", data);
                    mEditor.putString("src", data);
                    mEditor.commit();

                    break;
                case 3:

                    Log.d("STR", data);
                    mEditor.putString("str", data);
                    mEditor.commit();

                    break;
                case 4:

                    Log.d("TO", data);
                    mEditor.putString("to", data);
                    mEditor.commit();

                    break;
            }
        }
    });
}

@Override
public int getItemCount() {
    return mDocuments != null ? mDocuments.size(): 0;
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView mTitle, mTitleExpanded;
    public Button mButton;
    public EditText mEditText;
    public ImageView mImageView;

    public int originalHeight = 0;
    public boolean isViewExpanded = false;
    public ConstraintLayout mConstraintLayout;
    private DateWatcher mDateWatcher;

    public ViewHolder(final View view) {
        super(view);
        view.setOnClickListener(this);

        mConstraintLayout = (ConstraintLayout) view.findViewById(R.id.expanded);
        mTitle = (TextView) view.findViewById(R.id.name_title_tv);
        mTitleExpanded = (TextView) view.findViewById(R.id.name_title_tv_expanded);
        mButton = (Button) view.findViewById(R.id.add_button);
        mEditText = (EditText) view.findViewById(R.id.date_et);
        mImageView = (ImageView) view.findViewById(R.id.imageView_up);

        mDateWatcher = new DateWatcher(mEditText);
        mEditText.addTextChangedListener(mDateWatcher);


        if (isViewExpanded == false) {
            // Set Views to View.GONE and .setEnabled(false)
            mConstraintLayout.setVisibility(View.GONE);
            mConstraintLayout.setEnabled(false);
        }
    }


    @Override
    public void onClick(final View v) {

        // If the originalHeight is 0 then find the height of the View being used
        // This would be the height of the ConstraintLayout
        if (originalHeight == 0) {
            originalHeight = v.getHeight();
        }

        // Declare a ValueAnimator object
        ValueAnimator valueAnimator;
        if (!isViewExpanded) {
            mTitle.setVisibility(View.GONE);
            mTitle.setEnabled(false);
            mImageView.setVisibility(View.GONE);
            mConstraintLayout.setVisibility(View.VISIBLE);
            mConstraintLayout.setEnabled(true);
            isViewExpanded = true;
            valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight)); // These values in this method can be changed to expand however much you like
        } else {
            isViewExpanded = false;
            valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight), originalHeight);

            Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out

            a.setDuration(200);
            // Set a listener to the animation and configure onAnimationEnd
            a.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mTitle.setVisibility(View.VISIBLE);
                    mTitle.setEnabled(true);
                    mImageView.setVisibility(View.VISIBLE);
                    mConstraintLayout.setVisibility(View.GONE);
                    mConstraintLayout.setEnabled(false);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            // Set the animation on the custom view
            mConstraintLayout.startAnimation(a);
        }
        valueAnimator.setDuration(200);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                v.getLayoutParams().height = value.intValue();
                v.requestLayout();
            }
        });
        valueAnimator.start();

    }
}
}

FormActivity.xml

<android.support.v7.widget.CardView
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:id="@+id/card_view_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_margin="5dp">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraint_layout">


    <TextView
        android:id="@+id/name_title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"
        android:text="Revisión Técnico Mecánica"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/imageView_up"
        android:layout_width="37dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:rotation="180"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/arrow_black_down"
        app:layout_constraintLeft_toRightOf="@+id/name_title_tv"
        android:layout_marginLeft="8dp"
        app:layout_constraintHorizontal_bias="1.0"/>

    <include layout="@layout/format_form_expanded"/>

</android.support.constraint.ConstraintLayout>

FormActivity

public class Form extends AppCompatActivity {

private DBHelper mDBHelper;
private Context mContext;
private RecyclerView mRecyclerView;
private ArrayList<Document> mArrayList;
private FormAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);

    mDBHelper = new DBHelper(this);

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setContentView(R.layout.hint);
    dialog.setCanceledOnTouchOutside(true);
    //for dismissing anywhere you touch
    View masterView = dialog.findViewById(R.id.hint);
    masterView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    dialog.show();

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_form);

    mArrayList = new ArrayList<>();
    mArrayList.add(new Document(R.string.doc_soat, 0));
    mArrayList.add(new Document(R.string.doc_rtm, 1));
    mArrayList.add(new Document(R.string.doc_src, 2));
    mArrayList.add(new Document(R.string.doc_str, 3));
    mArrayList.add(new Document(R.string.doc_tao, 4));
    mArrayList.add(new Document(R.string.doc_ext, 5));
    mAdapter = new FormAdapter(mContext, mArrayList);

    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setHasFixedSize(true);

    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

在对整个逻辑进行一些挖掘和演练之后,我得到了这个样板答案:

  • 您需要在 ViewHolder 中声明 Viewgroup 才能在 onBindViewHolder
  • 中接收它
  • 传到需要的高度
  • 必须在视图中清楚animationUpdateListener

像这些一样,您将获得视图和按钮的 onClick 动画

    holder.mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {

            if (holder.originalHeight == 0) {
                holder.originalHeight = holder.mCardView.getHeight();
            }

            holder.isViewExpanded = false;
            ValueAnimator valueAnimator;

            valueAnimator = ValueAnimator.ofInt(holder.originalHeight + (int) (holder.originalHeight), holder.originalHeight);

            Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out

            a.setDuration(200);
            // Set a listener to the animation and configure onAnimationEnd
            a.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    holder.mConstraintLayout.setVisibility(View.GONE);
                    holder.mConstraintLayout.setEnabled(false);
                    holder. mTitle.setVisibility(View.VISIBLE);
                    holder.mTitle.setEnabled(true);
                    holder. mImageView.setVisibility(View.VISIBLE);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            // Set the animation on the custom view
            holder.mConstraintLayout.startAnimation(a);

            valueAnimator.setDuration(200);
            valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    holder.mCardView.getLayoutParams().height = value.intValue();
                    holder.mCardView.requestLayout();
                }
            });
            valueAnimator.start();


        }
    });