android右到左布局未能切换_Android更多状态的布局Layout,可随意切换状态的Layout,加载中、加载失败、空布局......

本文介绍了如何使用CustomStateLayout实现加载中、加载失败、加载错误、内容为空等多种状态的切换。通过自定义布局,可以方便地在不同状态间进行切换,提供了方便的显示和隐藏状态的方法。

使用CustomStateLayout作为底层布局,可一个方法切换 加载中、加载失败、加载错误、内容为空、正常布局等等的状态;

public class CustomStateLayout extends RelativeLayout {

private Context context;

private final int NORMAL = 0; //常规状态

private final int LOADING = 1; //加载中状态

private final int LOADFAIL = 2; //加载失败状态

private final int LOADERROR = 3; //加载错误状态

private final int LOADEMPTY = 4; //内容为空状态

public CustomStateLayout(Context context) {

super(context);

this.context = context;

init();

}

public CustomStateLayout(Context context, AttributeSet attrs) {

super(context, attrs);

this.context = context;

init();

}

public CustomStateLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

this.context = context;

init();

}

private RelativeLayout RelativeLayoutLoadIng,RelativeLayoutLoadFin,RelativeLayoutLoadError,RelativeLayoutLoadEmpty;

private LayoutInflater inflate;

private void init(){

inflate = LayoutInflater.from(context);

}

private void setLoadFail(){

if (RelativeLayoutLoadFin == null){

View v = this.inflate.inflate(R.layout.custom_loadfail,null);

RelativeLayoutLoadFin = v.findViewById(R.id.loadFin);

this.addView(RelativeLayoutLoadFin,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

}else{

RelativeLayoutLoadFin.setVisibility(VISIBLE);

}

}

private void setLoadIng(){

if (RelativeLayoutLoadIng == null){

View v = this.inflate.inflate(R.layout.custom_loading,null);

RelativeLayoutLoadIng = v.findViewById(R.id.loadIng);

this.addView(RelativeLayoutLoadIng,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

}else{

RelativeLayoutLoadIng.setVisibility(VISIBLE);

}

}

private void setLoadError(){

if (RelativeLayoutLoadError == null){

View v = this.inflate.inflate(R.layout.custom_loaderror,null);

RelativeLayoutLoadError = v.findViewById(R.id.loadError);

this.addView(RelativeLayoutLoadError,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

}else{

RelativeLayoutLoadError.setVisibility(VISIBLE);

}

}

private void setLoadEmpty(){

if (RelativeLayoutLoadEmpty == null){

View v = this.inflate.inflate(R.layout.custom_loadempty,null);

RelativeLayoutLoadEmpty = v.findViewById(R.id.loadEmpty);

this.addView(RelativeLayoutLoadEmpty,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

}else{

RelativeLayoutLoadEmpty.setVisibility(VISIBLE);

}

}

private void hideLoadFail(){

if (RelativeLayoutLoadFin != null){

RelativeLayoutLoadFin.setVisibility(GONE);

}

}

private void hideLoadIng(){

if (RelativeLayoutLoadIng != null){

RelativeLayoutLoadIng.setVisibility(GONE);

}

}

private void hideLoadError(){

if (RelativeLayoutLoadError != null){

RelativeLayoutLoadError.setVisibility(GONE);

}

}

private void hideLoadEmpty(){

if (RelativeLayoutLoadEmpty != null){

RelativeLayoutLoadEmpty.setVisibility(GONE);

}

}

public void showLoadIng(){

setState(LOADING);

}

public void showLoadFail(){

setState(LOADFAIL);

}

public void showLoadError(){

setState(LOADERROR);

}

public void showLoadEmpty(){

setState(LOADEMPTY);

}

public void hideAllState(){

setState(NORMAL);

}

private void setState(int state){

switch (state){

case NORMAL :

hideLoadIng();

hideLoadFail();

hideLoadError();

hideLoadEmpty();

break;

case LOADING :

setLoadIng();

hideLoadFail();

hideLoadError();

hideLoadEmpty();

break;

case LOADFAIL :

hideLoadIng();

setLoadFail();

hideLoadError();

hideLoadEmpty();

break;

case LOADERROR:

hideLoadIng();

hideLoadFail();

setLoadError();

hideLoadEmpty();

break;

case LOADEMPTY:

hideLoadIng();

hideLoadFail();

hideLoadError();

setLoadEmpty();

break;

default:

hideLoadIng();

hideLoadFail();

hideLoadError();

hideLoadEmpty();

break;

}

}

}

随便的一个“加载中”布局 R.layout.custom_loading (其它的各种状态布局都可以自己定义):

android:id="@+id/loadIng"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff"

>

android:layout_centerInParent="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/iv"

android:layout_width="40dp"

android:layout_height="40dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_toRightOf="@id/iv"

android:layout_marginLeft="10dp"

android:text="加载中..."

/>

使用:

public class CustomLayoutStateActivity extends AppCompatActivity implements View.OnClickListener {

private CustomStateLayout mCustomStateLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_custom_layout_state);

mCustomStateLayout = (CustomStateLayout) findViewById(R.id.customRl);

findViewById(R.id.bt1).setOnClickListener(this);

findViewById(R.id.bt2).setOnClickListener(this);

findViewById(R.id.bt3).setOnClickListener(this);

findViewById(R.id.bt4).setOnClickListener(this);

findViewById(R.id.bt5).setOnClickListener(this);

}

@Override

public void onClick(View view) {

switch (view.getId()) {

case R.id.bt1:

mCustomStateLayout.hideAllState();

break;

case R.id.bt2:

mCustomStateLayout.showLoadIng();

break;

case R.id.bt3:

mCustomStateLayout.showLoadFail();

break;

case R.id.bt4:

mCustomStateLayout.showLoadError();

break;

case R.id.bt5:

mCustomStateLayout.showLoadEmpty();

break;

}

}

}

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center_horizontal">

android:layout_width="match_parent"

android:id="@+id/customRl"

android:layout_height="200dp">

android:background="#ffdd88"

android:text="我是原布局"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/bt1"

android:text="正常的"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/bt2"

android:text="加载中的"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/bt3"

android:text="加载失败的"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/bt4"

android:text="加载错误的"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/bt5"

android:text="加载空的"

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值