首页 > 其他分享 >Android自定义view实现加载中、加载失败、无数据

Android自定义view实现加载中、加载失败、无数据

时间:2023-03-02 13:04:04浏览次数:54  
标签:layout 自定义 void id child Android 加载 public android


一、概述

Android中经常在有的app中可以见到“加载中”并不是以弹出对话框的形式显示的,而是占用整个屏幕,如果加载失败就会出现加载失败页面,点击加载失败页面中任意区域,都可以重新加载。今天就和大家一起学习如何通过自定义view的方式实现加载中、加载失败、无数据的效果。

Android自定义view实现加载中、加载失败、无数据_android

二、实现代码

自定义属性文件

<declare-styleable name="LoadingLayout">
<attr name="loadingView" format="reference" />
<attr name="stateView" format="reference" />
<attr name="emptyView" format="reference" />
</declare-styleable>

自定义view:LoadingLayout.java

package com.czhappy.effectdemo.loadinglayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.czhappy.effectdemo.R;


public class LoadingLayout extends FrameLayout {

/**
* 空数据View
*/
private int mEmptyView;
/**
* 状态View
*/
private int mStateView;
/**
* 加载View
*/
private int mLoadingView;

public LoadingLayout(Context context) {
this(context, null);
}

public LoadingLayout(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}

public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LoadingLayout, 0, 0);
try {
mStateView = a.getResourceId(R.styleable.LoadingLayout_stateView, R.layout.loadstate_layout);
mLoadingView = a.getResourceId(R.styleable.LoadingLayout_loadingView, R.layout.loading_layout);
mEmptyView = a.getResourceId(R.styleable.LoadingLayout_emptyView, R.layout.empty_layout);
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(mStateView, this, true);
inflater.inflate(mLoadingView, this, true);
inflater.inflate(mEmptyView, this, true);
} finally {
a.recycle();
}
}

/**
* 布局加载完成后隐藏所有View
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
for (int i = 0; i < getChildCount() - 1; i++) {
getChildAt(i).setVisibility(GONE);
}
}


/**
* 设置Empty点击事件
* @param listener
*/
public void setEmptyClickListener(final OnClickListener listener) {
if( listener!=null )
findViewById(R.id.state_retry2).setOnClickListener(listener);
}

/**
* 设置State点击事件
* @param listener
*/
public void setStateClickListener( OnClickListener listener ){
if(listener!=null)
findViewById(R.id.state_retry).setOnClickListener(listener);
}

/**
* 设置自定义布局的点击事件
* @param resoureId
* @param listener
*/
public void setViewOncClickListener(int resoureId,OnClickListener listener) {
findViewById(resoureId).setOnClickListener(listener);
}

/**
* 设置自定义布局的view文本
* @param resoureId
* @param text
*/
public void setViewText(int resoureId,String text){
((TextView)findViewById(resoureId)).setText(text);
}

/**
* 设置自定义布局的image
* @param resoureId
* @param img
*/
public void setViewImage(int resoureId,int img ){
((ImageView)findViewById(resoureId)).setImageResource(img);
}

/**
* State View
*/
public void showState() {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 0) {
child.setVisibility(VISIBLE);
} else {
child.setVisibility(GONE);
}
}
}

/**
* Empty view
*/
public void showEmpty() {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 2) {
child.setVisibility(VISIBLE);
} else {
child.setVisibility(GONE);
}
}
}


/**
* Loading view
*/
public void showLoading() {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 1) {
child.setVisibility(VISIBLE);
} else {
child.setVisibility(GONE);
}
}
}


/**
*
* @param text
*/
public void showLoading(String text) {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 1) {
child.setVisibility(VISIBLE);
((TextView) child.findViewById(R.id.loading_text)).setText(text + "");
} else {
child.setVisibility(GONE);
}
}
}


/**
* Empty view
*
* @param text
*/
public void showEmpty(String text) {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 2) {
child.setVisibility(VISIBLE);
((TextView) child.findViewById(R.id.empty_text)).setText(text + "");
} else {
child.setVisibility(GONE);
}
}
}


/**
*
* @param tips
*/
public void showState(String tips) {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 0) {
child.setVisibility(VISIBLE);
((TextView) child.findViewById(R.id.load_state_tv)).setText(tips + "");
} else {
child.setVisibility(GONE);
}
}
}

/**
* @param stateId
* @param tips
*/
public void showState(int stateId, String tips) {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i == 0) {
child.setVisibility(VISIBLE);
((ImageView) child.findViewById(R.id.load_state_img)).setImageResource(stateId);
((TextView) child.findViewById(R.id.load_state_tv)).setText(tips + "");
} else {
child.setVisibility(GONE);
}
}
}



/**
* 展示内容
*/
public void showContent() {
for (int i = 0; i < this.getChildCount(); i++) {
View child = this.getChildAt(i);
if (i > 2 ) {
child.setVisibility(VISIBLE);
} else {
child.setVisibility(GONE);
}
}
}
}

