🔥 An elegant and highly expandable android library for displaying different state of app like loading, empty, error and successful state.
- 👍 Support multiple states such as loading, empty, error, successful state.
- 👍 Less invasion of the business code.
- 👍 Reduce layout levels as much as possible.
- 👍 Support activity, fragment, view, and view in xml.
- 👍 Support set which state can be clicked?
- 👍 Support for customizing different state layouts.
- 👍 Support ViewPager + Fragment.
- 👍 Expand Easily.
The following screenshots are respectively:
- All demos
- activity
- Fragment
- fragment + ViewPager
- View
- used as common widget in xml
Download xplaceholder-demo.apk.
We need to take 3 steps to show placeholder for activty or view.
Add below code to your build.gradle
: `compile 'com.orzangleli:xplaceholder:1.0.0'``
Different apps have different fail, empty and loading layout.So you need to configure your own placeholder layout.
You can add some variables in your class.
For more details, you can refer ImageAndTextPlaceHolderVo.java
You need to implements all methods of IPlaceHolderLayout
. Be careful, bindView
is not be called when initializing, bindView
is be call when the first time you show empty, error, or loading layout.So when you override bindState
, you need to check whether view is null.
For more details, you can refer ImageAndTextPlaceHolderLayout.java.
- In activity, you can add code like this:
mImageAndTextPlaceHolderLayout = new ImageAndTextPlaceHolderLayout(this);
mImageAndTextPlaceHolderLayout.setPlaceHolderVo(new ImageAndTextPlaceHolderVo.Builder()
.setLoadingImageResource(R.drawable.icon_loading)
.setEmptyImageResource(R.drawable.icon_empty)
.setErrorImageResource(R.drawable.icon_error)
.setLottieFileName("lottie/lego_loader.json")
.setLoadingText(getString(R.string.loading_tip))
.setEmptyText(getString(R.string.empty_tip))
.setErrorText(getString(R.string.error_tip))
.setEnableLottie(true)
.build());
XPlaceHolderUtil.attach(this, mImageAndTextPlaceHolderLayout, this);
- In fragment, in
onCreateView
you need to return placeholder layout not your original view, like this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container, false);
...
mImageAndTextPlaceHolderLayout = new ImageAndTextPlaceHolderLayout(this.getContext());
ImageAndTextPlaceHolderVo imageAndTextPlaceHolderVo = new ImageAndTextPlaceHolderVo.Builder()
.setLoadingText("loading...")
.setLoadingImageResource(R.drawable.icon_loading)
.setEmptyText("empty data")
.setEmptyImageResource(R.drawable.icon_empty)
.setErrorText("load error, click to retry")
.setErrorImageResource(R.drawable.icon_error)
.build();
mImageAndTextPlaceHolderLayout.setPlaceHolderVo(imageAndTextPlaceHolderVo);
XPlaceHolderUtil.attach(view, mImageAndTextPlaceHolderLayout, this);
// 默认只有错误页面可以点击重试,可以设置
mImageAndTextPlaceHolderLayout.setAvailableStateForClick(new State[]{State.EMPTY, State.ERROR});
// 注意这里不再是返回之前的view
return mImageAndTextPlaceHolderLayout;
}
- In view, just use
XPlaceHolderUtil.attach
mTextView = findViewById(R.id.textView);
mImageAndTextPlaceHolderLayout = new ImageAndTextPlaceHolderLayout(this);
ImageAndTextPlaceHolderVo imageAndTextPlaceHolderVo = new ImageAndTextPlaceHolderVo.Builder()
.setLoadingText("loading...")
.setLoadingImageResource(R.drawable.icon_loading)
.setEmptyText("empty data")
.setEmptyImageResource(R.drawable.icon_empty)
.setErrorText("load error")
.setErrorImageResource(R.drawable.icon_error)
.build();
mImageAndTextPlaceHolderLayout.setPlaceHolderVo(imageAndTextPlaceHolderVo);
XPlaceHolderUtil.attach(mTextView, mImageAndTextPlaceHolderLayout, this);
- Used as common widget Define in xml:
<com.orzangleli.xplaceholder.placeholder.ImageAndTextPlaceHolderLayout
android:id="@+id/placeHolderLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#0aa"
app:layout_constraintTop_toTopOf="parent"
/>
Set placeholder layout state in Java:
mImageAndTextPlaceHolderLayout = this.findViewById(R.id.placeHolderLayout);
mImageAndTextPlaceHolderLayout.showEmpty();
For more details, you can refer ImageAndTextPlaceHolderLayout Demos .
This section will be added soon.
- Issues