Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

ViewSwitcher - Android Widgets Example

ViewSwitcher - Android Widgets สำหรับ ViewSwitcher เป็น Widget ใช้สำหรับ แสดงมุมมองของ Widgets ระหว่าง 2 ตัว โดยจะทำงานสลับกันไปมาระหว่าง มุมมอง (View) หรือ Widgets ที่อยู่ภายใต้ ViewSwitcher นั้น ๆ ส่วนถ้ากรณีมากกว่า 2 ตัวให้ใช้ ViewAnimator หรือ ViewFlipper เพราะใน Widgets 3-4 ตัวนี้การทำงานจะคล้าย ๆ กัน แต่ถ้าได้ใช้งานจริง ๆ จะเข้าใจเลยว่า จะมีความสามารถบางตัว ที่มีไม่เหมือนกันและวัตถุประสงค์ก็ต่างกันด้วยเช่นเดียวกัน

ViewSwitcher - Android Widgets

XML Syntax
    <ViewSwitcher
        android:id="@+id/viewSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Example 1 การใช้ ViewSwitcher ควบคุมการแสดงผล Widgets ของ ImageView แบบง่าย ๆ

ViewSwitcher - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<RelativeLayout 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" >

        <LinearLayout
            android:id="@+id/LinearLayout02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="75dp" >

        	<ViewSwitcher
           		android:id="@+id/viewSwitcher1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

		            <ImageView
		            android:id="@+id/imageView1"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:src="@drawable/pic_a" />
		            <ImageView
		            android:id="@+id/imageView2"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:src="@drawable/pic_b" />		            	            		            
                
            </ViewSwitcher>
            
        </LinearLayout>

</RelativeLayout>


XML Layout ที่ได้ออกแบบไว้

ViewSwitcher - Android Widgets

จากรูปจะเห็นว่า imageView1 และ imageView2 อยู่ภายใต้ ViewSwitcher ซึ่งเมื่อตอนทำงาน ViewSwitcher จะแสดงผลสลับไปสลับมาระหว่าง imageView1 และ imageView2

MainActivity.java (Java Code)
package com.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

	Button next;
    Button previous;
    ViewSwitcher vs;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
    	Animation inAnim = new AlphaAnimation(0, 1);
    	inAnim.setDuration(2000);
    	Animation outAnim = new AlphaAnimation(1, 0);
    	outAnim.setDuration(2000);

    	vs.setInAnimation(inAnim);
    	vs.setOutAnimation(outAnim);
    	
    	vs.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	vs.showNext();
            }
        });
      
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
}


Code ที่เป็น Java

    	vs.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	vs.showNext();
            }
        });

จะเห็นว่า ViewSwitcher มีการกำหนด Event ที่เป็น OnClick และมี Property ชื่อว่า showNext(); เพื่อแสดงมุมมองถัดไป และในกรณีที่จะมีแค่ 2 มุมมอง ก็จะเป็นการแสดงข้อมูลสลับไปสลับมา

Screenshot

ViewSwitcher - Android Widgets

แสสดงมุมมอง (View) แรก

ViewSwitcher - Android Widgets

เมื่อคลิกก็จะเปลี่ยนการแสดงผลเป็นมุมมอง (View) สอง




Example 2 การใช้ ViewSwitcher ควบคุมการแสดงผลมุมมอง (View) ที่เป็น TableLayout โดยมี tableLayout1 และ tableLayout2 และภายในแต่ล่ะ TableLayout ก็จะประกอบด้วย Widgets ต่าง ๆ ที่แต้สำคัญคือใน tableLayout1 จะมี ProgressBar ซึ่งวัตถประสงค์ก็คือ เราต้องการให้แสดง tableLayout1 ที่มี ProgressBar ซึ่งจะแสดงการทำงานหรือ Loading ประมาณ 5 วินาที จากนั้นค่อยแสดงผล tableLayout2

ViewSwitcher - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewSwitcher1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    	
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout1"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

		<ProgressBar
		    android:id="@+id/progressBar1"
		    style="?android:attr/progressBarStyleLarge"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content" />

     </TableLayout>
     
	
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout2"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Welcome to My App"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_a"
            android:layout_marginTop="10dp"  />
        
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="CopyRight 2012 www.ThaiCreate.Com"
            android:textAppearance="?android:attr/textAppearanceSmall" />   
            
        <Button
		    android:id="@+id/button1"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="Reload again" />
            
    </TableLayout>

</ViewSwitcher>


จาก XML Layout จะเห็นว่า tableLayout1 และ tableLayout2 อยู่ภายใต้ ViewSwitcher

MainActivity.java (Java Code)
package com.myapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

    ViewSwitcher Vs;
    private static final int REFRESH_SCREEN = 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // viewSwitcher1
        Vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
        startScan();
        
        // Reload again
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Vs.showPrevious();
            	startScan();
            }
        });
        	
    }
    
	public void startScan() {
		new Thread() {
			public void run() {					
				try{			
					Thread.sleep(5000);
					hRefresh.sendEmptyMessage(REFRESH_SCREEN);
				}catch(Exception e){
				}
			}
		}.start();
	}
	
	@SuppressLint("HandlerLeak")
	Handler hRefresh = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case REFRESH_SCREEN:
				Vs.showNext();
				break;
			default:
				break;
			}
		}
	};

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
}


ในการทำงานจะใช้ Thread Sleep เพื่อหยุดการทำงานของมุมมอง (View) tableLayout1 แรก ประมาณ 5 วินาที จากนั้นค่อยแสดงผล tableLayout2

Screenshot

ViewSwitcher - Android Widgets

กำลังแสดง ProgressBar ซึ่งจะทำงานประมาณ 5 วินาที

ViewSwitcher - Android Widgets

แสดงมุมมองที่สอง






   
Share

Property & Method (Others Related)

Android Transitions Widgets
ImageSwitcher - Android Widgets Example
AdapterViewFlipper - Android Widgets Example
StackView - Android Widgets Example
TextSwitcher - Android Widgets Example
ViewAnimator - Android Widgets Example
ViewFlipper - Android Widgets Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-01 16:24:26 / 2012-07-15 15:01:05
  Download : No files
 Sponsored Links / Related

 
Android Custom Adapter
Rating :

 
Android People Contact List, Name, Phone No, Photo Picture, Email and Address
Rating :

 
Android Rating (Vote) and ListView Part 1
Rating :

 
Android Rating (Vote) and ListView Part 2 (Member Login and Average Rating)
Rating :

 
Android PhoneGap (jQuery Mobile) Create Convert App from Website(URL)
Rating :

 
Android Capture Image and Camera Capture Screenshot (android.view.SurfaceView)
Rating :

 
Android Pull Down to Refresh And Release to Refresh or Update (Part 1)
Rating :

 
Android Pull Down to Refresh And Release to Update (Part 2 , PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่