下拉样式修改

This commit is contained in:
18401019693 2022-09-02 16:30:16 +08:00
parent 4ff3ae3269
commit b5cfb5abc1
4 changed files with 353 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import com.yunbao.common.R;
import com.yunbao.common.adapter.RefreshAdapter;
import com.yunbao.common.http.HttpCallback;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.views.weight.ClassicsHeaderNew;
import java.util.List;
@ -38,7 +39,7 @@ public class CommonRefreshView extends FrameLayout implements View.OnClickListen
private int mLayoutRes;
private View mContentView;
private SmartRefreshLayout mSmartRefreshLayout;
private ClassicsHeader mHeader;
private ClassicsHeaderNew mHeader;
private ClassicsFooter mFooter;
public RecyclerView mRecyclerView;
private FrameLayout mEmptyLayout;//没有数据的View

View File

@ -0,0 +1,349 @@
package com.yunbao.common.views.weight;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.internal.ArrowDrawable;
import com.scwang.smartrefresh.layout.internal.InternalClassics;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.yunbao.common.R;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
/**
* 新下拉头部
*/
public class ClassicsHeaderNew extends InternalClassics<ClassicsHeaderNew> implements RefreshHeader {
public static final byte ID_TEXT_UPDATE = 4;
public static String REFRESH_HEADER_PULLING = null;//"下拉可以刷新";
public static String REFRESH_HEADER_REFRESHING = null;//"正在刷新...";
public static String REFRESH_HEADER_LOADING = null;//"正在加载...";
public static String REFRESH_HEADER_RELEASE = null;//"释放立即刷新";
public static String REFRESH_HEADER_FINISH = null;//"刷新完成";
public static String REFRESH_HEADER_FAILED = null;//"刷新失败";
public static String REFRESH_HEADER_UPDATE = null;//"上次更新 M-d HH:mm";
public static String REFRESH_HEADER_SECONDARY = null;//"释放进入二楼";
// public static String REFRESH_HEADER_UPDATE = "'Last update' M-d HH:mm";
protected String KEY_LAST_UPDATE_TIME = "LAST_UPDATE_TIME";
protected Date mLastTime;
protected TextView mLastUpdateText;
protected SharedPreferences mShared;
protected DateFormat mLastUpdateFormat;
protected boolean mEnableLastTime = true;
protected String mTextPulling = null;//"下拉可以刷新";
protected String mTextRefreshing = null;//"正在刷新...";
protected String mTextLoading = null;//"正在加载...";
protected String mTextRelease = null;//"释放立即刷新";
protected String mTextFinish = null;//"刷新完成";
protected String mTextFailed = null;//"刷新失败";
protected String mTextUpdate = null;//"上次更新 M-d HH:mm";
protected String mTextSecondary = null;//"释放进入二楼";
//<editor-fold desc="RelativeLayout">
public ClassicsHeaderNew(Context context) {
this(context, null);
}
public ClassicsHeaderNew(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ClassicsHeaderNew(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mLastUpdateText = new TextView(context);
mLastUpdateText.setTextColor(0xff7c7c7c);
final View thisView = this;
final View arrowView = mArrowView;
final View updateView = mLastUpdateText;
final View progressView = mProgressView;
final ViewGroup centerLayout = mCenterLayout;
final DensityUtil density = new DensityUtil();
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ClassicsHeader);
LayoutParams lpArrow = (LayoutParams) arrowView.getLayoutParams();
LayoutParams lpProgress = (LayoutParams) progressView.getLayoutParams();
LinearLayout.LayoutParams lpUpdateText = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
lpUpdateText.topMargin = ta.getDimensionPixelSize(R.styleable.ClassicsHeader_srlTextTimeMarginTop, density.dip2px(0));
lpProgress.rightMargin = ta.getDimensionPixelSize(R.styleable.ClassicsFooter_srlDrawableMarginRight, density.dip2px(20));
lpArrow.rightMargin = lpProgress.rightMargin;
lpArrow.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableArrowSize, lpArrow.width);
lpArrow.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableArrowSize, lpArrow.height);
lpProgress.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableProgressSize, lpProgress.width);
lpProgress.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableProgressSize, lpProgress.height);
lpArrow.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpArrow.width);
lpArrow.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpArrow.height);
lpProgress.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpProgress.width);
lpProgress.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpProgress.height);
mFinishDuration = ta.getInt(R.styleable.ClassicsHeader_srlFinishDuration, mFinishDuration);
mEnableLastTime = ta.getBoolean(R.styleable.ClassicsHeader_srlEnableLastTime, mEnableLastTime);
mSpinnerStyle = SpinnerStyle.values()[ta.getInt(R.styleable.ClassicsHeader_srlClassicsSpinnerStyle, mSpinnerStyle.ordinal())];
if (ta.hasValue(R.styleable.ClassicsHeader_srlDrawableArrow)) {
mArrowView.setImageDrawable(ta.getDrawable(R.styleable.ClassicsHeader_srlDrawableArrow));
} else {
mArrowDrawable = new ArrowDrawable();
mArrowDrawable.setColor(0xff666666);
mArrowView.setImageDrawable(mArrowDrawable);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlDrawableProgress)) {
mProgressView.setImageDrawable(ta.getDrawable(R.styleable.ClassicsHeader_srlDrawableProgress));
} else {
mProgressDrawable = new ProgressDrawable();
mProgressDrawable.setColor(0xff666666);
mProgressView.setImageDrawable(mProgressDrawable);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextSizeTitle)) {
mTitleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, ta.getDimensionPixelSize(R.styleable.ClassicsHeader_srlTextSizeTitle, DensityUtil.dp2px(16)));
} else {
mTitleText.setTextSize(16);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextSizeTime)) {
mLastUpdateText.setTextSize(TypedValue.COMPLEX_UNIT_PX, ta.getDimensionPixelSize(R.styleable.ClassicsHeader_srlTextSizeTime, DensityUtil.dp2px(12)));
} else {
mLastUpdateText.setTextSize(12);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlPrimaryColor)) {
super.setPrimaryColor(ta.getColor(R.styleable.ClassicsHeader_srlPrimaryColor, 0));
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlAccentColor)) {
setAccentColor(ta.getColor(R.styleable.ClassicsHeader_srlAccentColor, 0));
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextPulling)) {
mTextPulling = ta.getString(R.styleable.ClassicsHeader_srlTextPulling);
} else if (REFRESH_HEADER_PULLING != null) {
mTextPulling = REFRESH_HEADER_PULLING;
} else {
mTextPulling = context.getString(R.string.srl_header_pulling);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextLoading)) {
mTextLoading = ta.getString(R.styleable.ClassicsHeader_srlTextLoading);
} else if (REFRESH_HEADER_LOADING != null) {
mTextLoading = REFRESH_HEADER_LOADING;
} else {
mTextLoading = context.getString(R.string.srl_header_loading);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextRelease)) {
mTextRelease = ta.getString(R.styleable.ClassicsHeader_srlTextRelease);
} else if (REFRESH_HEADER_RELEASE != null) {
mTextRelease = REFRESH_HEADER_RELEASE;
} else {
mTextRelease = context.getString(R.string.srl_header_release);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextFinish)) {
mTextFinish = ta.getString(R.styleable.ClassicsHeader_srlTextFinish);
} else if (REFRESH_HEADER_FINISH != null) {
mTextFinish = REFRESH_HEADER_FINISH;
} else {
mTextFinish = context.getString(R.string.srl_header_finish);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextFailed)) {
mTextFailed = ta.getString(R.styleable.ClassicsHeader_srlTextFailed);
} else if (REFRESH_HEADER_FAILED != null) {
mTextFailed = REFRESH_HEADER_FAILED;
} else {
mTextFailed = context.getString(R.string.srl_header_failed);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextSecondary)) {
mTextSecondary = ta.getString(R.styleable.ClassicsHeader_srlTextSecondary);
} else if (REFRESH_HEADER_SECONDARY != null) {
mTextSecondary = REFRESH_HEADER_SECONDARY;
} else {
mTextSecondary = context.getString(R.string.srl_header_secondary);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextRefreshing)) {
mTextRefreshing = ta.getString(R.styleable.ClassicsHeader_srlTextRefreshing);
} else if (REFRESH_HEADER_REFRESHING != null) {
mTextRefreshing = REFRESH_HEADER_REFRESHING;
} else {
mTextRefreshing = context.getString(R.string.srl_header_refreshing);
}
if (ta.hasValue(R.styleable.ClassicsHeader_srlTextUpdate)) {
mTextUpdate = ta.getString(R.styleable.ClassicsHeader_srlTextUpdate);
} else if (REFRESH_HEADER_UPDATE != null) {
mTextUpdate = REFRESH_HEADER_UPDATE;
} else {
mTextUpdate = context.getString(R.string.srl_header_update);
}
mLastUpdateFormat = new SimpleDateFormat(mTextUpdate, Locale.getDefault());
ta.recycle();
updateView.setId(ID_TEXT_UPDATE);
updateView.setVisibility(mEnableLastTime ? VISIBLE : GONE);
centerLayout.addView(updateView, lpUpdateText);
mTitleText.setText(thisView.isInEditMode() ? mTextRefreshing : mTextPulling);
try {//try 不能删除-否则会出现兼容性问题
if (context instanceof FragmentActivity) {
FragmentManager manager = ((FragmentActivity) context).getSupportFragmentManager();
if (manager != null) {
@SuppressLint("RestrictedApi")
List<Fragment> fragments = manager.getFragments();
if (fragments != null && fragments.size() > 0) {
setLastUpdateTime(new Date());
return;
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
KEY_LAST_UPDATE_TIME += context.getClass().getName();
mShared = context.getSharedPreferences("ClassicsHeader", Context.MODE_PRIVATE);
setLastUpdateTime(new Date(mShared.getLong(KEY_LAST_UPDATE_TIME, System.currentTimeMillis())));
}
@Override
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
if (success) {
mTitleText.setText(mTextFinish);
if (mLastTime != null) {
setLastUpdateTime(new Date());
}
} else {
mTitleText.setText(mTextFailed);
}
return super.onFinish(layout, success);//延迟500毫秒之后再弹回
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
final View arrowView = mArrowView;
final View updateView = mLastUpdateText;
switch (newState) {
case None:
updateView.setVisibility(mEnableLastTime ? VISIBLE : GONE);
case PullDownToRefresh:
mTitleText.setText(mTextPulling);
arrowView.setVisibility(VISIBLE);
arrowView.animate().rotation(0);
break;
case Refreshing:
case RefreshReleased:
mTitleText.setText(mTextRefreshing);
arrowView.setVisibility(GONE);
break;
case ReleaseToRefresh:
mTitleText.setText(mTextRelease);
arrowView.animate().rotation(180);
break;
case ReleaseToTwoLevel:
mTitleText.setText(mTextSecondary);
arrowView.animate().rotation(0);
break;
case Loading:
arrowView.setVisibility(GONE);
updateView.setVisibility(mEnableLastTime ? INVISIBLE : GONE);
mTitleText.setText(mTextLoading);
break;
}
}
public ClassicsHeaderNew setLastUpdateTime(Date time) {
final View thisView = this;
mLastTime = time;
mLastUpdateText.setText(mLastUpdateFormat.format(time));
if (mShared != null && !thisView.isInEditMode()) {
mShared.edit().putLong(KEY_LAST_UPDATE_TIME, time.getTime()).apply();
}
return this;
}
public ClassicsHeaderNew setTimeFormat(DateFormat format) {
mLastUpdateFormat = format;
if (mLastTime != null) {
mLastUpdateText.setText(mLastUpdateFormat.format(mLastTime));
}
return this;
}
public ClassicsHeaderNew setLastUpdateText(CharSequence text) {
mLastTime = null;
mLastUpdateText.setText(text);
return this;
}
public ClassicsHeaderNew setAccentColor(@ColorInt int accentColor) {
mTitleText.setTextColor(accentColor );
mLastUpdateText.setTextColor(accentColor);
return this;
}
public ClassicsHeaderNew setEnableLastTime(boolean enable) {
final View updateView = mLastUpdateText;
mEnableLastTime = enable;
updateView.setVisibility(enable ? VISIBLE : GONE);
if (mRefreshKernel != null) {
mRefreshKernel.requestRemeasureHeightFor(this);
}
return this;
}
public ClassicsHeaderNew setTextSizeTime(float size) {
mLastUpdateText.setTextSize(size);
if (mRefreshKernel != null) {
mRefreshKernel.requestRemeasureHeightFor(this);
}
return this;
}
public ClassicsHeaderNew setTextTimeMarginTop(float dp) {
final View updateView = mLastUpdateText;
MarginLayoutParams lp = (MarginLayoutParams) updateView.getLayoutParams();
lp.topMargin = DensityUtil.dp2px(dp);
updateView.setLayoutParams(lp);
return this;
}
}

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
<com.yunbao.common.views.weight.ClassicsHeaderNew
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -95,7 +95,7 @@ public class SidebarLiveAudience extends AbsDialogFragment {
}
sidebarList = (CommonRefreshView) findViewById(R.id.sidebarList);
// sidebarList.setHeaderTextColor(Color.parseColor("#50ffffff"));
sidebarList.setHeaderTextColor(Color.parseColor("#ffffff"));
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2, GridLayoutManager.VERTICAL, false);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override