2019年11月30日 星期六

[Android] Android Studio 的 product flavor soruce set 目錄無法執行新增 package 操作

markdown 在 Android Studio 中可以新增 product flavor 來定義不同的 Build Variant,例如我新增的 ad 跟 noAd 兩個 product flavor,就會有四個 Build Variant adDebug、adRelease、noAdDebug 與 noAdRelease。 接著照 [Android 官網的文件](https://developer.android.com/studio/build/build-variants#sourcesets)操作就可以新增 source set,但是我接著要在 source set 中新增 package 的時候,發現 source set 不是 java 目錄,如下圖,正常 java 目錄是藍色,灰色是一般目錄。 ![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi83T3qIARHbjH0N3hS_vqNVMm1te_biOn3jfynUoP8twTJSRSzOV2QrtEOFD_drX8QS3SdIcl0tVVufOzcO6yWd7Ll_Z_aWa2cVWLJ6XokzIBsEoQDINgVYDW1rbE_7rvgKYQLmYmB-cva/s1600/Image+1.png) 但是另一個 Build Variant noAd 卻又正常,接著就 clean build,重開 Android Studio 試了各種方法,都沒用,最後才發現,我的 Active Build Variant 是 noAdDebug,當切到 adDebug 後,ad 下面的 java 目錄就變成藍色,可以執行新增 package 操作了。 同理,在左側 Project view 切換到 Android 的時候,也只有 Active Build Variant 的目錄才會顯示出來。 ![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtiwvF7j6PiElrz86_ymy7g3WlVwPYZtfDhz10hOiTSDY_uKgmI_22QisMVWddYZqd2O32CcaWfnIKRF5s0En1bpz7QqFtFYz1AzIgRAI0AlssOOR2oXcGsVuwilZz7-coadnLobkqcfzG/s320/Image+2.png) 為了這個蠢問題浪費了幾個小時,其實不太懂這個設計的用意是為什麼阿。

2019年11月24日 星期日

[Android] NestedScrollView 嵌套 RecyclerView 時 Scoll 位置停在 RecyclerView

markdown 最近遇到的問題是,Fragment 中為一個 NextedScrollView ,裡面從上至下有 Button 等不同的 View,最下方是 RecyclerView,但是在 Fragment 顯示時,NextedScrollView 會自動滑動到 RecyclerView 的位置。 會這樣的原因是因為,NextedScrollView 在繪製時,會去找尋 child view 中有沒有 has focus 的 child view,如果有,則滾動到它的位置。而 RecyclerView 是它找到的 has focus 的 child view,因此位置就移動到 RecyclerView 了。 這也是為什麼有人呼叫 RecyclerView.setFoucsable(false) 後,就不會發生自動滾動了,因為沒有找到 has focus 的 child view。不過最好的解法,應該是在 NextedScrollView 的直接子 view 中增加 [descendantFocusability](https://developer.android.com/reference/android/view/ViewGroup#attr_android:descendantFocusability) 屬性並將之設為 blocksDescendants, 因為 NextedScrollView 繪製過程中看到了 child view 有這個屬性,就不會再做自動滾動的動作了。
<LinearLayout
    android:id="@+id/container"
    android:descendantFocusability="blocksDescendants"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
--- Reference: * [NestedScrollView、RecycleView、ViewPager 等布局方面的常见问题汇总,及解决](https://www.jianshu.com/p/8dd1e902b7cd) * [NestedScrollview won't start from top](https://stackoverflow.com/questions/33584187/nestedscrollview-wont-start-from-top) * [Recycler view inside NestedScrollView causes scroll to start in the middle](https://stackoverflow.com/questions/36314836/recycler-view-inside-nestedscrollview-causes-scroll-to-start-in-the-middle) * [ScrollView(RecyclerView等)为什么会自动滚动原理分析,还有阻止自动滑动的解决方案](https://juejin.im/post/5a2a04726fb9a045055e0993)

2019年11月21日 星期四

Linux SCP 無法使用問題

markdown 最近遇到一個問題是用 [.NET SSH library](https://github.com/sshnet/SSH.NET) 的 scp 想將檔案複製到一塊版子上時跳出 error,顯示 SCP not found,於是試著把環境變數印出來,結果發現 PATH 變數是空的。但是用 putty 登入之後卻又有 PATH 變數。查了資料才發現,shell 有 login/non-login、interactive/non-interactive shell 之分,而且不同設定的 shell 吃到的 profile 或是 bashrc 檔案是不同的。因此應該是在該版子上的 non-login 或 non-interactive shell,沒有去吃到正確的 profile 導致沒有 PATH 環境變數。 因為板子上的程式我無權修改,所以只能用迂迴的方法,在使用 scp 之前,先把板子上 scp 執行檔搬到預設登入的目錄,這樣就不會跳出 scp not found,可以順利將檔案 copy 進去了。