知識小記

recyclerview

1.setSpanLookup 可以根據(jù)不同的行規(guī)則去設置每行的item數(shù)以達到不規(guī)則排序的效果
2.StaggeredGridLayoutManager 實現(xiàn)最簡單瀑布流
3.PagerSnapHelper() 像viewpage一樣一頁一頁滾動 快速滾動LinearSnapHelper
4.禁止?jié)L動

 LinearLayoutManager layoutManager = new LinearLayoutManager(mContext) {
        @Override
        public boolean canScrollVertically() {
           
            return false;
        }
    };
讓iamgeview保持圖片的比例
<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>
最小距離

ViewConfiguration.getScaledTouchSlop();觸發(fā)移動事件的最小距離,自定義View處理touch事件的時候,有的時候需要判斷用戶是否真的存在movie,系統(tǒng)提供了這樣的方法。表示滑動的時候,手的移動要大于這個返回的距離值才開始移動控件。

int slop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context));
DragFloatingActionButton + recyclerview

問題 滾動時背景變化導致layout 回原來位置
解決 1:
解決2 :
floatingactionbutton icon 顯示太小:
在sdk28之前,需要設置

 android:scaleType="center" 
 app:borderWidth="0dp"
 app:fabSize="normal"

就可以正常顯示,但sdk升級到28之后再次出現(xiàn)圖片縮小問題,所以sdk28之后需設置屬性

app:maxImageSize="@dimen/dp_50"

就可以正常顯示了
app:backgroundTint - 設置FAB的背景顏色。
app:rippleColor - 設置FAB點擊時的背景顏色。
app:borderWidth - 該屬性尤為重要,如果不設置0dp,那么在4.1的sdk上FAB會顯示為正方形,而且在5.0以后的sdk沒有陰影效果。所以設置為borderWidth="0dp"。
app:elevation - 默認狀態(tài)下FAB的陰影大小。
app:pressedTranslationZ - 點擊時候FAB的陰影大小。
app:fabSize - 設置FAB的大小,該屬性有兩個值,分別為normal和mini,對應的FAB大小分別為56dp和40dp。
android:src - 設置FAB的圖標,Google建議符合Design設計的該圖標大小為24dp。
app:layout_anchor - 設置FAB的錨點,即以哪個控件為參照點設置位置。
app:layout_anchorGravity - 設置FAB相對錨點的位置,值有 bottom、center、right、left、top等。
View繪制分三個步驟,順序是:onMeasure,onLayout,onDraw。經(jīng)代碼親測,log輸出顯示:調(diào)用invalidate方法只會執(zhí)行onDraw方法;調(diào)用requestLayout方法只會執(zhí)行onMeasure方法和onLayout方法,并不會執(zhí)行onDraw方法。

dispatch
@Override  
    public boolean dispatchTouchEvent(MotionEvent ev) { 
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);  
    }
lottie 錯誤

Android lottie java.lang.IllegalStateException: Missing values for keyframe
對接lottie時,根據(jù)Lottie用法,加載json,效果顯示不出來,lottie官網(wǎng)上預覽json顯示正常,大量搜索后,發(fā)現(xiàn)lottie低版本,不適用現(xiàn)在Adobe After Effects和bodymovin插件,重新導出json后,正常顯示。
Settings > Advanced > Export Old Json Format

軟鍵盤收起
   private void hideInput(EditText editText){
        InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(editText.getWindowToken(),0);
    }

Android 軟鍵盤的彈出及關(guān)閉
Android 軟鍵盤的彈出及關(guān)閉2

 /**
     * 1、獲取main在窗體的可視區(qū)域
     * 2、獲取main在窗體的不可視區(qū)域高度
     * 3、判斷不可視區(qū)域高度
     *   ①大于180:鍵盤顯示  獲取Scroll的窗體坐標,算出main需要滾動的高度,使scroll顯示。
     *   ②小于180:鍵盤隱藏
     *   該值根據(jù)屏幕可以做出修改,在大屏手機上可以適當?shù)恼{(diào)大,不然會出現(xiàn)問題。
     *
     * @param main   根布局
     * @param scroll 需要顯示的最下方View
     */
    public void addLayoutListener(final View main, final View scroll) {
        main.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            Rect rect = new Rect();
            main.getWindowVisibleDisplayFrame(rect);
            int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;

            if (mainInvisibleHeight > 180) {
                int[] location = new int[2];
                scroll.getLocationInWindow(location);
                int scrollHeight = (location[1] + scroll.getHeight() + ((RelativeLayout.LayoutParams) scroll.getLayoutParams()).bottomMargin) - rect.bottom;
                if (scrollHeight > 0) {
                    main.scrollTo(0, scrollHeight);
                }
            } else {
                main.scrollTo(0, 0);
            }
        });
    }
