Padding负值遮挡视图(下拉刷新头部)

padding值负值时,表示当前视图被遮住了一部分

可使用 view**.set Padding(int left , int top , int right , int bottom)**方法进行动态设置。

下拉刷新头部的实现:

(1) 获取屏幕大小。
(2) 设置 刷新头高度(定值)

设置 显示部分高度为/屏幕高度(定值)

设置 整体高度 为刷新头高与屏幕高之和(定值)
(3)整体的 paddlingTop 设为负值时即可遮住刷新头。(为0-刷新头高时遮住全部刷新头)
(4) 整体的paddingTop值进行动态设置:
0-刷新头高**->** 不断增加**->** 0(完全展示)****-> 不断减少**->** 0-刷新头高

java 复制代码
//例
//整体为RelativeLayout(all)
//刷新头为ImageView
//显示部分为RelativeLayout(button)

//获取控件
RelativeLayout relativeLayout_all=findViewById(R.id.relative_all);
RelativeLayout relativeLayout_button=findViewById(R.id.relative_button);
Button button=findViewById(R.id.button);
ImageView imageView=findViewById(R.id.imageView);

//获取宽度高度
WindowManager windowManager= (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics metrics=new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
int screenWidth=metrics.widthPixels;
int screenHeight= metrics.heightPixels;
imageViewHeight=screenWidth;

//设置
//总高度设置
ConstraintLayout.LayoutParams layoutParams_all= (ConstraintLayout.LayoutParams) relativeLayout_all.getLayoutParams();
layoutParams_all.height=screenHeight+screenWidth;
relativeLayout_all.setLayoutParams(layoutParams_all);
//显示部分高为屏幕高
RelativeLayout.LayoutParams layoutParams_button= (RelativeLayout.LayoutParams) relativeLayout_button.getLayoutParams();
layoutParams_button.height=screenHeight;
relativeLayout_button.setLayoutParams(layoutParams_button);
//头部分为屏幕宽
RelativeLayout.LayoutParams layoutParams_image= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams_image.height=imageViewHeight;
imageView.setLayoutParams(layoutParams_image);
//隐藏部分
relativeLayout_all.setPadding(0,0-imageViewHeight,0,0);

//Handler用于UI变更
Handler handler=new Handler(){
    public void handleMessage(@NonNull Message msg) {
        switch (msg.what){
            case 1:button.setEnabled(false);break;
            case 2:button.setEnabled(true);break;
            case 3:relativeLayout_all.setPadding(0,msg.arg1,0,0);
        }
    }
};

//按钮监听器
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Thread thread=new Thread(){
            public void run() {
                //按钮失效
                Message message=new Message();
                message.what=1;
                handler.sendMessage(message);
                //刷新头下降
                int nowTop=0-imageViewHeight;
                while (true){
                    nowTop=nowTop+8;
                    if(nowTop>=0){
                        nowTop=0;
                    }
                    message=new Message();
                    message.what=3;
                    message.arg1=nowTop;
                    handler.sendMessage(message);
                    if(nowTop==0){
                        break;
                    }
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //刷新头上升
                while (true){
                    nowTop=nowTop-8;
                    if(nowTop<=0-imageViewHeight){
                        nowTop=0-imageViewHeight;
                    }
                    message=new Message();
                    message.what=3;
                    message.arg1=nowTop;
                    handler.sendMessage(message);
                    if(nowTop==0-imageViewHeight){
                        break;
                    }
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //按钮显示
                message=new Message();
                message.what=2;
                handler.sendMessage(message);
            }
        };
        thread.start();
    }
});
相关推荐
海棠一号5 分钟前
JAVA理论第五章-JVM
java·开发语言·jvm
天天爱吃肉82188 分钟前
新能源汽车热管理核心技术解析:冬季续航提升40%的行业方案
android·python·嵌入式硬件·汽车
eternal__day21 分钟前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
颜淡慕潇25 分钟前
Redis 实现分布式锁:深入剖析与最佳实践(含Java实现)
java·redis·分布式
程序员秘密基地31 分钟前
基于vscode,idea,java,html,css,vue,echart,maven,springboot,mysql数据库,在线考试系统
java·vue.js·spring boot·spring·web app
何中应33 分钟前
【设计模式-5】设计模式的总结
java·后端·设计模式
快乐觉主吖43 分钟前
Unity的日志管理类
android·unity·游戏引擎
明月看潮生1 小时前
青少年编程与数学 01-011 系统软件简介 06 Android操作系统
android·青少年编程·操作系统·系统软件·编程与数学
吾日三省吾码1 小时前
Spring 团队详解:AOT 缓存实践、JSpecify 空指针安全与支持策略升级
java·spring·缓存
snetlogon201 小时前
JDK17 Http Request 异步处理 源码刨析
android·网络协议·http