Android Skeleton骨架屏在fragment中的应用
2021-05-08 本文已影响0人
Looper
最近项目要求加入骨架屏,引用之后发现在fragment中把代码放入onCreateView( )方法中发现不显示,以此作为记录。
原因查看:
1.点开.show()方法进入ViewSkeletonScreen.class
2.找到
private View generateSkeletonLoadingView() {
ViewParent viewParent = mActualView.getParent();
if (viewParent == null) {
Log.e(TAG, "the source view have not attach to any view");
return null;
}
ViewGroup parentView = (ViewGroup) viewParent;
if (mShimmer) {
return generateShimmerContainerLayout(parentView);
}
return LayoutInflater.from(mActualView.getContext()).inflate(mSkeletonResID, parentView, false);
}
通过log发现进入了
Log.e(TAG, "the source view have not attach to any view");
解决方法
1.xml中不要直接引用最外层布局,而是里面嵌套一层
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".deviceport.home.fragment.PortCourseListNewFragment">
<!-- 筛选-->
<RelativeLayout
android:id="@+id/filterRl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent" />
<!-- 显示骨架层的位置-->
<LinearLayout
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/filterRl">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshSl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.在onResume()中调用方法,在数据加载完毕的时候关闭骨架屏
@Override
public void onResume() {
super.onResume();
//骨架屏
if (!loadDataFinish) {
skeletonScreen = Skeleton.bind(binding.mainView)
.load(R.layout.skeleton_course_list)
.shimmer(true)
.angle(ParamsUtil.SkeletonAngle)
.duration(ParamsUtil.SkeletonTime)
.color(R.color.skeleton_line_color)
.show();
}
}
@Override
public void showPlanList(ArrayList<PlanModelBean> planList) {
if (!loadDataFinish&&skeletonScreen!=null) {
loadDataFinish = true;
skeletonScreen.hide();
}
}