2022年4月6日 星期三

[Android] RecyclerView 瀑布流(方塊List 橫跨方式)


int columnCount = 3;
int spanCount = 2;
GridLayoutManager manager = new GridLayoutManager(context, columnCount,
    StaggeredGridLayoutManager.VERTICAL, false);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){

    @Override
    public int getSpanSize(int position) {
        if({判斷條件}){
           return spanCount;
        }
        return 1;
    }
});

deviceView.setLayoutManager(manager);

2018年11月15日 星期四

【開箱文】查克折合桌

礙於現在的租屋處實在是很小,所以買的傢俱希望都能有多功能,最近老婆大人又說希望有個餐桌,所以就從可以折合式的餐桌開始找。

而剛好,遇到雙11,在FB廣告中看到特X屋有在賣一組價格不會太貴的查克折合桌,所以就下定啦。

一組含桌子與兩張椅子不用兩千 ,只不過需要自己組裝。

2018年11月14日 星期三

【開箱文】米家IH 電子鍋 Part 1

不專業開箱又來了,這次開箱的玩具是小米賣的【米家電子鍋】。
號稱煮出來的飯又香又好吃,先來看看它的外觀。

很簡約的外箱,正正方方的。

2018年11月12日 星期一

[Android] WebView內嵌Youtube無法全螢幕的解決方式

開發WebView的時候,卻發現,網頁上如果有 Youtube的影片,可以正常播放,但卻無法全螢幕,找了許久,找到了解法。

2018年5月17日 星期四

【開箱文】小米空氣淨化器2S

收到台灣小米官網的到貨提醒,當下立即上網訂購 ,不到24小時,淨化器已經到手囉。


外箱包裝很簡單,正面提示你配件都放在淨化器裡面。


打開外箱,裡頭就是今天的主角。

2016年7月31日 星期日

Laravel 使用 GCS 做為儲存空間

前言
Laravel 有內建 Filesystem 套件,所以可以用來和例如 Amazon S3, Dropbox, Google Cloud Storage 等等服務串接

但找了一下發現和 Google Cloud Storage (GCS) 這方面的資料好像滿零散的
所以整理了一下

正文
GCS 使用的 API 和 S3 是相同的,但是目前只有 v2 有支援,所以需要安裝 v2 的 s3 相關套件,於 composer.json 的 require 加上

 { ...
 "league/flysystem-aws-s3-v2": "~1.0"
  }
 


然後 composer update 或是 install

2016年7月21日 星期四

Laravel Unit Test (上)

前言:
之所以寫這篇教學 ( 筆記 ),是因為希望執行 Unit Test 時可以有個單純的環境,且完整的測試 DB 設計 ( migration )是否正確
所以找了一下將開發環境和測試環境 DB 分離的方法

正文開始:

  1.   這邊使用 memory 形式的 SQLite 來做為 Laravel 與 PHPUnit 測試時的 DB 設定
    於 config/database.php 的 'connections' => [ … 設定 sql_testing 做為 Test 環境使用,如下範例

2016年6月26日 星期日

iOS 模擬器 couldn't be completed. (LaunchServicesError ... 解法



如果有使用 CocoaPods 的 XCode 專案,有時候會遇到一些鬼打牆的問題
例如 無緣無故忽然間就出現什麼 /bin/sh exit code 1
或是

The operation couldn't be completed. (LaunchServicesError error 0.)



Command /bin/sh failed with exit code 1

還是不能 copy 什麼 framework,出現 exit code 之類的

都有可能是專案沒有 Clean 乾淨 ( 一般的 Product -> Clean 可能無效)
這時候要按 Alt ,再選一次 Product -> Clean Build Folder ( 隱藏功能)
就有可能變正常了


2016年5月19日 星期四

Laravel 客制化登入頁面



這邊使用Laravel內建的User 資料表,可以先加上一些欄位
於 database/migrations/xxxx_create_users_table.php
加上 account 做示範,例如:

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('account’); // !!
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

然後migrate db, User 的Model fillable 也要填寫

先建立認證用的Controller,這邊叫User
於終端機輸入
php artisan make:controller UserController

2016年5月10日 星期二

[Android]Android Studio Error:Execution failed for task ':app:mergeDebugResources'. > String index out of range: 0

剛剛在編輯文字檔(strings.xml)時,因為先打了一堆的<string name=""></string>,想說直接輸入name跟value值就好了。
結果多餘的<string name=""></string>我既然忘記刪除啦~~

執行專案Android Studio就跑出這樣的錯誤了。如果你也遇到了,快去檢查你是否跟我遇到同樣的問題呢?
Error:Execution failed for task ':app:mergeDebugResources'. > String index out of range: 0

[Android] Android Studio Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

最近使用Android Studio做專案時,忽然出現以下錯誤

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72300Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72321Library UP-TO-DATE
:app:prepareComAndroidSupportCustomtabs2321Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2300Library UP-TO-DATE
:app:prepareComAndroidSupportMediarouterV72300Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72320Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72320Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42321Library UP-TO-DATE
:app:prepareComFacebookAndroidFacebookAndroidSdk4110Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServices840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAds840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppstate840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAuth840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase840Library UP-TO-DATE

2016年4月29日 星期五

如何使用Google的圖形驗證碼(reCAPTCHA)防止機器人


前言


因為MyAndroid這個網站也有一段時間了,開放留言就算有圖形驗證碼還是保受留言機器人騷擾之苦… 索性直接關閉所有人的留言功能
等到有空再來自幹一個防止機器人的驗證程序,但這個過程一拖就是好多年了 (平日上班太忙)
直到最近想說,乾脆就整合Google的圖形驗證碼吧

順便做個教學


正文


2016年4月22日 星期五

Android 強制顯示鍵盤

之前在實作有關EditText的頁面時,總覺得要強制顯示鍵盤,很困難,叫它出現不出現,真難控制。

後來才瞭解,原來畫面還沒準備好,就去呼叫強制顯示鍵盤,所以Android根本不理我。
所以必須先讓他等個0.1秒,這樣就可以成功呼叫啦。

final ImputMethodManager imm =
    (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

editText.postDelayed(new Runnable(){
    @Override
    public void run(){
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }
}, 100);


如果要隱藏鍵盤就比較單純了。
ImputMethodManager imm =
    (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder ib = Activity.this.getCurrentFocus().getWindowToken();
imm.hideSoftInputFromWindow(ib, InputMethodManager.HIDE_NOT_ALWAYS);

2015年10月2日 星期五

〔飲食〕在台北角落遇見『台北角落』

昨日在一如往常的下午茶時刻,走在車來人往的承德路上看見不一樣的風景;讓我多注意一眼。


有一台經過精心改造的三輪車後面載著不明物體

2015年9月29日 星期二

簡單介紹Swift 2.0的Guard語法

Guard是iOS在Swift 2.0中引入的一種語法
他改善了一些冗長的if寫法問題(下面範例會提到)
一般來說 guard要撘配else來使用,最好的用法(官方建議)是用在方法(method)中的return / break / continue 等流程控制

Swift的guard語法非常適合用來做optional的判斷
(概念上也可以把guard直接當做 if not 來看)

以下是簡單的範例:


// userName是個optional的值,可能是使用者輸入,或是網路來的
var userName:String?

func checkUserExists(name:String?)->Bool{
// 這裡用 底線(_) 是因為我們不是真的要值,只是想判斷optional是否有值
guard let _ = userName else{
return false
}
return true
}

checkUserExists(userName) // 輸出 false
userName = "Seachaos" // 給予userName有真正的值
checkUserExists(userName) // 輸出 true



上面範列的checkUserExists就是個簡單的例子

再來看一個滿常用到的範例,就是Dictionary (不管是Swift的Dictionary還是 NSDictionary 都適用) 的處理

2015年9月24日 星期四

Android DatePickerDialog Bug


android 內建的 DatePickerDialog 中


  • onDateSet 這個 method 會自動跑兩次,要加 view.isShown 來判斷


private DatePickerDialog.OnDateSetListener startDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerview,intyear,intmonthOfYear,intdayOfMonth) {
if(view.isShown()) {
//onDateSet() 會跑兩次,需要判斷是否已出現
}
}
  • DatePickerDialog background 後面會出現另一個圖層
要加入 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(color.TRANSPARENT));


Android LaunchMode


Activity 在 singleTask 模式下,若再以

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

的方式喚起 Activity,將無法收到新的 Bundle

必須


@Override
protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent);
     setIntent(intent);
     //now getIntent() should always return the last received intent
}

