Comments (5)
不能做到页面中的局部布局状态更换……
from ycstatelayout.
fragment?
from ycstatelayout.
可以,我的写法和这个项目的实质是一样的,局部,listview都行,但是多层嵌套(activity 嵌套fragment 嵌套fragment)就有问题,原因是获取view的问题。
from ycstatelayout.
@yingzikeji 能否把你的局部更新状态demo发个链接给我看看,多谢!
from ycstatelayout.
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.support.annotation.LayoutRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.idyoga.common.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;
import vip.devkit.library.Logcat;
/**
* File Name: BaseLayoutManager
* Create Date: 2018/10/17 10:50
* Describe :
* Author : By k
* E-mail :[email protected]
* VersionName: 1
* VersionCode: V 1.0
* Code Update:(author - time)
* Update Describe:
*/
public class BaseLayoutManager extends FrameLayout {
@IntDef({EMPTY, RETRY, LOADING, ERROR, NET_ERROR})
@Retention(RetentionPolicy.SOURCE)
public @interface ViewType {
}
public static final int EMPTY = 0x00000000;
public static final int RETRY = 0x00000001;
public static final int LOADING = 0x00000002;
public static final int ERROR = 0x00000003;
public static final int NET_ERROR = 0x00000005;
private BaseLayoutManager.OnRetryClickListener mRetryClickListener;
private BaseLayoutManager.OnInflateListener mInflateListener;
/**
* 初始化Activity
*
* @param activity the activity
* @return the
*/
public static BaseLayoutManager wrap(Activity activity) {
return wrap(((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0));
}
/**
* 初始化Fragment
*
* @param fragment the fragment
* @return
*/
public static BaseLayoutManager wrap(android.support.v4.app.Fragment fragment) {
return wrap(fragment.getView());
}
/**
* 初始化Fragment
*
* @param fragment the fragment
* @return
*/
public static BaseLayoutManager wrap(android.app.Fragment fragment) {
return wrap(fragment.getView());
}
/**
* 初始化View
*
* @param view the view
* @return
*/
public static BaseLayoutManager wrap(View view) {
if (view == null) {
throw new RuntimeException("content view can not be null");
}
ViewGroup parent = (ViewGroup) view.getParent();
if (view == null) {
throw new RuntimeException("parent view can not be null");
}
if (view == null) {
throw new RuntimeException("parent view can not be null");
}
ViewGroup.LayoutParams lp = view.getLayoutParams();
int index = parent.indexOfChild(view);
parent.removeView(view);
BaseLayoutManager layout = new BaseLayoutManager(view.getContext());
parent.addView(layout, index, lp);
layout.addView(view);
layout.setContentView(view);
return layout;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() == 0) {
return;
}
if (getChildCount() > 1) {
removeViews(1, getChildCount() - 1);
}
View view = getChildAt(0);
setContentView(view);
showLoading();
}
int mEmptyImage;
CharSequence mEmptyText;
int mErrorImage;
CharSequence mErrorText, mRetryText;
int mTextColor, mTextSize;
int mButtonTextColor, mButtonTextSize;
Drawable mButtonBackground;
int mEmptyResId = NO_ID, mLoadingResId = NO_ID, mErrorResId = NO_ID, mNetErrorResId = NO_ID;
int mContentId = NO_ID;
Map<Integer, View> mLayouts = new HashMap<>();
public BaseLayoutManager(Context context) {
this(context, null, R.attr.styleBaseLayoutManager);
}
public BaseLayoutManager(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.styleBaseLayoutManager);
}
LayoutInflater mInflater;
public BaseLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mInflater = LayoutInflater.from(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BaseLayoutManager, defStyleAttr, R.style.BaseLayoutManager_Style);
mEmptyImage = a.getResourceId(R.styleable.BaseLayoutManager_llEmptyImage, NO_ID);
mEmptyText = a.getString(R.styleable.BaseLayoutManager_llEmptyText);
mErrorImage = a.getResourceId(R.styleable.BaseLayoutManager_llErrorImage, NO_ID);
mErrorText = a.getString(R.styleable.BaseLayoutManager_llErrorText);
mRetryText = a.getString(R.styleable.BaseLayoutManager_llRetryText);
mTextColor = a.getColor(R.styleable.BaseLayoutManager_llTextColor, 0xff999999);
mTextSize = a.getDimensionPixelSize(R.styleable.BaseLayoutManager_llTextSize, dp2px(16));
mButtonTextColor = a.getColor(R.styleable.BaseLayoutManager_llButtonTextColor, 0xff999999);
mButtonTextSize = a.getDimensionPixelSize(R.styleable.BaseLayoutManager_llButtonTextSize, dp2px(16));
mButtonBackground = a.getDrawable(R.styleable.BaseLayoutManager_llButtonBackground);
mEmptyResId = a.getResourceId(R.styleable.BaseLayoutManager_llEmptyResId, R.layout.base_layout_empty);
mLoadingResId = a.getResourceId(R.styleable.BaseLayoutManager_llLoadingResId, R.layout.base_layout_loading);
mErrorResId = a.getResourceId(R.styleable.BaseLayoutManager_llErrorResId, R.layout.base_layout_error);
mNetErrorResId = a.getResourceId(R.styleable.BaseLayoutManager_llNetErrorResId, R.layout.base_layout_net_error);
a.recycle();
}
int dp2px(float dp) {
return (int) (getResources().getDisplayMetrics().density * dp);
}
private void setContentView(View view) {
mContentId = view.getId();
mLayouts.put(mContentId, view);
}
public BaseLayoutManager setLoading(@LayoutRes int id) {
if (mLoadingResId != id) {
remove(mLoadingResId);
mLoadingResId = id;
mLayouts.put(mLoadingResId, layout(mLoadingResId));
}
return this;
}
public BaseLayoutManager setEmpty(@LayoutRes int id) {
if (mEmptyResId != id) {
remove(mEmptyResId);
mEmptyResId = id;
mLayouts.put(mEmptyResId, layout(mEmptyResId));
}
return this;
}
public BaseLayoutManager setError(@LayoutRes int id) {
if (mErrorResId != id) {
remove(mErrorResId);
mErrorResId = id;
mLayouts.put(mErrorResId, layout(mErrorResId));
}
return this;
}
public BaseLayoutManager setNetError(@LayoutRes int id) {
if (mNetErrorResId != id) {
remove(mNetErrorResId);
mNetErrorResId = id;
mLayouts.put(mNetErrorResId, layout(mNetErrorResId));
}
return this;
}
public void showLoading() {
show(mLoadingResId);
}
public void showEmpty() {
show(mEmptyResId);
}
public void showError() {
show(mErrorResId);
}
public void showNetError() {
show(mNetErrorResId);
}
public void showContent() {
hide();
}
private void show(int layoutId) {
for (View view : mLayouts.values()) {
view.setVisibility(GONE);
}
layout(layoutId).setVisibility(VISIBLE);postInvalidate();
}
private void hide() {
for (View view : mLayouts.values()) {
view.setVisibility(GONE);
}
mLayouts.get(mContentId).setVisibility(VISIBLE);
postInvalidate();
}
private void remove(int layoutId) {
if (mLayouts.containsKey(layoutId)) {
View vg = mLayouts.remove(layoutId);
removeView(vg);
}
}
private View layout(final int layoutId) {
// Logcat.e("mLayouts 是否包含 layoutId:"+mLayouts.containsKey(layoutId));
if (mLayouts.containsKey(layoutId)) {
return mLayouts.get(layoutId);
}
View layout = mInflater.inflate(layoutId, this, false);
layout.setVisibility(GONE);
addView(layout);
mLayouts.put(layoutId, layout);
if (layoutId == mLoadingResId) {
} else if (layoutId == mEmptyResId) {
} else if (layoutId == mErrorResId) {
} else if (layoutId == mNetErrorResId) {
layout.findViewById(R.id.tv_retry).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Logcat.i("点击了重试");
if (mRetryClickListener != null) {
mRetryClickListener.onRetryClick();
}
}
});
}
ViewGroup viewGroup = (ViewGroup) layout;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
viewGroup.getChildAt(i).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mInflateListener != null) {
if (layoutId == mLoadingResId) {
mInflateListener.onInflate(LOADING, view);
} else if (layoutId == mEmptyResId) {
mInflateListener.onInflate(EMPTY, view);
} else if (layoutId == mErrorResId) {
mInflateListener.onInflate(ERROR, view);
} else if (layoutId == mNetErrorResId) {
mInflateListener.onInflate(NET_ERROR, view);
}
}
}
});
}
return layout;
}
/**
* 测量view
*
* @param view
*/
public static void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(childWidthSpec, childHeightSpec);
}
/**
* 获取view 的宽度
*
* @param view
* @return
*/
public static int getViewWidth(View view) {
measureView(view);
return view.getMeasuredWidth();
}
/**
* 获取View 的高度
*
* @param view
* @return
*/
public static int getViewHeight(View view) {
measureView(view);
return view.getMeasuredHeight();
}
/**
* 监听重试
*
* @param listener {@link BaseLayoutManager.OnRetryClickListener}
*/
public void setOnRetryClickListener(BaseLayoutManager.OnRetryClickListener listener) {
this.mRetryClickListener = listener;
}
/**
* Listener used to receive a notification after the RetryView is clicked.
*/
public interface OnRetryClickListener {
void onRetryClick();
}
/**
* Specifies the inflate listener to be notified after this StateView successfully
* inflated its layout resource.
*
* @param inflateListener The OnInflateListener to notify of successful inflation.
* @see BaseLayoutManager.OnInflateListener
*/
public void setOnInflateListener(BaseLayoutManager.OnInflateListener inflateListener) {
this.mInflateListener = inflateListener;
}
/**
* Listener used to receive a notification after a StateView has successfully
* inflated its layout resource.
*
* @see BaseLayoutManager#setOnInflateListener(BaseLayoutManager.OnInflateListener)
*/
public interface OnInflateListener {
/**
* @param view The inflated View.
*/
void onInflate(@BaseLayoutManager.ViewType int viewType, View view);
}
/**
* @param viewType
* @return 返回 viewType 对应的 View
*/
public LViewHolder initView(@BaseLayoutManager.ViewType int viewType) {
LViewHolder holder = null;
if (viewType == LOADING) {
holder = new LViewHolder(getContext(), mLayouts.get(mLoadingResId));
} else if (viewType == EMPTY) {
holder = new LViewHolder(getContext(), mLayouts.get(mEmptyResId));
} else if (viewType == ERROR) {
holder = new LViewHolder(getContext(), mLayouts.get(mErrorResId));
} else if (viewType == NET_ERROR) {
holder = new LViewHolder(getContext(), mLayouts.get(mNetErrorResId));
}
return holder;
}
}
styleable 可以不需要的
初始化直接使用initView,方便多状态的时候使用
from ycstatelayout.
Related Issues (10)
- fragment findviewbyid 为null
- 设置到特定位置
- 不支持fragment吗 HOT 1
- 能否做到recyclerView中的item局部状态loading HOT 1
- 可以支持自定义加载loading动画吗 HOT 1
- 能否给异常界面设置点击事件 HOT 1
- 测试页面3 切换到空布局的时候 异常 HOT 1
- NullPointerException
- 有个特别需要优化的地方 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ycstatelayout.