Objective
In most of our applications, the ListView is used, and the data of the ListView, sometimes from the network or time-consuming operation, is necessary to provide a friendly hint before the data is displayed, combined with the content of the Android combat page of the previous article to load the animation results, Combined with the properties of the ListView itself, we can achieve this effect. First See
Implementation of the ListView
The ListView itself has a setemptyview (view view); method, which is used to display a view when there is no data, and of course it is intended to be a view that is displayed when the data is 0 after loading, but we can also use it as a graph to display during loading.
Before using this method, we need to include this view in the layout file at the same level as the ListView. We do this using 58 of the same city load view from the previous article. As follows
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"tools:context="Cn.edu.zafu.emptyview.ListViewSampleActivity"> <listview android:id="@+id/listview"Android:layout_width="Match_parent"android:layout_height="Match_parent"></ListView> <CN. edu. Zafu. Emptyview. View. LoadingviewAndroid:id="@+id/loading"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_centerinparent="true"android:visibility="Invisible"> </CN. edu. Zafu. Emptyview. View. Loadingview></RelativeLayout>
The view can then be set by the Setemptyview method in the code.
ListView mListView = (ListView) findViewById(R.id.listview);LoadingView mLoadingView = (LoadingView) findViewById(R.id.loading);mListView.setEmptyView(mLoadingView);
The rest of the operating system will help you to complete, such as the display of the loaded view until the data is not available, the view will be hidden if there is data after the load is completed.
Implementation of the GridView
The GridView implementation is the same as the ListView, where the key code is affixed
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"tools:context="Cn.edu.zafu.emptyview.GridViewSampleActivity"> <gridview android:id="@+id/gridview"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:numcolumns="3"></GridView> <CN. edu. Zafu. Emptyview. View. LoadingviewAndroid:id="@+id/loading"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_centerinparent="true"android:visibility="Invisible"> </CN. edu. Zafu. Emptyview. View. Loadingview></RelativeLayout>
GridView mGridView = (GridView) findViewById(R.id.gridview);LoadingView mLoadingView = (LoadingView) findViewById(R.id.loading);mGridView.setEmptyView(mLoadingView);
The realization of Recyclerview
Compared to the ListView and the GridView, the implementation of the recyclerview is relatively more complicated, mainly Recyclerview did not provide Setemptyview method, but in fact, it is not difficult, As long as we inherit recyclerview to provide such a method, to achieve this effect, we first create a new class to inherit Recyclerview
PackageCn.edu.zafu.emptyview.view;ImportAndroid.content.Context;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/** * Created by Lizhangqu on 2015/6/20. * * Public class emptyrecyclerview extends recyclerview { PrivateView Memptyview; Public void Setemptyview(View Emptyview) {Memptyview = Emptyview; } Public Emptyrecyclerview(Context context) {Super(context); } Public Emptyrecyclerview(context context, AttributeSet attrs) {Super(context, attrs); } Public Emptyrecyclerview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); }}
Internally to hold a Emptyview object and provide the Setemptyview () method to the same invocation method as the ListView, but that's not enough because we don't know when the data will change, so we need an observer to notify our observers when the data changes, By implementing the corresponding logic in the observer to hide and display the Emptyview and the Recyclerview itself, then we will implement this observer
PrivateAdapterdataobserver Emptyobserver =NewAdapterdataobserver () {@Override Public void onChanged() {//callback when data changesadapter<?> Adapter = Getadapter ();if(Adapter! =NULL&& Memptyview! =NULL) {//If no data if(Adapter.getitemcount () = =0) {//Show Memptyview, hide itselfMemptyview.setvisibility (view.visible); Emptyrecyclerview. This. setvisibility (View.gone); }Else{//show itself, hide MemptyviewMemptyview.setvisibility (View.gone); Emptyrecyclerview. This. setvisibility (view.visible); } } } };
Then when to register the Observer, the best time is the Setadapter method, then we rewrite the method
@Override publicvoidsetAdapter(Adapter adapter) { super.setAdapter(adapter); ifnull) { adapter.registerAdapterDataObserver(emptyObserver); } emptyObserver.onChanged(); //一定要调用一下,通知观察者显示空View }
This also leads to the question of whether Emptyview is NULL in the Observer. The result is that before you set up the adapter, you must set the Emptyview, which is a good time to pay attention to the call, the code is as follows
mLoadingView = (LoadingView) findViewById(R.id.loading); mRecyclerView = (EmptyRecyclerView) findViewById(R.id.recyclerView); mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); //必须在调用设置适配器前调用setEmptyView mRecyclerView.setEmptyView(mLoadingView); mRecyclerView.setAdapter(new RecyclerViewAdapter(getApplicationContext(), mList));
A Emptyview Help class
In the project development process, will encounter many of the ListView, the ancient method is every encounter a ListView, in the XML file inside write Emptyview, and then add Emptyview, in fact, in many cases, Different ListView Emptyview is the same, for this, write a simple helper class, in the ListView need to add Emptyview, as long as the addition of a line of code can be implemented
Public classEmptyviewhelper {PrivateListView Mlistview;PrivateView Memptyview;PrivateContext Mcontext; Public Emptyviewhelper(ListView ListView) {Mlistview = ListView; Mcontext = Listview.getcontext (); Initemptyview (); } Public Emptyviewhelper(ListView ListView,intEmptyviewres) {Mlistview = ListView; Mcontext = Listview.getcontext (); Initemptyview (Emptyviewres); }Private void Initemptyview(intEmptyviewres) {Memptyview = View.inflate (Mcontext, Emptyviewres,NULL); ((ViewGroup) mlistview.getparent ()). AddView (Memptyview); Mlistview.setemptyview (Memptyview); }}
This helper class, gets the current ListView, then inflate writes the Emptyview in advance, then sets Emptyview, and then, when a ListView needs to add Emptyview, as long as the following line of code is OK
EmptyViewHelper emptyViewHelper = new EmptyViewHelper(mListview, R.layout.empty_view);
The implementation method is very simple, which has the following benefits
- Pre-written good emptyview, each encounter needs to set Emptyview, as long as a line of code can be implemented
- When the Emptyview needs to be changed, as soon as the Emptyview is changed, all Emptyview bound in the ListView are changed
SOURCE download
http://download.csdn.net/detail/sbsujjbcy/8824855
Combat Emptyview, load buffer for the ListView