Android Skeleton骨架屏在fragment中的應(yīng)用

最近項(xiàng)目要求加入骨架屏,引用之后發(fā)現(xiàn)在fragment中把代碼放入onCreateView( )方法中發(fā)現(xiàn)不顯示,以此作為記錄。

原因查看:
1.點(diǎn)開.show()方法進(jìn)入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發(fā)現(xiàn)進(jìn)入了
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()中調(diào)用方法,在數(shù)據(jù)加載完畢的時(shí)候關(guān)閉骨架屏

    @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();
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容