Skip to content

Commit

Permalink
add setEmptyView methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadCym committed Apr 26, 2016
1 parent 649c964 commit 3ae67bc
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
</activity>
<activity android:name=".HeaderAndFooterUseActivity">
</activity>
<activity android:name=".SectionUerActivity">
<activity android:name=".SectionUseActivity">
</activity>
<activity android:name=".MultipleItemUseActivity">
</activity>
<activity android:name=".EmptyViewUseActivity">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.chad.baserecyclerviewadapterhelper;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.baserecyclerviewadapterhelper.adapter.QuickAdapter;

public class EmptyViewUseActivity extends Activity {
private RecyclerView mRecyclerView;
private QuickAdapter mQuickAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty_view_use);
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
initAdapter();
}

private void initAdapter() {
mQuickAdapter = new QuickAdapter(this, 0);
View emptyView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);
mQuickAdapter.setEmptyView(emptyView);
mRecyclerView.setAdapter(mQuickAdapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class HomeActivity extends Activity {
private static final Class<?>[] ACTIVITY = {AnimationUseActivity.class, MultipleItemUseActivity.class, HeaderAndFooterUseActivity.class, PullToRefreshUseActivity.class, SectionUerActivity.class};
private static final String[] TITLE = {"Animation Use", "MultipleItem Use", "HeaderAndFooter Use", "PullToRefresh Use", "Section Use"};
private static final Class<?>[] ACTIVITY = {AnimationUseActivity.class, MultipleItemUseActivity.class, HeaderAndFooterUseActivity.class, PullToRefreshUseActivity.class, SectionUseActivity.class, EmptyViewUseActivity.class};
private static final String[] TITLE = {"Animation Use", "MultipleItem Use", "HeaderAndFooter Use", "PullToRefresh Use", "Section Use", "EmptyView Use"};
private ArrayList<HomeItem> mDataList;
private RecyclerView mRecyclerView;

Expand All @@ -38,7 +38,6 @@ public void onItemClick(View view, int position) {
}
});
mRecyclerView.setAdapter(homeAdapter);

}

private void initData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class SectionUerActivity extends Activity implements BaseQuickAdapter.OnRecyclerViewItemClickListener {
public class SectionUseActivity extends Activity implements BaseQuickAdapter.OnRecyclerViewItemClickListener {
private RecyclerView mRecyclerView;
private List<MySection> mData;
@Override
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_empty_view_use.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".EmptyViewUseActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"/>

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_section_uer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".SectionUerActivity">
tools:context=".SectionUseActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/empty_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data"
android:textColor="@android:color/black"/>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">BaseRecyclerViewAdapterHelper</string>
<string name="title_activity_header_and_footer_use">HeaderAndFooterUseActivity</string>
<string name="no_data">There is no data</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerV
protected static final int HEADER_VIEW = 0x00000111;
protected static final int LOADING_VIEW = 0x00000222;
protected static final int FOOTER_VIEW = 0x00000333;
protected static final int EMPTY_VIEW = 0x00000555;
private View mHeaderView;
private View mFooterView;
/**
* View to show if there are no items to show.
*/
private View mEmptyView;

@Deprecated
public void setOnLoadMoreListener(int pageSize, RequestLoadMoreListener requestLoadMoreListener) {
Expand Down Expand Up @@ -125,6 +130,7 @@ public BaseQuickAdapter(Context context, int layoutResId, List<T> data) {
this.mContext = context;
this.mLayoutResId = layoutResId;
}

public BaseQuickAdapter(Context context, List<T> data) {
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
this.mContext = context;
Expand Down Expand Up @@ -153,17 +159,27 @@ public int getFooterViewsCount() {
return mFooterView == null ? 0 : 1;
}

public int getmEmptyViewCount() {
return mEmptyView == null ? 0 : 1;
}

@Override
public int getItemCount() {
int i = mNextLoadEnable ? 1 : 0;
return mData.size() + i + getHeaderViewsCount() + getFooterViewsCount();
int count = mData.size() + i + getHeaderViewsCount() + getFooterViewsCount();
if (count == 0) {
count += getmEmptyViewCount();
}
return count;
}


@Override
public int getItemViewType(int position) {
if (mHeaderView != null && position == 0) {
return HEADER_VIEW;
} else if (mEmptyView != null && getItemCount() == 1) {
return EMPTY_VIEW;
} else if (position == mData.size() + getHeaderViewsCount()) {
if (mNextLoadEnable)
return LOADING_VIEW;
Expand All @@ -187,6 +203,8 @@ public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new HeadViewHolder(mHeaderView);
} else if (viewType == FOOTER_VIEW) {
return new FooterViewHolder(mFooterView);
} else if (viewType == EMPTY_VIEW) {
return new EmptyViewHolder(mEmptyView);
} else {
return onCreateDefViewHolder(parent, viewType);
}
Expand Down Expand Up @@ -218,6 +236,13 @@ public ContentViewHolder(View itemView) {
}
}

public static class EmptyViewHolder extends BaseViewHolder {

public EmptyViewHolder(View itemView) {
super(itemView.getContext(), itemView);
}
}


public void addHeaderView(View header) {
if (header == null) {
Expand All @@ -236,6 +261,24 @@ public void addFooterView(View footer) {
this.notifyDataSetChanged();
}

/**
* Sets the view to show if the adapter is empty
*/
public void setEmptyView(View emptyView) {
mEmptyView = emptyView;
}

/**
* When the current adapter is empty, the BaseQuickAdapter can display a special view
* called the empty view. The empty view is used to provide feedback to the user
* that no data is available in this AdapterView.
*
* @return The view to show if the adapter is empty.
*/
public View getEmptyView() {
return mEmptyView;
}

public void isNextLoad(boolean isNextLoad) {
mNextLoadEnable = isNextLoad;
mLoadingMoreEnable = false;
Expand Down Expand Up @@ -286,6 +329,8 @@ public void onClick(View v) {

} else if (holder instanceof HeadViewHolder) {

} else if (holder instanceof EmptyViewHolder) {

} else {
int index = position - getHeaderViewsCount();
BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
Expand Down

0 comments on commit 3ae67bc

Please sign in to comment.