Weird animation on Gallery when a invalidate is requested from it's children

后端 未结 6 1733
春和景丽
春和景丽 2021-01-14 17:38

This is the adapter of my Gallery on which I display ImageViews (thumb of a page)

I want to load the images ASync (some times this can come from Network), so I did t

6条回答
  •  耶瑟儿~
    2021-01-14 18:26

    If you still haven't found a solution here is mine.

    You need CustomGallery, which is almost 1:1 default Android Gallery. But you can't extend Gallery, you have to copy whole source code to your class. You will need to do the same with CustomAbsSpinner and CustomAdapterView classes. All this to change only one line of code... In your CustomGallery class change onLayout() method to this:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    
        /*
         * Remember that we are in layout to prevent more layout request from
         * being generated.
         */
    
        mInLayout = true;
    
        /*
         * Add if condition to avoid calling layout()  unnecessarily:
         * -when children calls onLayout after loading imageview
         */
        if(changed || mDataChanged)
            layout(0, false);
        mInLayout = false;
    }
    

    This will cause that layout() is called only when actually needed.

    There was also another known bug in gallery behaviour, which causes gallery jumps on notifyDataSetChanged(). If you are having this bug as well just comment out one line in CustomAdapterView class:

        @Override
        public void onChanged() {
            mDataChanged = true;
            mOldItemCount = mItemCount;
            mItemCount = getAdapter().getCount();
    
            // Detect the case where a cursor that was previously invalidated has
            // been repopulated with new data.
            if (CustomAdapterView.this.getAdapter().hasStableIds() && mInstanceState != null
                    && mOldItemCount == 0 && mItemCount > 0) {
                CustomAdapterView.this.onRestoreInstanceState(mInstanceState);
                mInstanceState = null;
            } else {
                rememberSyncState();
            }
            checkFocus();
            //comment this line to avoid gallery snap on notyfiDataSetChanged 
            //requestLayout();
        }
    

    Anyway, good topic Marcos! Thank you for leading me to the solution. If you need source of the custom classes let me know.

    EDIT (by Marcos Vasconcelos):

    There's a bunch of styleables things that need to be copy in order to get the R fix for those classes.

    For those who are searching the Android 2.2 source code (without res folder, if anyone found it please notify us), this can be found here: http://en.newinstance.it/2010/12/01/android-sdk-2-2_r2-sources/

    For more recent versions of the API this can be done trough SDK Manager and will be located into /sources/android-

提交回复
热议问题