Android---stateLayout在不同的界面间切换

----- 效果图


------- 添加依赖

compile 'com.helin.loadinglayout:loadinglayout:0.0.1'

------ layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3000"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_loading"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="loading" />

        <Button
            android:id="@+id/btn_empty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="empty" />

        <Button
            android:id="@+id/btn_fail"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="fail" />

        <Button
            android:id="@+id/btn_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="content" />
    </LinearLayout>

    <com.helin.loadinglayout.LoadingLayout
        android:id="@+id/loading_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="2dp"></ListView>

    </com.helin.loadinglayout.LoadingLayout>

</LinearLayout>

---- 核心代码是

 <com.helin.loadinglayout.LoadingLayout
        android:id="@+id/loading_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="2dp"></ListView>

    </com.helin.loadinglayout.LoadingLayout>

------ code

private ListView mListView;
    private LoadingLayout loadingLayout;
    private Button btnLoading;
    private Button btnEmpty;
    private Button btnFail;
    private Button btnContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statelayout);
        initView();
        initData();
        initListenter();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.list_view);
        loadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);

        /*btn*/
        btnLoading = (Button) findViewById(R.id.btn_loading);
        btnEmpty = (Button) findViewById(R.id.btn_empty);
        btnFail = (Button) findViewById(R.id.btn_fail);
        btnContent = (Button) findViewById(R.id.btn_content);

    }


    private void initData() {
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        setStrings(stringArrayAdapter);
        mListView.setAdapter(stringArrayAdapter);
    }

    private void initListenter() {
        btnLoading.setOnClickListener(this);
        btnEmpty.setOnClickListener(this);
        btnFail.setOnClickListener(this);
        btnContent.setOnClickListener(this);

        /*loadingLayout 为fail设置点击事件*/
        loadingLayout.setViewOncClickListener(R.id.state_retry,
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loadingLayout.showLoading();
                    }
                });
    }

    @NonNull
    private void setStrings(ArrayAdapter<String> arrayAdapter) {
        for (int i = 0; i < 30; i++) {
            arrayAdapter.add("item" + i);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_loading:
                loadingLayout.showLoading();
                break;
            case R.id.btn_empty:
                loadingLayout.showEmpty();
                break;
            case R.id.btn_fail:
                loadingLayout.showState();
                break;
            case R.id.btn_content:
                loadingLayout.showContent();
                break;
            default:
                break;
        }
    }


---- 定制material progressbar 样式



------ stateLayout的思路

添加4种子布局,通过控制布局的显示和隐藏达到目的。


----  参考blog

简书 _小河马 点击打开链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值