YCStateLayout

Introduction: 状态切换,让 View 状态的切换和 Activity 彻底分离开。用 builder 模式来自由的添加需要的状态 View,可以设置有数据,数据为空,加载数据错误,网络错误,加载中等多种状态,并且支持自定义状态的布局。。目前已经用于新芽正式项目中,拓展性强!!
More: Author   ReportBugs   
Tags:

0.Dependency

0.1 Used

  • Add this in your root build.gradle file (not your module build.gradle file):
      allprojects {
          repositories {
              maven { url "https://jitpack.io" }
          }
      }
    
  • Then, add the library to your module build.gradle
      iimplementation 'com.github.yangchong211:YCStateLayout:1.2.5'
    

0.2 Basic introduction

Function introduction
  • State switching, so that the View state switch and Activity completely separate. Using builder mode to freely add the required state View, can set data, data is empty, load data error, network error, load and other states, and support the layout of custom state. At present has been used in other formal projects, strong expansion!
About language

0.3 Case demonstration animation

image

1.An introduction to the switching State of Android Interface

1.1 Problems encountered in development

  • How to switch the interface state?
  • Some interfaces want to customize custom state?
  • How does the state add a click event?

1.2 You can set up more than 5 state View

  • content interface view
  • loading data view
  • loading data error view
  • no data after loading view
  • no network view

2.Train of thought change, extract separate class management several kinds of state

2.1 Previous practice:

  • Directly include these interfaces into the main interface, then dynamically switch the interface, later found that this processing is not easy to reuse in other projects, and in activity to deal with the display and hiding of these states is messy
  • Using the child class to inherit the parent property, write the switching state in the parent class, but what can some interfaces do if they do not inherit the parent class

2.2 Present practice:

  • The switching of View states is completely separated from Activity. The View of these states must be encapsulated in a management class and several methods are exposed to implement the switching between View.
  • The View that can be required varies from project to project, so consider designing the management class as an builder schema to freely add the required state View

3.An Analysis of the advantages of this State switching tool

  • Free to switch content, empty data, exception error, load, network error, etc.
  • The parent class BaseActivity directly exposes the state in 5, making it easy for subclasses to uniformly manage state switching
  • If some pages want to customize the state layout, they can also be implemented freely, simple

4.usage method

4.1 Specific use

  • As shown below, you can refer directly to the code, and more directly see the demo ``` statusLayoutManager = StateLayoutManager.newBuilder(this)
              .contentView(R.layout.activity_content)
              .emptyDataView(R.layout.activity_emptydata)
              .errorView(R.layout.activity_error)
              .loadingView(R.layout.activity_loading)
              .netWorkErrorView(R.layout.activity_networkerror)
              .build();
    
/**
 * Click to refresh the data
 */
private void initEmptyDataView() {
    statusLayoutManager.showEmptyData();
    LinearLayout ll_empty_data = (LinearLayout) findViewById(R.id.ll_empty_data);
    ll_empty_data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            initData();
            adapter.notifyDataSetChanged();
            showContent();
        }
    });
}

/**
 * Click to refresh
 */
private void initErrorDataView() {
    statusLayoutManager.showError();
    LinearLayout ll_error_data = (LinearLayout) findViewById(R.id.ll_error_data);
    ll_error_data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            initData();
            adapter.notifyDataSetChanged();
            showContent();
        }
    });Click to refresh
}

/**
 * Click set up the network
 */
private void initSettingNetwork() {
    statusLayoutManager.showNetWorkError();
    LinearLayout ll_set_network = (LinearLayout) findViewById(R.id.ll_set_network);
    ll_set_network.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
            startActivity(intent);
        }
    });
}
```

5.Realization effect

6.About other

6.1 Update

  • v1.0 Updated on 28 March 2017
  • v1.1 Updated on 3 December 2017
  • v1.1.5 Updated on 25 April 2018
  • v1.1.6 Updated on November 15, 2018, updating targetSdkVersion to 27

6.2 about 支付宝

image

6.4 About recommend

  • Great summary of blog notes, including Java basics and in-depth knowledge, Android technology blog, Python learning notes, and so on, but also includes the usual development of bug summary, of course, also collected a large number of interview questions after work. Long-term update, maintenance and revision, continuous improvement. Open source files are in markdown format!
  • chained address : https://github.com/yangchong211/YCBlogs
  • If you feel good, you can star, thank you! Of course, you are also welcome to put forward suggestions, everything starts at a slight, quantitative change causes qualitative change!

6.4 About LICENSE

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools