Lập trình android: Hiệu ứng khi xóa một item ra khỏi listview

Xin chào mọi người với bài viết này mình sẽ chia sẻ làm sao để thêm hiệu ứng khi xóa một item ra khỏi listview trong lập trình ứng dụng android. Với việc thêm hiệu ứng này sẽ giúp các Bạn hay người dùng sử dụng apps của các Bạn làm ra mà có sử dụng việc thêm hoặc xóa các item trong listview trở nên mượt mà và bắt mắt hơn. Với code này Tú chỉ chia sẻ code chú ý nhất đó là hiệu ứng và xóa một item ra khỏi listview, chứ không phải toàn bộ soure code demo. Và Tú sẽ giải thích từng dòng code một cho các Bạn hiểu rõ hơn. Để biết về demo các Bạn có thể coi video demo sau. [embed]https://www.youtube.com/watch?v=h5vcGei8FUQ[/embed] Còn đây sẽ là code của Mình. Các Bạn coppy vào sẽ bị lỗi và khắc phục lỗi đó thì dễ thôi. cũng không khó lắm đâu. Và đây chỉ là code hiệu ứng và xóa một item chứ không có sự kiện vuốt để xóa như video trên các Bạn nhé.

HashMap<Long, Integer> mItemIdTopMap = new HashMap<Long, Integer>();
private void removeRowItem(View viewToRemove) {
    int firstVisiblePosition = mListView.getFirstVisiblePosition();
    // Save top position of all visible rows in list view with row id
    for (int i = 0; i < mListView.getChildCount(); ++i) {
        View child = mListView.getChildAt(i);
        if (child != viewToRemove) {
            int position = firstVisiblePosition + i;
            long itemId = mAdapter.getItemId(position);
            mItemIdTopMap.put(itemId, child.getTop());
        }
    }
    // Delete the item from the adapter. If view to remove is first view, the second view will
    // be top, the position of second view now is 0, but the row id is still 1 as before remove
    int position = mListView.getPositionForView(viewToRemove);
    mAdapter.remove(mAdapter.getItem(position));
    // Returns the ViewTreeObserver for this view's hierarchy. The view tree
    // observer can be used to get notifications when global events, like: layout, happen.
    final ViewTreeObserver observer = mListView.getViewTreeObserver();
    final AccelerateInterpolator interpolator = new AccelerateInterpolator(0.1f);
    // Register a callback to be called when the view tree is about to be drawn
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            observer.removeOnPreDrawListener(this);
            boolean firstAnimation = true;
            int firstVisiblePosition = mListView.getFirstVisiblePosition();
            for (int i = 0; i < mListView.getChildCount(); ++i) {
                final View child = mListView.getChildAt(i);
                int position = firstVisiblePosition + i;
                // if row to remove is first row, then the item id of position 0 is 1
                long itemId = mAdapter.getItemId(position);
                // start top is position of view before Adapter remove item
                Integer startTop = mItemIdTopMap.get(itemId);
                // top is the current position of view after remove
                int top = child.getTop();
                // Remove first row
                if (startTop != null) {
                    // Now the start top and top of second view (which is next to removed view)
                    // must be different AFTER adapter removed view
                    if (startTop != top) {
                        // delta is distance
                        // start top is position on screen of second view BEFORE adapter remove view
                        // top is position on screen of second view after Adapter removed view
                        int delta = startTop - top;
                        // Move the child back to a distance
                        child.setTranslationY(delta);
                        // Then animate it to 0 position on the screen
                        child.animate()
                                .setInterpolator(interpolator)
                                .setDuration(MOVE_DURATION)
                                .translationY(0);
                        if (firstAnimation) {
                            child.animate().withEndAction(new Runnable() {
                                public void run() {
                                    mListView.setEnabled(true);
                                }
                            });
                            firstAnimation = false;
                        }
                    }
                } else {
                    // Remove other rows exclude first row
                    // Animate new views along with the others. The catch is that they did not
                    // exist in the start state, so we must calculate their starting position
                    // based on neighboring views.
                    int childHeight = child.getHeight() + mListView.getDividerHeight();
                    startTop = top + (i > 0 ? childHeight : -childHeight);
                    int delta = startTop - top;
                    child.setTranslationY(delta);
                    child.animate()
                            .setInterpolator(interpolator)
                            .setDuration(MOVE_DURATION)
                            .translationY(0);
                    if (firstAnimation) {
                        child.animate().withEndAction(new Runnable() {
                            public void run() {
                                mListView.setEnabled(true);
                            }
                        });
                        firstAnimation = false;
                    }
                }
            }
            mItemIdTopMap.clear();
            return true;
        }
    });
}

 

Ở đây đang chú ý 2 dòng code sau các Bạn cần phải nắm rõ. nó có tác dụng xóa đi 1 item trong adapter


 // Delete the item from the adapter
        int position = mListView.getPositionForView(viewToRemove);
        mAdapter.remove(mAdapter.getItem(position));


Comments

Popular posts from this blog

Cách custom bo tròn cạnh imageView trong android

Tối ưu ListView với Viewholder trong lập trình ứng dụng android.