視圖切換(ViewSwitcher)使用

image.png

目錄

ViewSwitcher

ViewSwitcher顧名思義. ViewSwitcher主要應(yīng)用場景之一:比如在一個(gè)布局文件中,根據(jù)業(yè)務(wù)需求,需要在兩個(gè)View間切換,在任意一個(gè)時(shí)刻,只能顯示一個(gè)View.

ViewSwitcher本身繼承了 FrameLayout,因此可以將多個(gè)View 層疊在一起,每次只顯示一個(gè)組件。當(dāng)程序控制從一個(gè)View切換到另一個(gè)View時(shí), ViewSwitcher支持指定動(dòng)畫效果。

值得注意的是ViewSwitcher最多只能有2個(gè)view.
ViewSwitcher的addView函數(shù)的代碼如下:

/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if this switcher already contains two children
 */
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
  if (getChildCount() >= 2) {
    throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
  }
  super.addView(child, index, params);
}

當(dāng)view超過2時(shí),就會報(bào)錯(cuò).

使用:

    <ViewSwitcher
        android:id="@+id/viewswitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="251dp"
        android:layout_marginBottom="145dp" >

        <ImageView
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:src="@drawable/p001" />

        <ImageView
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:src="@drawable/p002" />

    </ViewSwitcher>

使用實(shí)例

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/prev"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="previous" />

        <Button
            android:id="@+id/next"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="next" />
    </LinearLayout>

    <ViewSwitcher
        android:id="@+id/viewswitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="251dp"
        android:layout_marginBottom="145dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

        <ImageView
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:src="@drawable/p001" />

        <ImageView
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:src="@drawable/p002" />

    </ViewSwitcher>

</android.support.constraint.ConstraintLayout>

代碼實(shí)現(xiàn):

package com.example.user.viewsw;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {
    Button buttonPrev, buttonNext;
    ViewSwitcher viewSwitcher;
    Animation slide_in_left, slide_out_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 獲取按鈕
        buttonPrev = (Button) findViewById(R.id.prev);
        buttonNext = (Button) findViewById(R.id.next);

        // 獲取ViewSwitcher
        viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);

        // 載入動(dòng)畫效果
        slide_in_left = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left);
        slide_out_right = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right);

        // 設(shè)定動(dòng)畫效果
        viewSwitcher.setInAnimation(slide_in_left);
        viewSwitcher.setOutAnimation(slide_out_right);

        buttonPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                viewSwitcher.showPrevious();
            }
        });

        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                viewSwitcher.showNext();
            }
        });
    }
}

運(yùn)行效果:


image.png

image.png

參考

Android零基礎(chǔ)入門第54節(jié):視圖切換組件ViewSwitcher
android使用ViewSwitcher實(shí)現(xiàn)視圖切換

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容