set animation on listview scrolling in android

2019-04-02 16:19发布

问题:

I want to set an animation for a ListView for when a user scrolls the ListView. I am setting an animation for ListView when loading, but I want to set an animation for scrolling.

This is my animation file in eclipse :

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="schemas.android.com/apk/res/android";   
    android:duration="900" android:fromXDelta="-100%p" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:repeatCount="0" android:repeatMode="reverse" 
    android:fillEnabled="true" android:fillAfter="true" android:toXDelta="0%p" >
</translate>

this code is set items to my listview :

@Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    StructNote item = getItem(position);
    if (convertView == null) {
        convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.fill(this, item, position); return convertView;
}

and this is a ActivityMain that set animation to listview

ListView lstContent = (ListView) findViewById(R.id.lstContent);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);    
lstContent.setLayoutAnimation(new LayoutAnimationController(animation));

Any one can help me??

回答1:

ListView Adapter:

Animation scaleUp;

 public MyAdapter(...) {
           //...
     scaleUp = AnimationUtils.loadAnimation(activity, R.anim.scale_up_fast);
 }

 public View getView(final int position, View convertView, ViewGroup parent) {

    CardView cv;

    if (convertView == null){
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.your_layout, parent, false);
    }
    cv = (CardView) convertView.findById(R.id.yourView);//Change this to your view      
    cv.startAnimation(scaleUp);
 }

Animation:

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

    <scale
        android:duration="300"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />
</set>


回答2:

There is lot of way to do this kind of task i'm suggesting you known example, you can create your own animation as well use pre define library for this. example 1,Some listview which have effects extra than the listview widget



回答3:

well first of all remove semicolon(;) from animation xml then I have see scroll animation like this app https://play.google.com/store/apps/details?id=com.nilstechsys.myquotes

and this is a simple code of scroll animation in listview adpter

 Animation animation = null;
        animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0, 0);
        //animation = AnimationUtils.loadAnimation(activity, R.anim.your_animation);
        animation.setDuration(400);
        convertView.startAnimation(animation);
        animation = null;


回答4:

This is the best possible solution that i can give to you as per your requirement! Please go through this Adapter once.

 public class MyAdapter extends ArrayAdapter<String>{

            private int mLastPosition;

            public MyAdapter(Context context, ArrayList<String> objects) {
                super(context, 0, objects);
            }

            private class ViewHolder{
                public TextView mTextView;
            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ViewHolder holder;

                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
                    holder.mTextView = (TextView) convertView.findViewById(R.id.checkbox);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.mTextView.setText(getItem(position));

                // This tells the view where to start based on the direction of the scroll.
                // If the last position to be loaded is <= the current position, we want
                // the views to start below their ending point (500f further down).
                // Otherwise, we start above the ending point.
                float initialTranslation = (mLastPosition <= position ? 500f : -500f);

                convertView.setTranslationY(initialTranslation);
                convertView.animate()
                        .setInterpolator(new DecelerateInterpolator(1.0f))
                        .translationY(0f)
                        .setDuration(300l)
                        .setListener(null);

                // Keep track of the last position we loaded
                mLastPosition = position;

                return convertView;
            }


        }


回答5:

Simple and fabulous animation on listview scrolling

 @Override`
public View getView(final int i, View view, ViewGroup viewGroup) {
    View converview = view;`
// other coding related to view

    if (animateOrNot) {    //boolean if want to animate
        doCards(converview, i, prevPos);}
       `prevPos=i; //take prevPos as global variable of int
    return converview;
}`



    public static void doCards(View view, int position, int prevPosition) {
    view.setScaleX(0.5f);
    if (position > prevPosition) {
        view.setRotationX(90);
    } else {
        view.setRotationX(-90);
    }
    view.animate().rotationX(0).scaleX(1.0f).start();
}