Android自定义布局实现仿qq侧滑部分代码
发布时间 - 2026-01-11 00:27:08 点击率:次自定义布局实现仿qq侧滑部分Android代码,供大家参考,具体内容如下

源码DEMO地址:https://github.com/applelili/ImitationQQ
实现说明:
通过自定义布局实现:
SlidingLayout继承于 HorizontalScrollView
/**
* Created by Administrator on 2017/3/29.
*/
public class SlidingLayout extends HorizontalScrollView{
/** 左侧右边间距 */
private float rightPadding;
/** 左侧菜单的宽度 */
private int leftWidth;
private ViewGroup leftView;
private ViewGroup contentView;
private final Context context;
private boolean isOpenMeun = true;
private ImageView shadowView;
public SlidingLayout(Context context) {
this(context,null);
}
public SlidingLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
//获取自定义的属性
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.SlidingLayout);
rightPadding=typedArray.getDimension(R.styleable.SlidingLayout_rightPadding,80);
//计算左侧菜单的宽度
leftWidth = (int) (getScreenWidth() - rightPadding + 0.5f);
}
//获取屏幕的宽度
private float getScreenWidth() {
return getResources().getDisplayMetrics().widthPixels;
}
@Override /** 布局解析完毕的时候 */
protected void onFinishInflate() {
super.onFinishInflate();
ViewGroup container= (ViewGroup) getChildAt(0);
if(container.getChildCount() > 2){
throw new IllegalStateException("SlidingLayout中只能放两个子View");
}
//获取左侧菜单view
leftView = (ViewGroup) container.getChildAt(0);
//获取主布局的Viwe
contentView = (ViewGroup) container.getChildAt(1);
//设置子view 的宽度
leftView.getLayoutParams().width = leftWidth;
contentView.getLayoutParams().width = (int) getScreenWidth();
//移除父布局
container.removeView(contentView);
FrameLayout frameLayout=new FrameLayout(context);
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
frameLayout.addView(contentView);
//添加阴影
shadowView = new ImageView(context);
shadowView.setBackgroundColor(Color.parseColor("#99000000"));
frameLayout.addView(shadowView);
container.addView(frameLayout);
}
/**
* 该方法在滑动的时候会不断的调用
* @param l : left
* @param t
* @param oldl
* @param oldt
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float x=l*0.8f;//偏移量
leftView.setTranslationX(x);//平移
float color = 1 - l * 1.0f / leftWidth;
shadowView.setAlpha(color);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP://手指抬起的时候判断是否关闭
int currentX = getScrollX();
if (isOpenMeun) {
if (currentX >= leftWidth / 2) {
closeMeun();
} else {
openMeun();
}
//点击关闭
float x = ev.getX();
if (x > leftWidth) {
closeMeun();
}
return true;
} else {//关闭状态
if (currentX < leftWidth / 2) {
openMeun();
} else {
closeMeun();
}
return true;
}
}
return super.onTouchEvent(ev);
}
/** 关闭菜单 */
public void closeMeun(){
isOpenMeun = false;
smoothScrollTo(leftWidth,0);// 250ms
}
/** 打开菜单 */
public void openMeun(){
isOpenMeun = true;
smoothScrollTo(0,0);
}
}
attrs属性文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SlidingLayout"> <attr name="rightPadding" format="dimension"/> </declare-styleable> </resources>
布局方面
<?xml version="1.0" encoding="utf-8"?> <com.example.myqq.SlidingLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:rightPadding="65dp" tools:context="com.example.myqq.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_main" /> <include layout="@layout/right_main" /> </LinearLayout> </com.example.myqq.SlidingLayout>
activity
package com.example.myqq;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String strings[] = {"开通会员", "QQ钱包", "个性装扮", "我的收藏", "我的相册", "我的文件", "我的日程", "我的名片夹"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setState();
setContentView(R.layout.activity_main);
ListView listView= (ListView) findViewById(R.id.list_left);
listView.setDividerHeight(0);
listView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings));
ImageView bgimg1= (ImageView) findViewById(R.id.bgimg);
float currentY=bgimg1.getTranslationY();
ObjectAnimator animator = ObjectAnimator.ofFloat(bgimg1, "translationY", currentY, -100, -40, currentY);
animator.setDuration(5000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.start();
}
@TargetApi(20)
private void setState() {
WindowManager.LayoutParams params=new WindowManager.LayoutParams();
params.flags=WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
getWindow().setAttributes(params);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# qq
# 侧滑
# Android高仿QQ6.0侧滑删除实例代码
# Android使用ViewDragHelper实现仿QQ6.0侧滑界面(一)
# Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
# Android滑动优化高仿QQ6.0侧滑菜单(滑动优化)
# Android使用DrawerLayout实现仿QQ双向侧滑菜单
# 基于Android实现仿QQ5.0侧滑
# Android基于ViewDragHelper仿QQ5.0侧滑界面效果
# Android程序开发之使用Design包实现QQ动画侧滑效果和滑动菜单导航
# Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解
# Android仿QQ6.0主页面侧滑效果
# 自定义
# 具体内容
# 大家多多
# 移除
# 判断是否
# 名片夹
# 偏移量
# getChildAt
# getChildCount
# container
# onFinishInflate
# void
# IllegalStateException
# View
# throw
# gt
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
活动邀请函制作网站有哪些,活动邀请函文案?
Android实现代码画虚线边框背景效果
在centOS 7安装mysql 5.7的详细教程
MySQL查询结果复制到新表的方法(更新、插入)
php8.4header发送头信息失败怎么办_php8.4header函数问题解决【解答】
Laravel如何使用Eloquent进行子查询
nodejs redis 发布订阅机制封装实现方法及实例代码
Laravel API资源(Resource)怎么用_格式化Laravel API响应的最佳实践
魔毅自助建站系统:模板定制与SEO优化一键生成指南
如何在企业微信快速生成手机电脑官网?
jQuery中的100个技巧汇总
Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】
网站页面设计需要考虑到这些问题
Laravel如何记录自定义日志?(Log频道配置)
网站制作壁纸教程视频,电脑壁纸网站?
ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】
Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程
如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】
Laravel Octane如何提升性能_使用Laravel Octane加速你的应用
Laravel如何为API生成Swagger或OpenAPI文档
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
北京网站制作公司哪家好一点,北京租房网站有哪些?
如何在Windows 2008云服务器安全搭建网站?
PHP正则匹配日期和时间(时间戳转换)的实例代码
如何用AWS免费套餐快速搭建高效网站?
Laravel怎么使用Blade模板引擎_Laravel模板继承与Component组件复用【手册】
javascript中的try catch异常捕获机制用法分析
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
黑客如何利用漏洞与弱口令入侵网站服务器?
Laravel API资源类怎么用_Laravel API Resource数据转换
如何在建站主机中优化服务器配置?
手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?
大同网页,大同瑞慈医院官网?
Laravel如何处理文件下载请求?(Response示例)
如何用PHP快速搭建CMS系统?
教你用AI将一段旋律扩展成一首完整的曲子
如何在宝塔面板中修改默认建站目录?
Android自定义控件实现温度旋转按钮效果
香港网站服务器数量如何影响SEO优化效果?
如何在云主机快速搭建网站站点?
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
如何为不同团队 ID 动态生成多个独立按钮
linux top下的 minerd 木马清除方法
如何用VPS主机快速搭建个人网站?
Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件
如何快速搭建虚拟主机网站?新手必看指南
详解vue.js组件化开发实践
Laravel如何与Pusher实现实时通信?(WebSocket示例)

