set animation on listview scrolling in android

后端 未结 5 1442
温柔的废话
温柔的废话 2021-01-01 08:20

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, b

相关标签:
5条回答
  • 2021-01-01 08:38

    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>
    
    0 讨论(0)
  • 2021-01-01 08:38

    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();
    }
    
    0 讨论(0)
  • 2021-01-01 08:51

    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

    0 讨论(0)
  • 2021-01-01 08:54

    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;
                }
    
    
            }
    
    0 讨论(0)
  • 2021-01-01 08:55

    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;
    
    0 讨论(0)
提交回复
热议问题