如何处理View
中的空数据RecyclerView
,我已经尝试过很多来自互联网的方法,但似乎都没有。我也在使用领域数据库,所以我不知道这是否是检查它是否为空的正确方法。
这是Fragment
xml RecyclerView
所在的位置
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_no_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:text="emptty"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:visibility="invisible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_favorite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
&#13;
这是我的片段类
public class FavouriteFragment extends Fragment {
RecyclerView mRecyclerView;
FavouriteAdapter adapter;
Realm mRealm;
int positions;
TextView emptyText;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.favourite_fragment,container,false);
mRealm=Realm.getDefaultInstance();
RealmQuery<news> quotesRealmQuery = mRealm.where(News.class).equalTo("favourite",true);
RealmResults<News> mResults = newsRealmQuery.findAll();
emptyText= (TextView) view.findViewById(R.id.tv_no_data);
//adapter=new FavouriteAdapter(getActivity(),mResults,mRealm);
mRecyclerView= (RecyclerView) view.findViewById(R.id.rv_favorite);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
if (!mResults.isEmpty()) {
//if data is available, don't show the empty text
emptyText.setVisibility(View.INVISIBLE);
adapter=new FavouriteAdapter(getActivity(),mResults,mRealm,pos,single);
mRecyclerView.setAdapter(adapter);
mRealm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm element) {
adapter.notifyItemRemoved(positions);
}
});
} else
emptyText.setVisibility(View.VISIBLE);
return view;
}
答案 0 :(得分:3)
您的RecyclerView不透明,因此您应该记得在空白时隐藏它:
if (!mResults.isEmpty()) {
//if data is available, don't show the empty text
emptyText.setVisibility(View.INVISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);
} else {
emptyText.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.GONE);
}