此 Activity 的生命週期為

1.onNewIntent
    
2.onRestart

3.onStart

4.onResume

2015年9月22日 星期二

iOS測量文字長度, 以Swift和Objective-C為例

iOS中,除了用UILabel的SizeToFit來測量文字長度外
NSString其實也有內建提供這個方法

以下是Objective-C的範例:
+ (CGSize)getSizeFromString:(NSString*)str withFont:(UIFont*)font{

    return [str sizeWithAttributes:@{

                                     NSFontAttributeName : font

                                     }];

}


這個method只要輸入NSString和UIFont,就可以得到String的尺吋(以CGSize回傳,有該String將要顯示的width或是height)

而Swift要測量的話,要先轉成NSString,且AttributteName也有所修改
如下
func getSizeFromString(string:String, withFont font:UIFont)->CGSize{

        let textSize = NSString(string: string ?? "").sizeWithAttributes(

            [ NSFontAttributeName:font ])

        return textSize

}

2015年9月20日 星期日

XCode 7 遇到 Bitcode 錯誤的解決辦法


最近Apple Release了 XCode 7
如果在開發iOS的APP 有遇到類以下的錯誤:



ld: '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/usr/lib/dylib1.o' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

那就是遇到了BITCODE的問題了

解決辦法很簡單,就是關掉他,方法如下圖
(到專案的 Build Settings ,搜尋 bitcode)
BitCode是什麼呢?

他是一個有點像是Java Bytecode一樣的東西
但他是給LLVM (Apple的編譯器)用的,簡單的說就是Apple希望你可以在上傳到App Store時也一起上傳Bitcode,好處就是App Store可以幫你針對不同的 iOS 設備去優化你的App,不用重新上傳或是針對CPU做編譯

但會出現以上的錯誤可能就是因為用了沒有Bitcode的Library(通常是C++或是第三方的套件)
所以XCode在產生Bitcode就會失敗了
目前看來Bitcode比較適用於純 Swift 或是Objective-C的專案



2015年9月16日 星期三

Mac安裝PostgreSQL與簡單教學

PostgreSQL和MySQL一樣都是一種SQL Server
以下有些教學是可以提供給會MySQL,也想使用PostgreSQL的使用者看看

第零步,安裝PostgreSQL

使用brew來安裝 (Linux的話就看是apt / yum 還是什麼的,以下以Mac為例)

brew install postgresql

成功的話,會看到最後有段訊息

To have launchd start postgresql at login:
  ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
  postgres -D /usr/local/var/postgres


主要的重點是說,你想要一登入Mac的使用者,就自動啟動PostgreSQL的話,就在終端機輸入


ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

要手動啟動 (第一次裝完最好也輸入一次)

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

如果想要停止的話輸入

postgres -D /usr/local/var/postgres




第一步,建立Database
createdb 是 postgresql 建立資料庫的指令