Skip to content

🔥 An elegant and highly expandable android library for displaying different state of app like loading, empty, error and successful state.

Notifications You must be signed in to change notification settings

hust201010701/XPlaceHolder

Repository files navigation

XPlaceHolder

What is XPlaceHolder?

🔥 An elegant and highly expandable android library for displaying different state of app like loading, empty, error and successful state.

Features

  • 👍 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.

ScreenShots

The following screenshots are respectively:

  1. All demos
  2. activity
  3. Fragment
  4. fragment + ViewPager
  5. View
  6. used as common widget in xml

20180906153616828034829.png2018090615361682989840.gif20180906153616830825231.gif20180906153616831752910.gif20180906153616832942007.gif20180906153616833871218.gif

Have a Try

Download xplaceholder-demo.apk.

Getting Started

We need to take 3 steps to show placeholder for activty or view.

1. Add library

Add below code to your build.gradle: `compile 'com.orzangleli:xplaceholder:1.0.0'``

2. Configuring custom layout

Different apps have different fail, empty and loading layout.So you need to configure your own placeholder layout.

1. Create your own Entity class for placeholder

You can add some variables in your class.

For more details, you can refer ImageAndTextPlaceHolderVo.java

2. Implements IPlaceHolderLayout

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.

3. Attach activty or view to placeholder layout

  • 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 .

How does XPlaceHolder works?

This section will be added soon.

Support

  • Issues

About

🔥 An elegant and highly expandable android library for displaying different state of app like loading, empty, error and successful state.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages