Skip to content

Commit

Permalink
Merge pull request XRecyclerView#48 from mahuafactory/master
Browse files Browse the repository at this point in the history
支持setEmptyView
  • Loading branch information
jianghejie committed Mar 17, 2016
2 parents 98a19b6 + ed1c1d1 commit bc4eb79
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
android:name=".StaggeredGridActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".EmptyViewActivity"
android:label="@string/app_name" >
</activity>
</application>

</manifest>
66 changes: 66 additions & 0 deletions app/src/main/java/com/example/xrecyclerview/EmptyViewActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.xrecyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

public class EmptyViewActivity extends AppCompatActivity {
private XRecyclerView mRecyclerView;
private MyAdapter mAdapter;
private ArrayList<String> listData;
private View mEmptyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mRecyclerView = (XRecyclerView)this.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);

mEmptyView = findViewById(R.id.text_empty);

mRecyclerView.setEmptyView(mEmptyView);

//没有数据,触发emptyView
listData = new ArrayList<String>();
mAdapter = new MyAdapter(listData);

mRecyclerView.setAdapter(mAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


}
6 changes: 6 additions & 0 deletions app/src/main/java/com/example/xrecyclerview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public void gotoStaggeredGridActivity(View v) {
startActivity(intent);
}

public void gotoEmptyViewActivity(View v) {
Intent intent = new Intent();
intent.setClass(this, EmptyViewActivity.class);
startActivity(intent);
}

}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@
android:text="StaggeredGridLayoutManager"
/>

<Button
android:onClick="gotoEmptyViewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="emptyViewSupport"
/>

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_recyclerview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<TextView
android:gravity="center"
android:id="@+id/text_empty"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#dcdcdc"
android:text="emptyView"
android:visibility="gone"/>
</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class XRecyclerView extends RecyclerView {
private static final int TYPE_FOOTER = -3;
private int previousTotal = 0;
private int mPageCount = 0;
//adapter没有数据的时候显示,类似于listView的emptyView
private View mEmptyView;

public XRecyclerView(Context context) {
this(context, null);
Expand Down Expand Up @@ -152,12 +154,22 @@ public void setArrowImageView(int resid) {
}
}

public void setEmptyView(View emptyView) {
this.mEmptyView = emptyView;
mDataObserver.onChanged();
}

public View getEmptyView() {
return mEmptyView;
}

@Override
public void setAdapter(Adapter adapter) {
mAdapter = adapter;
mWrapAdapter = new WrapAdapter(mHeaderViews, mFootViews, adapter);
super.setAdapter(mWrapAdapter);
mAdapter.registerAdapterDataObserver(mDataObserver);
mDataObserver.onChanged();
}

@Override
Expand Down Expand Up @@ -279,7 +291,26 @@ private boolean isOnTop() {
private final RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
mWrapAdapter.notifyDataSetChanged();
Adapter<?> adapter = getAdapter();
if (adapter != null && mEmptyView != null) {
int emptyCount = 0;
if (pullRefreshEnabled) {
emptyCount++;
}
if (loadingMoreEnabled) {
emptyCount++;
}
if (adapter.getItemCount() == emptyCount) {
mEmptyView.setVisibility(View.VISIBLE);
XRecyclerView.this.setVisibility(View.GONE);
} else {
mEmptyView.setVisibility(View.GONE);
XRecyclerView.this.setVisibility(View.VISIBLE);
}
}
if (mWrapAdapter != null) {
mWrapAdapter.notifyDataSetChanged();
}
}

@Override
Expand Down Expand Up @@ -420,7 +451,7 @@ public int getItemViewType(int position) {
if(isFooter(position)){
return TYPE_FOOTER;
}
int adjPosition = position - getHeadersCount();;
int adjPosition = position - getHeadersCount();
int adapterCount;
if (adapter != null) {
adapterCount = adapter.getItemCount();
Expand Down

0 comments on commit bc4eb79

Please sign in to comment.