使用Android中的onOverScrolled获取Bounce效果

时间:2017-01-17 11:29:12

标签: android android-studio android-fragments

我有一个应用程序需要在滚动时获得反弹效果。我被告知我需要使用onOverScrolled()方法。我想知道热使用它和在哪里。谢谢:))

1 个答案:

答案 0 :(得分:0)

弹跳动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator" >

<scale
   android:duration="600"
   android:fromXScale="1"
   android:fromYScale="0.5"
   android:pivotX="50%"
   android:pivotY="0%"
   android:toXScale="1.0"
   android:toYScale="1.0" />

<alpha
   android:duration="600"
   android:fromAlpha="0.0"
   android:toAlpha="1.0" />

</set>

适配器代码

public class Adapter_Event_Profile extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private int lastPosition = -1;

public Adapter_Event_Profile(Context _context) {
    mContext = _context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_event_profile, parent, false);
    return new ProfileEventHolder(v);
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    setAnimation(((ProfileEventHolder) holder).cardView, position);
}

@Override
public int getItemCount() {
    return 15;
}

public class ProfileEventHolder extends RecyclerView.ViewHolder {
    public CardView cardView;

    public ProfileEventHolder(View itemView) {
        super(itemView);
        cardView = (CardView) itemView.findViewById(R.id.cardView);
    }
}

private void setAnimation(View viewToAnimate, int position) {
    // If the bound view wasn't previously displayed on screen, it's animated
    if (position > lastPosition) {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.bounce_animation);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
  }
}

RecyclerView项目

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/scale_5dp"
app:cardElevation="2dp"
card_view:cardCornerRadius="8dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="@dimen/scale_10dp">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/ivUserImage"
        android:layout_width="@dimen/scale_80dp"
        android:layout_height="@dimen/scale_80dp"
        android:src="@drawable/user" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginEnd="@dimen/scale_15dp"
        android:layout_marginStart="@dimen/scale_15dp"
        android:gravity="center_vertical"
        android:orientation="vertical">


        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Lets_party"
            android:textSize="17sp"
            android:textStyle="bold" />

        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/scale_5dp"
            android:text="@string/creat"
            android:textSize="14sp" />

        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/scale_5dp"
            android:drawableLeft="@drawable/ic_access_time"
            android:drawablePadding="5dp"
            android:gravity="center_vertical"
            android:text="@string/datetime"
            android:textSize="14sp" />

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

滚动RecyclerView时,会发生Bounce Animation。