Search in sources :

Example 1 with HeaderListView

use of org.qii.weiciyuan.support.lib.HeaderListView in project weiciyuan by qii.

the class Utility method getListViewFirstAdapterItemView.

public static View getListViewFirstAdapterItemView(ListView listView) {
    if (listView instanceof HeaderListView) {
        HeaderListView headerListView = (HeaderListView) listView;
        int childCount = headerListView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = headerListView.getChildAt(i);
            if (!headerListView.isThisViewHeader(childView)) {
                return childView;
            }
        }
        //fallback to first view
        AppLogger.v("all listview children are header view");
        return headerListView.getChildAt(0);
    }
    return listView.getChildAt(0);
}
Also used : HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) ImageView(android.widget.ImageView) HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) ListView(android.widget.ListView)

Example 2 with HeaderListView

use of org.qii.weiciyuan.support.lib.HeaderListView in project weiciyuan by qii.

the class FriendsTimeLineFragment method addNewDataAndRememberPositionAutoRefresh.

private void addNewDataAndRememberPositionAutoRefresh(final MessageListBean newValue) {
    int initSize = getList().getSize();
    if (getActivity() != null && newValue.getSize() > 0) {
        if (hasActionMode()) {
            AppLogger.v("ListView have ActionMode, skip notifyDataSetChanged");
            return;
        }
        final HeaderListView headerListView = (HeaderListView) getListView();
        View firstChildView = getListView().getChildAt(0);
        boolean isFirstViewHeader = headerListView.isThisViewHeader(firstChildView);
        if (isFirstViewHeader && !headerListView.isInTouchByUser()) {
            //                //Overlay is Android 4.3 method
            if (!Utility.isJB2()) {
                getList().addNewData(newValue);
                getAdapter().notifyDataSetChanged();
                Utility.setListViewItemPosition(getListView(), 0, 0, null);
                return;
            }
            //animate add item
            final ListView listView = getListView();
            final ArrayList<Pair<Long, Bitmap>> previousViewsBitmap = new ArrayList<Pair<Long, Bitmap>>();
            final HashMap<Long, Integer> previousViewsTop = new HashMap<Long, Integer>();
            final HashMap<Long, View> previousViews = new HashMap<Long, View>();
            int childCount = listView.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childView = listView.getChildAt(i);
                if (headerListView.isThisViewHeader(childView)) {
                    continue;
                }
                int firstAdapterItemPosition = listView.getFirstVisiblePosition();
                int currentAdapterItemPosition = firstAdapterItemPosition + i - listView.getHeaderViewsCount();
                long currentAdapterItemId = getAdapter().getItemId(currentAdapterItemPosition);
                int childViewTop = childView.getTop();
                Bitmap bitmap = Utility.getBitmapFromView(childView);
                Pair<Long, Bitmap> pair = new Pair<Long, Bitmap>(currentAdapterItemId, bitmap);
                previousViewsBitmap.add(pair);
                previousViewsTop.put(currentAdapterItemId, childViewTop);
                childView.setHasTransientState(true);
                previousViews.put(currentAdapterItemId, childView);
            }
            getList().addNewData(newValue);
            getAdapter().notifyDataSetChanged();
            getListView().getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                    DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
                    final ViewGroupOverlay overlay = listView.getOverlay();
                    Set<Long> previousViewsId = previousViews.keySet();
                    boolean somePreviousViewsAreStillInScreen = false;
                    ArrayList<View> newAddedItemViews = new ArrayList<View>();
                    int deltaY = 0;
                    int childCount = listView.getChildCount();
                    for (int i = 0; i < childCount; i++) {
                        View childView = listView.getChildAt(i);
                        if (headerListView.isThisViewHeader(childView)) {
                            continue;
                        }
                        int firstAdapterItemPosition = listView.getFirstVisiblePosition();
                        int currentAdapterItemPosition = firstAdapterItemPosition + i - listView.getHeaderViewsCount();
                        long currentAdapterItemId = getAdapter().getItemId(currentAdapterItemPosition);
                        if (previousViewsId.contains(currentAdapterItemId)) {
                            somePreviousViewsAreStillInScreen = true;
                            deltaY = childView.getTop() - previousViewsTop.get(currentAdapterItemId);
                        } else {
                            newAddedItemViews.add(childView);
                        }
                    }
                    if (!somePreviousViewsAreStillInScreen) {
                        deltaY = listView.getHeight();
                    }
                    for (View view : newAddedItemViews) {
                        view.setTranslationY(-deltaY);
                        view.animate().translationY(0).setInterpolator(decelerateInterpolator);
                    }
                    for (Pair<Long, Bitmap> pair : previousViewsBitmap) {
                        long id = pair.first;
                        int top = previousViewsTop.get(id);
                        final View view = previousViews.get(id);
                        final Bitmap bitmap = pair.second;
                        final ImageView imageView = new ImageView(getActivity());
                        imageView.setImageBitmap(bitmap);
                        imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                        imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
                        overlay.add(imageView);
                        imageView.setTranslationY(top);
                        view.setAlpha(0);
                        imageView.animate().translationY(top + deltaY).setInterpolator(decelerateInterpolator).withEndAction(new Runnable() {

                            @Override
                            public void run() {
                                overlay.remove(imageView);
                                bitmap.recycle();
                                view.setAlpha(1.0f);
                                view.setHasTransientState(false);
                            }
                        });
                    }
                    return true;
                }
            });
            return;
        }
        getList().addNewData(newValue);
        Runnable endAction = new Runnable() {

            @Override
            public void run() {
                newMsgTipBar.setValue(newValue, false);
                newMsgTipBar.setType(TopTipBar.Type.ALWAYS);
            }
        };
        //keep current read position when user touch listview
        if (isFirstViewHeader && headerListView.isInTouchByUser()) {
            int index = getListView().getFirstVisiblePosition();
            getAdapter().notifyDataSetChanged();
            int finalSize = getList().getSize();
            final int positionAfterRefresh = index + finalSize - initSize + getListView().getHeaderViewsCount();
            //use 1 px to show newMsgTipBar
            Utility.setListViewItemPosition(getListView(), positionAfterRefresh, 1, endAction);
            return;
        }
        //need to calc the first child view's top distance
        int index = getListView().getFirstVisiblePosition();
        View firstAdapterItemView = Utility.getListViewFirstAdapterItemView(getListView());
        final int top = (firstAdapterItemView == null) ? 0 : firstAdapterItemView.getTop();
        getAdapter().notifyDataSetChanged();
        int finalSize = getList().getSize();
        final int positionAfterRefresh = index + finalSize - initSize;
        Utility.setListViewItemPosition(getListView(), positionAfterRefresh, top, endAction);
    }
}
Also used : DecelerateInterpolator(android.view.animation.DecelerateInterpolator) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) Bitmap(android.graphics.Bitmap) ListView(android.widget.ListView) HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) VelocityListView(org.qii.weiciyuan.support.lib.VelocityListView) ImageView(android.widget.ImageView) ViewTreeObserver(android.view.ViewTreeObserver) Pair(android.util.Pair) ViewGroupOverlay(android.view.ViewGroupOverlay) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) VelocityListView(org.qii.weiciyuan.support.lib.VelocityListView) WifiAutoDownloadPictureRunnable(org.qii.weiciyuan.othercomponent.WifiAutoDownloadPictureRunnable)

Aggregations

View (android.view.View)2 ImageView (android.widget.ImageView)2 ListView (android.widget.ListView)2 HeaderListView (org.qii.weiciyuan.support.lib.HeaderListView)2 Bitmap (android.graphics.Bitmap)1 Pair (android.util.Pair)1 ViewGroupOverlay (android.view.ViewGroupOverlay)1 ViewTreeObserver (android.view.ViewTreeObserver)1 DecelerateInterpolator (android.view.animation.DecelerateInterpolator)1 WebView (android.webkit.WebView)1 AdapterView (android.widget.AdapterView)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 WifiAutoDownloadPictureRunnable (org.qii.weiciyuan.othercomponent.WifiAutoDownloadPictureRunnable)1 VelocityListView (org.qii.weiciyuan.support.lib.VelocityListView)1