Skip to content
This repository has been archived by the owner on Jan 8, 2018. It is now read-only.

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

Open
mitrofany4 opened this issue Apr 29, 2014 · 1 comment

Comments

@mitrofany4
Copy link

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
Copy link

ncoolz commented Apr 30, 2014

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.)

  • Make a custom layout like below. �The point of below is that you make mHeaderImage be changed to a different image in refreshingImpl() method.
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();
    }
}
  • Add the custom layout into assets/pulltorefresh.xml
...
<LoadingLayouts>
      ...
     <layout name="custom_drawable">com.example.app.CustomDrawableLoadingLayout</layout>
...
  • Use the custom 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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants