Coder Social home page Coder Social logo

android-pagestatelayout's Introduction

简介

Adnroid界面数据从网络加载情况下的状态切换:包括加载中、加载失败、加载空数据、网络异常

引用

   allprojects {
   	repositories {
   		...
   		maven { url 'https://jitpack.io' }
   	}
   }

Download
implementation 'com.github.mylhyl:Android-PageStateLayout:latestVersion'

非androidx请移步v1

下载APK体验或手机扫描下面二维码

使用

  • layout中使用,注意PageStateLayout中只能包含一个子布局(需要显示的数据内容)
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/smartRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--app:psl_contentLayoutId="@id/recyclerView" 如果不指定,那么只能包含一个子视图-->
        <!--也可直接用原生 FrameLayout或 RelativeLayout 代替PageStateLayout,代码中则使有用 PageStateLayout.wrap(activity,int,int)-->
        <com.mylhyl.pagestate.PageStateLayout
            android:id="@+id/pageStateLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:psl_contentLayoutId="@id/recyclerView"
            app:psl_emptyImgSrc="@mipmap/ic_push_msg_empty"
            app:psl_errorImgSrc="@mipmap/ic_pager_invalid"
            app:psl_errorNetImgSrc="@mipmap/ic_network_invalid">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </com.mylhyl.pagestate.PageStateLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
  • 代码中使用
 //contentId:是与其它4个状态切换显示的数据内容布局id
PageStateLayout.wrap(Activity activity, int contentId)
PageStateLayout.wrap(Fragment fragment, int contentId)

更多请查看wrap方法重载

//如下: PageStateLayout.wrap(MainActivity.this, R.id.psl_content) xml

提供方法

代码支持的方法
layout支持的属性

全局替换

  • layout.xml 在工程中新建需要自定义的布局,文件名必须与library-layout相同,且view的id也必须相同
  • color.xml资源文件的name必须相同,传送门
  • 正在加载...暂无数据加载失败网络异常请检查不喜欢可替换,string.xml资源文件的name必须相同

android-pagestatelayout's People

Contributors

mylhyl avatar qingtianxiaoyu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

qingtianxiaoyu

android-pagestatelayout's Issues

当指定的contentId布局宽高有权重或者是约束布局的子View的时候宽高显示不正常。

项目中类似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;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.