测试类LoadingLayoutActivity.java

package com.czhappy.effectdemo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.czhappy.effectdemo.R;
import com.czhappy.effectdemo.loadinglayout.LoadingLayout;

/**
* Description:
* User: chenzheng
* Date: 2017/2/17 0017
* Time: 18:05
*/
public class LoadingLayoutActivity extends AppCompatActivity implements View.OnClickListener{

private Button btn1, btn2, btn3;
private LoadingLayout mLoading;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1000){
mLoading.showContent();
}else if(msg.what==2000){
mLoading.showState("加载失败,点击重试!");
}else if(msg.what==3000){
mLoading.showEmpty();
}
}
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_layout);

initView();
}

private void initView() {
mLoading = (LoadingLayout) findViewById(R.id.loading_layout);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
mLoading.showContent();
mLoading.setStateClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLoading.showLoading();
mLoading.postDelayed(new Thread(){
@Override
public void run() {
super.run();
//模拟网络请求
mHandler.sendEmptyMessage(2000);
}
},1000);
}
});
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
mLoading.showLoading();
mLoading.postDelayed(new Thread(){
@Override
public void run() {
super.run();
//模拟网络请求
mHandler.sendEmptyMessage(1000);
}
},2000);
break;
case R.id.btn2:
mLoading.showLoading();
mLoading.postDelayed(new Thread(){
@Override
public void run() {
super.run();
//模拟网络请求
mHandler.sendEmptyMessage(2000);
}
},2000);
break;
case R.id.btn3:
mLoading.showLoading();
mLoading.postDelayed(new Thread(){
@Override
public void run() {
super.run();
//模拟网络请求
mHandler.sendEmptyMessage(3000);
}
},2000);
break;
}
}
}

正在加载的布局文件loading_layout.xml

至于empty_layout.xml和loadstate_layout.xml这些简单布局的代码就不贴出来了,根据需要自己定义。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:id="@+id/loading_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<com.pnikosis.materialishprogress.ProgressWheel
android:id="@+id/progress_wheel"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
wheel:matProg_barColor="#5588FF"
wheel:matProg_progressIndeterminate="true" />

<TextView
android:id="@+id/loading_text"
android:layout_width="wrap_content"
android:padding="5dp"
android:layout_height="wrap_content"
android:text=" 正在加载..."
android:textColor="#999999"
android:textSize="15sp" />
</LinearLayout>

三、注意事项

项目中实现正在加载的material design风格的进度动画,用的是开源库
​​​https://github.com/pnikosis/materialish-progress​​​
如果你有其他需求,可以自己替换

Android自定义view实现加载中、加载失败、无数据_自定义view_02


标签:layout,自定义,void,id,child,Android,加载,public,android
From: https://blog.51cto.com/u_4427045/6095981

相关文章

  • Android面试题汇总
    1.面试题:知道Service吗,它有几种启动方式?Service是一个专门在后台处理长时间任务的Android组件,它没有UI。它有两种启动方式,startService和bindService。startService只是......
  • Android图像处理实例解析
    一、概述本篇文章介绍的是关于Android图像处理相关的,主要有动态修改图像的色相、饱和度及亮度,颜色矩阵,图像像素点调整、图像矩阵实现图像的平移缩放等,Xfermode相关知识点,......
  • Android Studio 友盟api实现apk多渠道打包
    本篇主要给大家介绍利用友盟api实现Android多渠道打包,进入友盟的官网,注册账号,添加对应的应用。1.添加友盟库的依赖2.在manifest.xml中声明appkey,以及渠道占位符3.builde......
  • Android学习之双向滑动菜单
    MainActivity.java: packagecom.example.bidirslidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.Vie......
  • Android学习之左侧滑动菜单
    MainActivity.java:packagecom.example.slidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnCli......
  • Android从上到下抽屉式效果
    SlidingDrawerDemo.java: packageorg.lee.android;importorg.lee.android.ExpoInterpolator.EasingType;importorg.lee.android.ExpoInterpolator.ExpoInterpolator;imp......
  • Android学习之ExpandableListView
    ExpListActivity.java: packageorg.lee.android;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importandroid.app......
  • Android百度地图添加覆盖物(AndroidSDK_v2.4.1)
     使用网络功能所需权限:<!--使用网络功能所需权限--><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permission......
  • Xbox 自定义控制台和控制器抽奖活动 All In One
    Xbox自定义控制台和控制器抽奖活动AllInOnehttps://www.xbox.com/en-US/promotions/sweepstakes/disney-mandalorian-3-custom-console官方比赛规则1.赞助商这些......
  • Android病毒分析基础(二)—ChatGPT提问技巧
    今天我们来用最近比较火的“ChatGPT”学习一下Android病毒分析基础,看看和我们之前学的有什么不同,又能学习到什么新的东西,同时了解一下“ChatGPT”提问的一些精髓。和我们......