Recyclerview 設置ItemView的內(nèi)嵌偏移長度(inset)

TextItemDecoration.class

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //只是添加下面這一行代碼
            outRect.set(50, 50, 50, 50);
        }
鍵盤不彈出
android:focusable="true"
android:focusableInTouchMode="true"
清空焦點去掉豎線
edittext.clearfocuse();
 editText.setFocusableInTouchMode(fase);
splashactivity theme 劉海屏適配
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowFullscreen">true</item>
    <!--不讓windowBackground延申到navigation bar區(qū)域-->
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <!--適配Android P劉海屏-->
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
[lianjie]([https://blog.csdn.net/wds1181977/article/details/88095843](https://blog.csdn.net/wds1181977/article/details/88095843)
)
[Q 文件]([https://www.jb51.net/article/177201.htm](https://www.jb51.net/article/177201.htm)
)([https://www.anzhuoq.com/thread-4890.htm](https://www.anzhuoq.com/thread-4890.htm)
)

ViewGroup容器組件的繪制,當它沒有背景時直接調(diào)用的是dispatchDraw()方法, 而繞過了draw()方法,當它有背景的時候就調(diào)用draw()方法,而draw()方法里包含了dispatchDraw()方法的調(diào)用。因此要在ViewGroup上繪制東西的時候往往重寫的是dispatchDraw()方法而不是onDraw()方法,或者自定制一個Drawable,重寫它的draw(Canvas c)和 getIntrinsicWidth(), getIntrinsicHeight()方法,然后設為背景。

常用SpannableStringBuilder
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(mPrivacyTv.getText());
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                WebViewActivity.startThisActivity(getActivity(), getResources().getString(R.string.privacy_url), getResources().getString(R.string.policy));
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(getResources().getColor(R.color.textColorPrimary));
                ds.setUnderlineText(true);
            }
        }, 0, mPrivacyTv.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mPrivacyTv.setText(stringBuilder);
        mPrivacyTv.setMovementMethod(LinkMovementMethod.getInstance());
   private SpannableStringBuilder getSpannerText(String strColor, int color, String str){
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(str);
        int start = str.indexOf(strColor);
        int end = start + strColor.length();
        if (start >= 0){
            stringBuilder.setSpan(new ForegroundColorSpan(getResources().getColor(color)),start,end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return  stringBuilder;
    }

retrofit interceptor 獲取respond的內(nèi)容 但是這樣會導致accpt收不到數(shù)據(jù)

//        Logger.e(TAG, "intercept: respond:" + response.body().string());

profiler 攔截net網(wǎng)絡請求 查看其參數(shù)
view.getLocationOnscren為0 猜測是因為view 沒有完全展示在頁面中
BigDecimal 用String.valueof() 精度最大

滾動沖突vertical recycler+horizatal recycler+coordinatorlayout.就是對內(nèi)部嵌套的橫向滑動的RecyclerView設置它的setNestedScrollingEnabled(false)
如果是整個xml中使用fragment(內(nèi)部頁面為 linearlyout包裹reccyler) 那么很有可能會導致其內(nèi)部的recycler和外部的滾動控件造成滾動沖突

避免多次加載fragment

private View rootView;// 緩存Fragment view
 
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState)
  {
    Log.i(TAG, "onCreateView");
 
    if (rootView == null)
    {
      rootView = inflater.inflate(R.layout.fragment_1, null);
    }
    // 緩存的rootView需要判斷是否已經(jīng)被加過parent,如果有parent需要從parent刪除,要不然會發(fā)生這個rootview已經(jīng)有parent的錯誤。
    ViewGroup parent = (ViewGroup) rootView.getParent();
    if (parent != null)
    {
      parent.removeView(rootView);
    }
    return rootView;
}
Android私有目錄分享權(quán)限問題

在分享私有目錄下的文件的時候失敗(Uri.parser的Uri),11及以上的系統(tǒng)不允許訪問其它app的私有目錄,用FileProvider就可以訪問
https://developer.android.google.cn/training/secure-file-sharing/share-file?hl=zh-cn

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容