Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView animate rows after a selected row is removed

I have read a lot of tutorials about animation of the rows, but all of them describe how to animate the selected row. I managed to do it. But the there is a problem. When the row is removed with an animation, I remove data from the adapter and call notifyDataSetChanged(); The rows (below the removed row) go up without animation. How can I achieve the animation of these rows? I want them to slide up smoothly.

like image 475
Alexey Avatar asked Nov 04 '22 10:11

Alexey


1 Answers

Remove the list item on item click, Hope this code will helpful for you

 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {
                // TODO Auto-generated method stub
                Animation anim = AnimationUtils.loadAnimation(view.getContext(),
                        android.R.anim.slide_out_right);
                anim.setDuration(500);
                view.startAnimation(anim);

                new Handler().postDelayed(new Runnable() {

                    public void run() {

                        strings.remove(position);
                        mAdapter.notifyDataSetChanged();

                    }

                }, anim.getDuration());

            }
            });

UPDATE

Just keep in mind the architecture framework which works when the notifydatasetChanged() is called.

  1. The getView method is called
  2. On the call of get view, it will reconstruct all the rows for the list view.

In your case, we have to animate the getView method ( which is to be called again on the action of the notifydatasetchanged). Here is the solution :

/**
       * Hear strings is the data set
       */
      @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            final String str = this.strings.get(position);
            final Holder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(
                        android.R.layout.simple_list_item_1, null);
                convertView.setBackgroundColor(0xFF202020);

                holder = new Holder();
                holder.textview = (TextView) convertView
                        .findViewById(android.R.id.text1);
                holder.textview.setTextColor(0xFFFFFFFF);

                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.textview.setText(str);

            Animation animation = null;
            animation = new ScaleAnimation((float) 1.0, (float) 1.0, (float) 0,
                    (float) 1.0);

            animation.setDuration(750);
            convertView.startAnimation(animation);
            animation = null;

            return convertView;
        }

Please check if it works and let me know if it was useful/helpful to you.

like image 173
Ashish Dwivedi Avatar answered Nov 09 '22 12:11

Ashish Dwivedi