naver / android-pull-to-refresh

Pull To Refresh Views for Android! v3.2.3
Apache License 2.0
311 stars 82 forks source link

Can I use different drawables for loading state and pull/refresh state in the flip animation? #10

Open mitrofany4 opened 10 years ago

mitrofany4 commented 10 years ago

I need to use f.e. indicator arrow drawable for pull/refresh animation and custom loading spinner for loading.

If I use "ptrDrawable" in the layout or setLoadingDrawable animation works like rotate. If I use getDefaultDrawableResId() in the LoadingLayout situation is the same.

ncoolz commented 10 years ago

ptrDrawable or setLoadingDrawable, getDefaultDrawableResId() are used to set an image which is shown except when a current state is "refreshing". There is no feature to support to set an image for a refreshing state yet. And, the rotating circle when refreshing is not an image but that is a progress bar.

But there is an other way to set it. Below is the way. (I have to say that this is not the best way.)

public class CustomDrawableLoadingLayout extends FlipLoadingLayout {

    private final int refreshingHeaderImageResource = R.drawable.heart;

    private Drawable mLoadingDrawable;
    /**
     * Default Constructor (Parameters must not be omitted in constructor).
     */
    public CustomDrawableLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
        super(context, mode, scrollDirection, attrs);
    }

    @Override
    protected void onLoadingDrawableSet(Drawable imageDrawable) {
        super.onLoadingDrawableSet(imageDrawable);
        // Save the current imageDrawable
        mLoadingDrawable = imageDrawable;
    }

    /**
     * Customize some part of the layout when refreshing
     */
    @Override
    protected void refreshingImpl() {
        super.refreshingImpl();
        // Let's start to change a header image.
        updateRefreshingHeaderImage();

    }

    private void updateRefreshingHeaderImage() {
        // Header image is set to be hidden in super class. So, we have to set it back.
        updateHeaderImageVisible();
        // Here is the most important code. Set other image you want to change.
        mHeaderImage.setImageResource(refreshingHeaderImageResource);
    }

    private void updateHeaderImageVisible() {
        mHeaderImage.setVisibility(View.VISIBLE);
        mHeaderProgress.setVisibility(View.GONE);
    }

    @Override
    protected void resetImpl() {
        super.refreshingImpl();
        // Reset header image
        resetRefreshingHeaderImage();
    }

    private void resetRefreshingHeaderImage() {
        mHeaderImage.setImageDrawable(mLoadingDrawable);
        updateHeaderImageVisible();
    }
}
...
<LoadingLayouts>
      ...
     <layout name="custom_drawable">com.example.app.CustomDrawableLoadingLayout</layout>
...
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        ...
        ptr:ptrAnimationStyle="custom_drawable"
        />

This is done. You may also set some custom animation to mHeaderImage(But a previous animation of mHeaderImage must be restored when reset). The more clearer way to make custom animations of icons is that you override LoadingLayout instead of FlipLoadingLayout or RotateLoadingLayout. You can make a custom loading layout by referring to the source of FlipLoadingLayout.

ps. I think that it's hard to customize LoadingLayout. I have a plan refactoring totally a structure of LoadingLayout(but not this time, because of backward compatibility).