我的問題是在我的RecycleView上佈局上的更改視圖。我有列表夢想。這個視圖包含兩個狀態myDreamEmptyLayout witch show Text「現在你沒有夢想了。\ n添加夢想!」當myDreamList爲空並且大小爲等於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();
}
}