片段隐藏并在点击时显示(Fragment Hide and show on click)
我有一个需要从活动中不断更新的片段,我只想在点击按钮时显示这个片段,点击相同的按钮片段应该被隐藏但仍然是活动的,所以我可以更新内容来自活动的片段(片段包含回收者视图)。 在隐藏和显示此片段时,我想为该事务设置动画。
**
工作守则
**
活动XML(包含片段的框架布局)
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> <fragment android:name="octave.foodster.fragment_cart" android:id="@+id/fragment_cart" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout>Working Code
**
Activity XML (the frame layout that contains the fragment)
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> <fragment android:name="octave.foodster.fragment_cart" android:id="@+id/fragment_cart" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout>The java code in the activity (i removed the animation for now)
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; case R.id.cart: Log.i("click", "menu"); showHideFragment(fragment); break; } return super.onOptionsItemSelected(item); } public void showHideFragment(final Fragment fragment) { final FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction(); fragTransaction.setCustomAnimations(R.anim.fragment_slide_from_right, R.anim.animation_leave); if (fragment.isHidden()) { fragTransaction.show(fragment); Log.d("hidden", "Show"); } else { fragTransaction.hide(fragment); Log.d("Shown", "Hide"); } fragTransaction.commit(); }And in the activities onCreate()--this is done to hide the fragment by default
fragmentManager = getSupportFragmentManager(); fragment = fragmentManager.findFragmentById(R.id.fragment_cart); final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.hide(fragment); fragmentTransaction.commit();最满意答案
在onClick函数中执行此功能,以便在单击按钮时显示片段隐藏
// Call this function inside onClick of button public void showHideFragment(final Fragment fragment){ FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if (fragment.isHidden()) { fragTransaction.show(fragment); Log.d("hidden","Show"); } else { fragTransaction.hide(fragment); Log.d("Shown","Hide"); } fragTransaction.commit(); }Follow this fuction in an onClick function to do show hide of fragment on clicking a button
// Call this function inside onClick of button public void showHideFragment(final Fragment fragment){ FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if (fragment.isHidden()) { fragTransaction.show(fragment); Log.d("hidden","Show"); } else { fragTransaction.hide(fragment); Log.d("Shown","Hide"); } fragTransaction.commit(); }更多推荐
发布评论