DataLoadingLayoutAndroid

简介:A custom view for data loading status on Android

Description

DataLoadingLayout is a simple library for Android. It's a layout to show current data loading status(loading, load success,load error)

Screenshots

Usage

Step 1:Add the dependency:

open the build.gradle file of your module, at the dependencies function add the below code:

    compile 'com.lvleo:data-loading-layout:0.1.1'

Step 2:Add the view to your layout.xml file:

<com.lvleo.dataloadinglayout.DataLoadingLayout
        android:id="@+id/loading_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_buttons"
        android:background="@android:color/darker_gray"
        app:loadingBarColor="@android:color/holo_purple"
        app:statusText="暂无数据"
        app:statusTextColor="@android:color/black"
        app:statusTextSize="16sp"
        />

Step 3: The activity implements DataLoadingLayout.OnViewTouchListener, after init the view setDataView(view) and setOnMyViewTouchListener(this),final Override the onTouchUp() function. like this:

public class MainActivity extends AppCompatActivity implements DataLoadingLayout.OnViewTouchListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private Button btnNoData, btnDataLoadingSuccess, btnDataLoadingError;

    private TextView txtResult;

    private DataLoadingLayout mLoadingLayout;

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

        btnNoData = (Button) findViewById(R.id.btn_data_empty);
        btnDataLoadingSuccess = (Button) findViewById(R.id.btn_data_success);
        btnDataLoadingError = (Button) findViewById(R.id.btn_data_error);

        txtResult = (TextView) findViewById(R.id.txt_result);

        mLoadingLayout = (DataLoadingLayout) findViewById(R.id.loading_layout);

        mLoadingLayout.setDataView(txtResult);
        mLoadingLayout.setOnMyViewTouchListener(this);

        btnNoData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData("http://api.map.baidu.com/telematics/v3/weather?location=");
            }
        });
        btnDataLoadingSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData("http://api.map.baidu.com/telematics/v3/weather?location=无锡");
            }
        });
        btnDataLoadingError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData("http://api.map.baidu.com/telematics/v3/weathersssss?location=无锡");
            }
        });

        mLoadingLayout.loadSuccess("Demo 演示");
    }

    @Override
    public void onTouchUp() {
        // if data load Error, can get data again by touch the view

        getData("http://api.map.baidu.com/telematics/v3/weather?location=无锡");

    }

    private void getData(String subUrl) {
        mLoadingLayout.loading();

        String url = subUrl + "&output=json&ak=6gYxFLrG9vipiq1bkQLnHhUH&" +
                "mcode=4C:45:2B:FC:13:89:0F:76:88:A8:D3:9F:69:F6:51:9C:BC:F6:9E:65;baidumapsdk.demo";

        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(20 * 1000);
        client.setConnectTimeout(20 * 1000);
        client.setResponseTimeout(20 * 1000);
        client.setMaxRetriesAndTimeout(3, 20 * 1000);
        client.allowRetryExceptionClass(SocketTimeoutException.class);
        client.allowRetryExceptionClass(ConnectTimeoutException.class);
        client.get(url, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.i(TAG, "onSuccess: response==" + response);
                if (response.optInt("error") == 0) {
                    mLoadingLayout.loadSuccess();

                    JSONObject object = response.optJSONArray("results").optJSONObject(0).
                            optJSONArray("weather_data").optJSONObject(0);

                    String weather = "今日天气\r\n" + object.optString("date") + "\r\n 温度:" +
                            object.optString("temperature") + "\r\n 风向:" + object.optString("wind");

                    txtResult.setText(weather);

                } else {
                    mLoadingLayout.loadSuccess("暂无数据,\n 点击屏幕 重新加载 ");
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);

                mLoadingLayout.loadError("服务器连接失败,\n 点击屏幕 重新加载");

            }
        });
    }
}

XML attributes

NameTypeDefaultDescription
statusTextSizedimension14the status string's font size
statusTextColorcolorColor.GRAYthe status string's color
statusTextstringNo Datathe status default content
loadingBarColorcolorColor.BLUEthe ProgressBar's color
loadingBarSizedimension48the ProgressBar's size
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值