Android侧拉框的简单实现

本文介绍了如何在Android中实现侧拉菜单。通过一个简单的LinearLayout布局和手势处理,结合VelocityTracker计算滑动速度,实现菜单的平滑显示和隐藏。示例代码详细展示了触摸事件的处理,以及ListView的数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

界面很简单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal">
    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000" 
        android:orientation="vertical">
        <ListView 
            android:id="@+id/menu_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#ffffff"
            android:dividerHeight="5dp">
        </ListView>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff" 
        android:orientation="vertical">
        <TextView
             android:id="@+id/content_text"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="25sp"
            android:text="hello"/>
    </LinearLayout>
</LinearLayout>


具体代码实现

public class MainActivity extends Activity implements OnTouchListener,
OnItemClickListener {
/**
* 滚动显示和隐藏menu时,手指滑动需要达到的速度。
*/
public static final int SNAP_VELOCITY = 200;
/**
* 屏幕宽度值。
*/
private int screenWidth;


/**
* menu最多可以滑动到的左边缘。值由menu布局的宽度来定,marginLeft到达此值之后,不能再减少。
*/
private int leftEdge;


/**
* menu最多可以滑动到的右边缘。值恒为0,即marginLeft到达0之后,不能增加。
*/
private int rightEdge = 0;


/**
* menu完全显示时,留给content的宽度值。
*/
private int menuPadding = 250;


/**
* 主内容的布局。
*/
private View content;


/**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值