2015年12月30日 星期三

Android-音量控制

參考資訊 : [多媒体] 音量调节
參考資訊 : [索引汇总帖] 【eoeAndroid社区索引】android设备功能之音量教程实例汇总
參考資訊 : How to capture key events from bluetooth headset with android

Android-MediaPlayer 音樂播放


import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class MainActivity extends Activity {

    private MediaPlayer mediaPlayer;

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

        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer = MediaPlayer.create(this, R.raw.lucky_star);
            mediaPlayer.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

    }
}


參考資訊 : 使用多媒體播放器(MediaPlayer)播放MP3音樂
參考資訊 : 音樂播放器(MediaPlayer)

2015年12月26日 星期六

Android-ListView 和 CheckBox 問題

不知道是否有大大遇過類似以下情況 :
比如有 30 筆資料
點選第 1 筆後
竟然在 2X 筆突然出現被勾選的狀態
 If you check first checkbox and slide down, Actually the twentieth checkbox
was checked.

解決辦法 :
1 . 把被點選的位置記錄下來
2 . 生成 CheckBox 時去判斷是否要被勾選

import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{

    private HashMap<Integer, Boolean> hashMap = new HashMap<Integer,Boolean>();
    .
    .
    .
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Elements elements = null;  

        if (convertView == null) {  
            .
            .
            .
        } else {  
            .
            .
            .
        }  
        
        elements.checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {  
                    hashMap.put(position, isChecked);
                }else{
                    hashMap.remove(position);
                }                
            }});
        elements.checkBox1.setChecked(hashMap.get(position)==null? false : true);
        
        return convertView; 
    }

}



參考資料 : ListView点击checkbox其他checkbox也被同时选中的问题

Android-Copy project and rename package

