我的RecycleView上的布局上有更改视图的问题。我有列表的梦想。这个视图包含两个状态myDreamEmptyLayout女巫显示文字"现在你没有梦想。 \ n添加梦想!"当myDreamList为空且size等于0.但当myDreamList大小超过1时显示列表梦。 问题是当myDream在列表中并删除时,所有人都无法显示我的视图myDreamEmptyLayout。 什么问题?
这是我的fragment_my_dream.xml
<?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"
android:background="@color/colorBackground"
>
<LinearLayout
android:id="@+id/myDreamEmptyLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/for_now_you_don_t_have_dream_n_add_dream"
android:textColor="@color/colorWhite"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/myDreamRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
这是我的片段类
public class MyDreamFragment extends Fragment {
public static final int REQUEST_CODE_CREATE = 1;
@BindView(R.id.myDreamRecyclerView)
RecyclerView myDreamRecyclerView;
@BindView(R.id.myDreamEmptyLayout)
LinearLayout myDreamEmptyLayout;
private MyDreamAdapter myDreamAdapter;
private List<MyDream> myDreamList;
String idUserString;
private DbHelper dbHelper;
private Unbinder unbinder;
private Dao<MyDream, Integer> myDreamDao;
private Dao<User, Integer> myUserDao;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my_dream, container, false);
unbinder = ButterKnife.bind(this, rootView);
TextView title = (TextView) getActivity().findViewById(R.id.toolbarTextView);
title.setText(R.string.title_my_dreams);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Application application = (Application) getApplicationContext();
idUserString = application.getFirebaseId();
dbHelper = new DbHelper(getActivity());
try {
myDreamDao = dbHelper.getDao(MyDream.class);
} catch (SQLException e) {
e.printStackTrace();
}
try {
myUserDao = dbHelper.getDao(User.class);
} catch (SQLException e) {
e.printStackTrace();
}
QueryBuilder<User, Integer> userQa = myUserDao.queryBuilder();
try {
userQa.where().eq(User.Columns.FIREBASE_ID, idUserString);
} catch (SQLException e) {
e.printStackTrace();
}
QueryBuilder<MyDream, Integer> myDreamQa = myDreamDao.queryBuilder();
try {
myDreamList = myDreamQa.join(userQa).query();
} catch (SQLException e) {
e.printStackTrace();
}
if (myDreamList != null && myDreamList.size() > 0) {
myDreamRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myDreamAdapter = new MyDreamAdapter(myDreamList, getActivity());
myDreamRecyclerView.setAdapter(myDreamAdapter);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_add, menu);
}
@Override
public void onResume() {
super.onResume();
if (myDreamList.size() == 0) {
myDreamEmptyLayout.setVisibility(View.VISIBLE);
myDreamRecyclerView.setVisibility(View.GONE);
} else {
myDreamEmptyLayout.setVisibility(View.GONE);
myDreamRecyclerView.setVisibility(View.VISIBLE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_addream:
Intent intent = new Intent(getActivity(), AddEditDreamActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == AddEditDreamActivity.RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data != null) {
MyDream myDream = data.getParcelableExtra("dream");
myDreamAdapter.addMyDream(myDream);
Toast.makeText(getActivity(), "Dream was added", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
答案 0 :(得分:0)
您的myDreamList
不会对待处理的更改作出反应,因此myDreamEmptyLayout
不会获知新信息(例如大小已达零)可用。要反驳这一情况,请查看this ObservableArrayList及其OnListChangedCallback。将myDreamList
更改为ObservableArrayList
移动onResume
代码(当然没有super
)更改为OnListChangedCallback
并通过addOnListChangedCallback
添加此回调。这样,对列表所做的更改将根据需要反映在布局上。
代码:
//this goes to onCreate. DISCLAIMER : code written without testing, please confirm that it works.
myDreamList.addOnListChangedCallback(new ObservableList.OnListChangedCallback() {
/*snipped useless methods*/
@Override
public void onChanged(ObservableList<T> sender) {
if (sender.size() == 0) {
myDreamEmptyLayout.setVisibility(View.VISIBLE);
myDreamRecyclerView.setVisibility(View.GONE);
} else {
myDreamEmptyLayout.setVisibility(View.GONE);
myDreamRecyclerView.setVisibility(View.VISIBLE);
}
}
}