安卓开发常用知识

RecyclerView添加分隔线

默认情况下RecyclerView中间是没有分割线的,需要用户自己添加
添加方式如下

//添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.custom_divider));
        rvNews.addItemDecoration(divider);

custom_divider的布局为




    
        
            
        
    

    
        
            
        
    



自定义ProgressBar

需求:定义如下图所示的progressbar


image.png

首先xml中添加progressbar

    

style="@android:style/Widget.ProgressBar.Horizontal"样式一定要加入,不然会是默认的,圆圈转动的样式
progress_bg代码如下



    
    
    
        
            

            
        
    

    
    
        
            
                

                
            
        
    

ScrollView嵌套RecyclerView造成 RecyclerView滑动卡顿

rvGoods.setNestedScrollingEnabled(false);

自定义progressBar样式



    
    
        
            
            
        
    

    
    
        
            

        
    


//下面是bg_progress_training_1

    
    

这里的clip可以处理进度条后面的截断问题,让进度条末尾为圆弧形

ConstraintLayout中TextView超出屏幕

将宽度设置成0dp
加入app:layout_constraintHorizontal_weight="1"
右侧这时候用layout_constraintRight_toRightOf="parent"

RecycleView 设置GridLayoutManager时,做到平均分配空间大小的问题,item 布局文件的宽度,不要写死了,容易造成裁剪

rvSpecialRecommend.setAdapter(recommendAdapter);
rvSpecialRecommend.addItemDecoration(new SpaceItemDecoration(getContext(), 2, DisplayUtils.dp2px(getContext(), 10)));


/**
 * GridLayoutManager 均分屏幕
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
    private Context mContext;
    private int spanCount;
    private int dividerWidth;

    public SpaceItemDecoration(Context context, int spanCount, int dividerWidthDp) {
        this.mContext = context;
        this.spanCount = spanCount;
        this.dividerWidth = dividerWidthDp;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        int pos = parent.getChildAdapterPosition(view);
        // 计算这个child 处于第几列
        int column = (pos) % spanCount;

        outRect.left = (column * dividerWidth / spanCount);
        outRect.right = dividerWidth - (column + 1) * dividerWidth / spanCount;

        if (pos >= spanCount)
            outRect.top = dividerWidth;

    }

}

Android监听键盘的展示与隐藏

private fun setKeyboardVisibilityListener(onKeyboardVisibilityListener: SoftInputUtils.OnKeyboardVisibilityListener) {
        val parentView: View = (findViewById(android.R.id.content) as ViewGroup).getChildAt(0)
        parentView.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            var alreadyOpen = false
            val defaultKeyboardHeightDP = 100f
            private val EstimatedKeyboardDP: Float =
                (defaultKeyboardHeightDP + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 48 else 0).toFloat()
            private val rect: Rect = Rect()
            override fun onGlobalLayout() {
                val estimatedKeyboardHeight = TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    EstimatedKeyboardDP,
                    parentView.resources.displayMetrics
                )
                parentView.getWindowVisibleDisplayFrame(rect)
                val heightDiff: Int = parentView.rootView.height - (rect.bottom - rect.top)
                val isShown = heightDiff >= estimatedKeyboardHeight
                if (isShown == alreadyOpen) {
                    return
                }
                alreadyOpen = isShown
                onKeyboardVisibilityListener.onVisibilityChanged(isShown)
            }
        })
    }

    public interface OnKeyboardVisibilityListener {
        void onVisibilityChanged(boolean visible);
    }

   setKeyboardVisibilityListener(this)

gradle 8.0 不支持Switch Case 点击事件

gradle.properties添加这个

android.nonFinalResIds=false

AgentWeb加载网页时,隐藏网页中不想展示的一些元素

javascript:(
            function test(dom) {
                        var header = document.querySelector('div[class="header-v3"]');
                        if(header!=null){
                            header.style.display = "none";
                        }
                        var bottom = document.querySelector('div[id="bottom_half_cont"]');
                        if(bottom!=null){
                            bottom.style.display = "none";
                        }
            }
)()

在WebChromeClient的onProgress中添加


image.png

你可能感兴趣的:(安卓开发常用知识)