If you  want to create new project form the project ( A -> A' )
You must :
1 . rename package name
2 . AndroidManifest registered activity、service、package... must change package name
3 .Change def packageName on build.gradle
4 . rebuild project

2015年12月21日 星期一

Android-文字 轉 語音 TTS


import java.util.Locale;

import android.content.Context;
import android.speech.tts.TextToSpeech;

public class MyTTS implements TextToSpeech.OnInitListener{

    private Context context;
    private TextToSpeech mTextToSpeech = null;
    private boolean isLoaded = false;

    // initTTS
    public MyTTS(Context context){
        try {
            this.context = context;
            this.mTextToSpeech = new TextToSpeech(context, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) 
        {
            mTextToSpeech.setLanguage(Locale.US);
            isLoaded = true;
        } 
        else 
        {
            isLoaded = false;
        }        
    }

    // stop action about tts
    public void close(){
        if(mTextToSpeech != null)
        {
            mTextToSpeech.stop(); 
            mTextToSpeech.shutdown();
        }
    }
    
    public void queueSpeak(String text) {
        if (isLoaded)
            mTextToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
    }

    public void flushSpeak(String text) {
        if (isLoaded)
            mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    
}


參考資料 : Android TextToSpeech Example

Android-防丟器為例













在還沒開始前,想必大家對 Bluetooth LE Scanner 不陌生吧
在開始編寫前可以先拿上方的應用程式操作看看




檢查手機版本和有無開啟藍芽
/** 初始化 */
public boolean initialize() {
    // For API level 18 and above, get a reference to BluetoothAdapter through
    // BluetoothManager.
    if (bluetoothManager == null) {
        bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (bluetoothManager == null) {
            Log.e(TAG, "Unable to initialize BluetoothManager.");
            return false;
        }
    }
    bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null) {
        Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
        return false;
    }
    return true;
}

裝置連線
/** 連線至藍芽裝置 */
public boolean connect(final String address) {
    if (bluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && bluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (bluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }
    final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    bluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

與裝置離線
public void disconnect() {
    if (bluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    bluetoothGatt.disconnect();
}

關閉藍芽
public void close() {
    if (bluetoothGatt == null) {
        return;
    }
    Log.w(TAG, "mBluetoothGatt closed");
    mBluetoothDeviceAddress = null;
    bluetoothGatt.close();
    bluetoothGatt = null;
}

連線後的回調
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            Log.i(TAG, "Attempting to start service discovery:" +
                bluetoothGatt.discoverServices());
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            disconnect();
            close();
        }
        broadcastUpdate(intentAction);
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
                enableNotification();
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic,
            int status) {
        Log.w(TAG, "onCharacteristicRead received: " + characteristic.getValue());
        
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic) {
        Log.w(TAG, "onCharacteristicChanged received: " + characteristic.getValue());
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        Log.w(TAG, "onCharacteristicWrite received: ");
    }
};

新增服務提醒
public void enableNotification()
{
    // is support SERVICE 
    BluetoothGattService Service = bluetoothGatt.getService(SERVICE_UUID);
    if (Service == null) {
        Log.w(TAG, "service not found!");
        return;
    }
    // is support Characteristic
    BluetoothGattCharacteristic Char = Service.getCharacteristic(CHARACTERISTIC_UUID);
    if (Char == null) {
        Log.w(TAG, "Tx charateristic not found!");
        return;
    }
    bluetoothGatt.setCharacteristicNotification(Char,true);
    BluetoothGattDescriptor descriptor = Char.getDescriptor(CCCD);
    final byte[] ENABLE_NOTIFICATION_VALUE__ = {0x01};
    descriptor.setValue(ENABLE_NOTIFICATION_VALUE__);
    bluetoothGatt.writeDescriptor(descriptor);
}

取得所有服務
for(BluetoothGattService bluetoothGattService: gatt.getServices())
{
    Log.w(TAG, "----------------------------------");
    Log.w(TAG, "BluetoothGattService : " + bluetoothGattService.getUuid() );
    for(BluetoothGattCharacteristic bluetoothGattCharacteristic: bluetoothGattService.getCharacteristics())
    {
        Log.w(TAG, "BluetoothGattCharacteristic : " + bluetoothGattCharacteristic.getUuid() );
        for(BluetoothGattDescriptor bluetoothGattDescriptro: bluetoothGattCharacteristic.getDescriptors())
        {
            Log.w(TAG, "BluetoothGattDescriptor : " + bluetoothGattDescriptro.getUuid());
            if(bluetoothGattDescriptro.getValue() != null)
                Log.w(TAG, "BluetoothGattDescriptor : " + bluetoothGattDescriptro.getValue());                
            
        }
    }
}

參考資料 : AndroidとBLE
參考資料 : NordicSemiconductor/Android-nRF-Toolbox
參考資料 : BLE Health Devices: First Steps with Android
參考資料 : Bluetooth Low Energy (or LE) is a very cool technology.
參考資料 : SensorTag User Guide_Android

2015年12月18日 星期五

Android-判斷手機 網路 藍芽 狀態

手機裝置狀態
Check Cellphone State : Bluetooth、NetWork

權限
<!-- 藍芽 -->
<uses-permission android:name="android.permission.BLUETOOTH" />

<!-- 網路 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
 * 裝置狀態
 */
public class DeviceState {

    /** 網路是否連線 */
    public static boolean isNetWorkConnection(Context context){

        boolean isConnection = false;

        ConnectivityManager mConnectivityManager = 
                (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

        if(mNetworkInfo != null)
            if(mNetworkInfo.isConnected())
                isConnection = true;

        return isConnection;
    }

    /** 裝置是否支援低功率藍芽 */
    public static boolean isSupportBle(){

        boolean isSupport = false;

        BluetoothAdapter mBluetoothAdapter = 
                BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter != null) 
            isSupport = true;

        return isSupport;
    }

}

參考資料 : 【Android】檢查網路連線狀態 Connectivity Network Active State

歡迎轉載,請註明出處。

2015年12月17日 星期四

Android-RecyclerView Sample 範例 Kotlin

更新日期:20189116 新增範例( kotlin )

雖然有 ListView 和 GridView 顯示資訊
但還要另外寫選配器
Google 又另外推出 RecyclerView
聞說效率比前 2 個 View 效率更高
話不多說

先來個簡單流程說明吧
1 . 首先設計要顯示表格的畫面內容
2 . 將剛剛設計的內容格式放置選配器內
3 . 將資料放置選配器中
4 . 依照不同的需求顯示

直接來個範例

1 . build.gradle 先匯入以下
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'

2 . 新增元素介面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/item_record_1_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/item_record_1_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

3 . 設定頁面參數
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.light.blue.counter.R;

public class MyViewHolder_1 extends RecyclerView.ViewHolder {

    public ImageView mImageView;
    public TextView mTextView;

    public MyViewHolder_1(View view) {
        super(view);
        // 添加預設的模組
        mImageView = view.findViewById(R.id.item_record_1_img);
        mTextView = view.findViewById(R.id.item_record_1_text);
    }
}


4 . 設定選配器
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.light.blue.adpater.view.MyViewHolder_1;
import com.light.blue.counter.R;

import java.util.List;

public class MyAdapter_1 extends RecyclerView.Adapter<MyViewHolder_1> {

    private List<String> mList;

    public MyAdapter_1(List<String> list) {
        mList = list;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder_1 holder, int position) {
        holder.mTextView.setText(mList.get(position).toString());
    }

    @Override
    public MyViewHolder_1 onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_record_1, viewGroup, false);

        return new MyViewHolder_1(view);
    }

}

