StateDataLayout
I intended to write a lib, but turn out it can’t be.
It should be called a architecture to solving the problem than a lib, because it require a lot of constraints. What’s problem ? I’m not in the mood to talk. (If I like it, I’ll do it – as Tùng Mountain said)
Demo Github link: https://github.com/allenatwork/StateDataLayout
A Layout that change state base on response from server.
Layout State:
- loading
- error
- no data
- have data
Usage
-
Initialize Data layout
In your onCreateView in your fragment, put the following code to intilize StateDataLayout:
stateDataLayout= new StateDataLayout<>(this, inflater); stateDataLayout .setLayoutRes(getLayoutRes(), R.layout.nodata_layout, R.layout.loading_layout, R.layout.error_layout);
When you start call API, you’ll want to show loading:
stateDataLayout.bindLoading();
When API response, if success and data return, pass the data into layout:
stateDataLayout.bindData(data);
And if it error, pass the error message string into layout:
stateDataLayout.bindError(mes);
-
Define Response Object.
Your response object must implement interface
public interface GetInfoData { boolean hasData(); }
and sure you have to implemnt hasData() method. Which define the returned data is valid or not (Valid mean has data ?)
-
In your controller class, here is Fragment
You must implement controller interface:
public interface ControllerTask<K> { void reload(); void displayData(K data); }
K is the class type of response data we said above.
sure you will have to implement two method:
– reload: reload data
– displayData (K data): with the data type K, you (actually) bind it in your view.
That’s it. So simple ,right ?