1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView;
import androidx.annotation.ColorInt; import androidx.annotation.NonNull;
import com.scwang.smartrefresh.layout.api.RefreshHeader; import com.scwang.smartrefresh.layout.api.RefreshKernel; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.constant.RefreshState; import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import org.libpag.PAGFile; import org.libpag.PAGView;
public class CommonRefreshHeader extends RelativeLayout implements RefreshHeader { public static final String DEFAULT_LOADING_FILE = "load_bubble.pag";
protected View mView; protected ImageView sdv_background; private int mFinishDuration = 300; private ViewGroup rootLayout; private PAGView pagView; private TextView toastTv;
public CommonRefreshHeader(Context context) { super(context); initView(context); }
public CommonRefreshHeader(Context context, AttributeSet attrs) { super(context, attrs); initView(context); }
public CommonRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); }
public int getLayoutId() { return R.layout.layout_refresh_head; }
protected void initView(Context context) { mView = LayoutInflater.from(context).inflate(getLayoutId(), this); toastTv = mView.findViewById(R.id.tv_toast); rootLayout = mView.findViewById(R.id.rootLayout); sdv_background = mView.findViewById(R.id.sdv_background); PAGFile file = PAGFile.Load(context.getAssets(), DEFAULT_LOADING_FILE); pagView = new PAGView(getContext()); LayoutParams params = new LayoutParams(UiUtils.dip2px(39), UiUtils.dip2px(39)); params.addRule(RelativeLayout.CENTER_IN_PARENT); pagView.setLayoutParams(params); pagView.setFile(file); pagView.setRepeatCount(0); rootLayout.addView(pagView); }
public void setMarginTop(int marginTop) { if (mView == null) return; ViewGroup root = mView.findViewById(R.id.rootLayout); if (root != null && root.getLayoutParams() instanceof MarginLayoutParams) { MarginLayoutParams params = (MarginLayoutParams) root.getLayoutParams(); params.topMargin = marginTop; root.setLayoutParams(params); } }
@NonNull @Override public View getView() { return mView; }
@Override public SpinnerStyle getSpinnerStyle() { return SpinnerStyle.Translate; }
@Override public void setPrimaryColors(@ColorInt int... colors) {
}
@Override public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
}
@Override public void onMoving(boolean b, float v, int i, int i1, int i2) {
if (pagView != null && !pagView.isPlaying()) { pagView.setProgress(v); pagView.flush(); } }
@Override public void onReleased(@NonNull RefreshLayout refreshLayout, int i, int i1) {
}
@Override public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) { LogUtils.e("RefreshHeader", "onStartAnimator"); if (pagView != null) pagView.play(); }
@Override public int onFinish(RefreshLayout layout, boolean success) { LogUtils.e("RefreshHeader", "onFinish"); if (pagView != null) pagView.stop(); return mFinishDuration; }
@Override public boolean isSupportHorizontalDrag() { return false; }
@Override public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) { switch (newState) { case None: case PullDownToRefresh: case Refreshing: if (pagView != null) pagView.setVisibility(VISIBLE); if (toastTv != null) toastTv.setVisibility(GONE); break; case ReleaseToRefresh: break; case RefreshFinish: if (pagView != null) pagView.setVisibility(GONE); if (toastTv != null) toastTv.setVisibility(VISIBLE); break; } }
public void setToastText(String str) { if (StringUtils.isEmpty(str)) return; if (toastTv != null) { toastTv.setText(str); } }
public void setFinishDuration(int finishDuration) { this.mFinishDuration = finishDuration; } }
|