5 . 在主畫面宣告
<?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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView_recyclerList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

6 .
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.light.blue.adpater.MyAdapter_1
import com.light.blue.sql.until.MyDatabaseHandler
import kotlinx.android.synthetic.main.activity_recycleview.*
import java.util.*

class RecycleListActivity : AppCompatActivity() {

    private lateinit var myDatabaseHandler: MyDatabaseHandler;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycleview)

        myDatabaseHandler = MyDatabaseHandler(this);

        var listStr = ArrayList<String>();
        listStr.add("小明");
        listStr.add("小王");
        listStr.add("小黃");
        listStr.add("小黑");
        listStr.add("小白");

        recycleView_recyclerList.layoutManager = LinearLayoutManager(this);
        // recycleView_recyclerList.layoutManager = GridLayoutManager(this, 3);
        recycleView_recyclerList.adapter = MyAdapter_1(listStr);

    }

}


( LinearLayoutManager )






















(GridLayoutManager)




參考資料 : Creating Lists with RecyclerView in Android
參考資料 : RecyclerView使用详解(一)

2015年12月16日 星期三

Android-Create New Project

想必各位大大心目中有一些想實踐的App
但要如何開始?
建議 : 不要一想到就直接開始動工
不然後繼無力就很容易失去原有的方向

不做申論只需簡單問自己
然後以最簡短的一句話寫在紙上

首先,先分析 3W1H !!!
1 . Why?
為甚麼要寫此 App?

2 . Who?
此 App 使用對象為?

3 . What?
未來展望?想要的目的?

4 . How?
要如何做?

如果此問題都能回答出來
因該就算是踏出第一步了
--------------------------------------------------
接下來就是實作分析
1 . Functional Map
2 . Flow
3 . UI design

Android-水波紋特效 ripple

20170311 新增:ImageButton has ripple effect

Google 在 5.0 有新增一些特效
最明顯的例子 : 按鍵有水波紋特效
至於能不能讓使用者接受
只能見仁見智了

1 . 在專案內 新增 drawable-v21
2 . 用 ripple 包覆元素
3 . 物件套用剛剛客製的 drawable

注意 : 最小 SDK 版本為 21

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white">
    <item android:drawable="@drawable/ic_l"/>
</ripple>

參考資訊 : Lollipop RippleDrawable vs Selector for Pre-Lollipop
參考資訊 : How to Add Ripple Effect/Animation to a Android Button
參考資料:Apply Material Design Touch Ripple to ImageButton?

--------------------------------------------------------
如果手機版本比較低可以參考 :
參考資訊 : material-ripple
參考資訊 : RippleEffect
參考資訊 : 內有連結

2015年12月14日 星期一

Android-Take Photo and save file

1 . 新增權限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2 . 設定 RequestCode
public static final int TAKE_PHOTO_IMAGE_CODE = 1234;

3 . 開啟相機
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PHOTO_IMAGE_CODE);    

4 . 接收拍照結果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PHOTO_IMAGE_CODE) 
    {
        //Check if data in not null 
        if (data != null)
        {
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");
            imageView.setImageBitmap(bitmap);
        
        }
    }
}

5 . 如果要有儲存照片功能
public static final String FileName = "Hello";

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Check that request code matches ours:
    if (requestCode == TAKE_PHOTO_IMAGE_CODE) 
    {
        // 建立資料夾
        File imageStorageFolder = 
            new File(Environment.getExternalStorageDirectory()+File.separator + FileName);
            
        if (!imageStorageFolder.exists())
        {
            imageStorageFolder.mkdirs();
            Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
        }
        //Check if data in not null 
        if (data != null)
        {
            String imageName = "image";
            String fileNameExtension = ".jpg";
            File sdCard = Environment.getExternalStorageDirectory();
            String strImageStorageFolder = File.separator + FileName + File.separator;
            File destinationFile = 
                new File(sdCard, strImageStorageFolder + imageName + fileNameExtension);
            Log.d(TAG, "the destination for image file is: " + destinationFile );
            if (data.getExtras() != null)
            {
                Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                //判斷照片為橫向或者為直向,並進入ScalePic判斷圖片是否要進行縮放
                if(bitmap.getWidth()>bitmap.getHeight())
                    ScalePic(bitmap,mPhone.heightPixels);
                else 
                    ScalePic(bitmap,mPhone.widthPixels);
                try
                {
                    FileOutputStream out = new FileOutputStream(destinationFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                } 
                catch (Exception e) 
                {
                    Log.e(TAG, "ERROR:" + e.toString());
                }
                
            }
        }
    }
}

