ViewAnimator
public class ViewAnimator
extends FrameLayout
FrameLayout容器的基類,用於在其視圖之間切換時執行動畫。
我們都瞭解FrameLayout佈局的特性,在FrameLayout中添加的View都默認位於左上角,按照添加的順序,最後添加的View位置最上層。
ViewAnimator的使用就是調整ViewAnimator包裹的子View的顯示層次,可以通過setDisplayedChild(int whichChild)方法,設置哪一個子View將被顯示。
佈局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_main_layout">
<ViewAnimator
android:id="@+id/sample_output"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
<ScrollView
style="@style/Widget.SampleMessageTile"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="@style/Widget.SampleMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/horizontal_page_margin"
android:paddingRight="@dimen/horizontal_page_margin"
android:paddingTop="@dimen/vertical_page_margin"
android:paddingBottom="@dimen/vertical_page_margin"
android:text="@string/intro_message" />
</ScrollView>
<fragment
android:name="com.example.android.common.logger.LogFragment"
android:id="@+id/log_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ViewAnimator>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<FrameLayout
android:id="@+id/sample_content_fragment"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0px" />
</LinearLayout>
menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AppCompatResource">
<item android:id="@+id/menu_toggle_log"
android:showAsAction="always"
android:title="Show Log" />
</menu>
Activity
public class MainActivity extends SampleActivityBase {
// Whether the Log Fragment is currently shown
private boolean mLogShown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SwipeRefreshListFragmentFragment fragment = new SwipeRefreshListFragmentFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);
//R.id.sample_output對應的組件是否是ViewAnimator或其子類的一個實例,若是,menu_toggle_log,顯示
logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);
//logFragment顯示的狀態不同顯示不同字符
logToggle.setTitle(mLogShown ? Hide Log: Show Log);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//用於修改menu_toggle_log顯示的字符串及ViewAnimator顯示的view
switch(item.getItemId()) {
case R.id.menu_toggle_log:
mLogShown = !mLogShown;
ViewAnimator output = (ViewAnimator) findViewById(R.id.sample_output);
if (mLogShown) {
//顯示LogFragment
output.setDisplayedChild(1);
} else {
//顯示user guide
output.setDisplayedChild(0);
}
//使Activity的選項菜單無效。
//這將導致菜單的相關演示文稿通過在下一次請求菜單時
//通過調用onCreateOptionsMenu和onPrepareOptionsMenu進行完全更新。
supportInvalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
}