在过去的几个小时中,我一直在尝试为RecyclerView安装一个emptyview。我发现的解决方案涉及创建新类,并将空视图作为XML中的recyclerview放置在该级别上。我理解后者,但是我还没有看到用于SQLite数据库的Java解决方案。我使用过的尝试将放入空白视图,但随后不会显示recyclerview信息。有人可以帮我解决这个问题吗?
这是我的活动:
public class DisplayActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor>{
private static final int PRESCRIPTION_LOADER = 0;
private CustomCursorAdapter mAdapter;
RecyclerView mRecyclerView;
private static final String TAG = "MainTagName";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ViewGroup emptyView = findViewById(R.id.empty_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
mAdapter = new CustomCursorAdapter(this);
mRecyclerView.setAdapter(mAdapter);
if (mAdapter.getItemCount()==0) {
mRecyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}else {
mRecyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
//Kick off the loader
getLoaderManager().initLoader(PRESCRIPTION_LOADER,null,this);
//….
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
//….
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
Log.i(TAG,"Loader Manager Loading Finished: " + data.getCount());
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
这是我的RecyclerView适配器:
public class CustomCursorAdapter extends
RecyclerView.Adapter<CustomCursorAdapter.MyTaskViewHolder>{
private Cursor mCursor;
private Context mContext;
public CustomCursorAdapter (Context mContext){
this.mContext = mContext;
}
@Override
public MyTaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item, parent, false);
return new MyTaskViewHolder (view);
}
@Override
public void onBindViewHolder(CustomCursorAdapter.MyTaskViewHolder holder, int position) {
mCursor.moveToPosition(position);
//….
}
@Override
public int getItemCount() {
if (mCursor ==null){
return 0;
}
return mCursor.getCount();
}
public Cursor swapCursor(Cursor c) {
if (mCursor == c){
return null;
}
Cursor temp = mCursor;
this.mCursor = c;
if (c != null){
this.notifyDataSetChanged();
}
return temp;
}
class MyTaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//….
这是我的RecyclerView XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.cloiud.DisplayActivity"
tools:showIn="@layout/activity_display">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<include layout="@layout/empty_view"/>
这是我的emptyview XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="center">
//....
答案 0 :(得分:0)
编辑:我想出了一个解决方案。我最终将其他代码放入了我的加载器。
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
Log.i(TAG,"Loader Manager Loading Finished: " + data.getCount());
if (mAdapter.getItemCount()== 0) {
mRecyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}else {
mRecyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
}
我不确定它是否正确,但是它的运行方式类似于listview的emptyview。