FrameLayout的子类ViewAnimator

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 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);
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_34383316/article/details/76696132