private void ScalePic(Bitmap bitmap,int phone)
{
    //縮放比例預設為1
    float mScale = 1 ;
    //如果圖片寬度大於手機寬度則進行縮放,否則直接將圖片放入ImageView內
    if(bitmap.getWidth() > phone )
    {
        //判斷縮放比例
        mScale = (float)phone/(float)bitmap.getWidth();
        Matrix mMat = new Matrix() ;
        mMat.setScale(mScale, mScale);
        Bitmap mScaleBitmap = Bitmap.createBitmap(bitmap,
                0,
                0,
                bitmap.getWidth(),
                bitmap.getHeight(),
                mMat,
                false);
        imageView.setImageBitmap(mScaleBitmap);
    }
    else imageView.setImageBitmap(bitmap);
}


參考資料 : Android : Use Camera Activity for Thumbnail and FullSize Image
參考資料 : 【Android】開啟相機-照相功能與相簿-相片集並讀取相片匯入至程式

歡迎轉載,請註明出處。

Java-時間 日期 增減


DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, 3);
String date = dateFormat.format(calendar.getTime());
System.out.println(date);

歡迎轉載,請註明出處。

2015年12月13日 星期日

Android-廣告 滑動頁面 ViewPager + PagerAdapter(一)

更新日期 : 20160116 新增索引點
更新日期 : 20160217 切換頁面監聽更改

相信各位在安裝 App 時候一定有引導教學左右滑動頁面
經過測試後
其實跟我們常用的 ListView 、GridView 大同小異
如果對 BaseAdapter 熟悉的朋友
一定很快就能上手
注意 : 要使用此功能,必須匯入 V4 包

順序
1 . 抓出畫面共同點!? ( 有些App會在最後一頁有 Button ,可以先隱藏 )
2 . 設計畫面UI
3 . 設計選配器
4 . 將 選配器放進 ViewPager ( 像 ListView )內

這次的 Sample 以單純圖片顯示

1 . cell_pager_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:scaleType="fitXY"
        android:layout_gravity="right" />
</LinearLayout>

2 . MyPagerAdapter.java
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

public class MyPagerAdapter extends PagerAdapter {
    
    private Context context;
    private List<Integer> list;
    private LayoutInflater inflater;

    public MyPagerAdapter(Context context, List<Integer> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            
        // 佈局
        View itemView = inflater
            .inflate(R.layout.cell_pager_image, container, false);

        // 佈局元件內容
        ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView);
        imageView.setImageResource(list.get(position));

        // 加載
        (container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // LinearLayout 是以 cell_pager_image 主體變更
        container.removeView((LinearLayout) object);
    }

}

3 . activity_main.xml (將設定好的選配器設置到 ViewPager 上
<?xml version="1.0" encoding="utf-8"?>
<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">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>


4 . MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements ViewPager.OnPageChangeListener{

    ViewPager viewPager;
    MyPagerAdapter myPagerAdapter;

    // 在專案內放的測試圖
    private static final int[] pictures = { R.drawable.icon_01,
            R.drawable.icon_02, R.drawable.icon_03 };

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

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        List<Integer> list = new ArrayList<Integer>();
        for(int i : pictures){
         list.add(i);
        }

        myPagerAdapter = new MyPagerAdapter(this, list);
        viewPager.setAdapter(myPagerAdapter);

    }

}


如果要對頁面設定監聽器
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

參考資料 : Viewpager setonpagechangelistener deprecated

有關於底下原點,必須要在MainActivity設定
簡單的說必須配合監聽換頁
如果有時間會在寫一些 Sample


參考資料 : Android ViewPager Gallery Images and Texts Tutorial
參考資料 : Android通用圆点指示器——android-Universal-CircleIndicator
參考資料 : ViewPagerIndicator 分頁指示器

參考資料 : VIEW PAGER WITH CIRCULAR INDICATOR (WITHOUT ANY LIBRARY)

歡迎轉載,請註明出處。

2015年12月7日 星期一

Android-DatePickerDialog 範例 Sample

官方日期選擇器

1 . 實作 DatePickerDialog
Calendar calendar0 = Calendar.getInstance();
DatePickerDialog dialog0 = new DatePickerDialog(this, this,
        calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH),
        calendar0.get(Calendar.DAY_OF_MONTH));
dialog0.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
dialog0.show();

2 . 實作 DatePickerDialog
....implements implements DatePickerDialog.OnDateSetListener{ ... }

3 . 接回參數
// CallBack
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
                      int dayOfMonth) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    Log.d("DateTest","Date : " + format.format(new Date()));
    
}

歡迎轉載,請註明出處。