mylhyl / android-pagestatelayout Goto Github PK
View Code? Open in Web Editor NEWandroid page state toggle 界面加载状态切换
android page state toggle 界面加载状态切换
项目中类似recyclerView或者listView和ScrollView需要状态的情况也不在少数,测试发现
在activity中直接pageState = StatusLayout.wrap(this,R.id.id_of_scroll_view)
类似这样的代码,在scrollView设置了权重的情况下,并不能加载scrollview正确的宽高。
原理很简单,原View被加载到了自定义View,自定义View获取了它的lp,但是原View的宽高这时候不应该都是MATCH_PARENT吗?
我懒得copy和paste,直接反射写了段。
public static PageState wrap(Activity activity, @IdRes int contentId) {
ViewGroup actContent = activity.findViewById(android.R.id.content);
ViewGroup rootLayout = (ViewGroup) (actContent).getChildAt(0);
View contentLayout = rootLayout.findViewById(contentId);
if (contentLayout == null) {
throw new RuntimeException("contentLayout can not be null");
}
ViewGroup.LayoutParams lp = contentLayout.getLayoutParams();
ViewGroup parent = (ViewGroup) contentLayout.getParent();
if (parent == null) {
throw new RuntimeException("parent of the contentLayout can not be null");
}
int contentViewIndex = parent.indexOfChild(contentLayout);
// 移除子View
parent.removeView(contentLayout);
// 添加statusView
PageStateLayout statusLayout = new PageStateLayout(parent.getContext());
parent.addView(statusLayout, contentViewIndex,lp);
// statusView添加子View
statusLayout.addView(contentLayout);
// 设置宽高
contentLayout.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
contentLayout.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
// 反射调用setContentView方法,将contentLayout设置为statusLayout的contentLayout
Class<?> temp = statusLayout.getClass();
Field f;
try {
f = temp.getDeclaredField("mPageStateCreator");
f.setAccessible(true);
Method get = f.get(statusLayout).getClass().getDeclaredMethod("setContentView", View.class);
get.setAccessible(true);
get.invoke(f.get(statusLayout),contentLayout);
} catch (Exception e) {
e.printStackTrace();
}
return statusLayout;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.