Android 개발 의 ViewSwitcher 사용법 실례

5696 단어 AndroidViewSwitcher
이 사례 는 안 드 로 이 드 개발 의 ViewSwitcher 용법 을 다 루 었 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
android.widget.ViewSwitcher 는 ViewAnimator 의 하위 클래스 로 두 View 사 이 를 전환 하 는 데 사용 되 지만 매번 하나의 View 만 표시 할 수 있 습 니 다.
ViewSwitcher 의 addView 함수 코드 는 다음 과 같 습 니 다.

/**
 * {@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);
}

이 를 통 해 알 수 있 듯 이 View 의 수량 이 두 개 를 초과 하면 이상 을 던 집 니 다.java.lang.IllegalState Exception,"Can't add more than 2 views to a ViewSwitcher"를 인쇄 합 니 다.ViewSwitcher 의 factory 를 사용 하여 View 를 만 들 거나 자신 이 만 든 View 를 추가 할 수 있 습 니 다.
다음은 하나의 예 를 들 어 ViewSwitcher 의 용법 을 소개 합 니 다.
레이아웃 파일:activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       tools:context=".MainActivity" >
  <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" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="- Button 2 -" />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="LinearLayout 2" />
    </LinearLayout>
  </ViewSwitcher>
</LinearLayout>

Activity 코드:

package com.example.AndroidTest;
import android.app.Activity;
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 MyActivity extends Activity {
  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) findViewById(R.id.viewswitcher);
    slide_in_left = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_in_left);
    slide_out_right = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_out_right);
    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();
      }
    });
    ;
  }
}

구현 효과 도:

ViewSwitcher 의 setFactory 설정 을 사용 하여 전환 하 는 View 를 두 단계 로 나 눕 니 다.
STEP 1:ViewSwithcer 의 인 스 턴 스 획득

switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);

2 부:인터페이스 ViewFactory 실현

switcher.setFactory(new ViewFactory()
{
  @Override
  public View makeView()
  {
    return inflater.inflate(R.layout.slidelistview, null);
  }
});

더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기