片段隐藏并在点击时显示(Fragment Hide and show on click)

编程入门 行业动态 更新时间:2024-10-15 00:24:34
片段隐藏并在点击时显示(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(); }

更多推荐

本文发布于:2023-08-07 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   片段   Fragment   click   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!