防百度地图上下拖动View

本文介绍如何在Android项目中创建一个类似百度地图的上下拖动效果。通过自定义UpAndDownView,并结合RelativeLayout与ScrollView,实现在手指滑动时地图视图跟随移动,松手后自动滑动到底部或顶部。详细代码示例和解决滑动冲突的方法一并提供,项目源码已上传至GitHub。
摘要由CSDN通过智能技术生成

因项目需求要实现类似于百度地图上下拖动的效果,特此分享,百度地图效果:
这里写图片描述
地图显示为一个Activity,View可以跟随手指的滑动而移动,手指松开时自动滑动到屏幕的底部或者顶部。
直接上代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <com.test.updown.view.UpAndDownView
        android:id="@+id/up_down_view"
        android:visibility="gone"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.test.updown.view.UpAndDownView>

</RelativeLayout>

布局文件,因为需求是View在地图的底部,所以自定义UpAndDownView放在了parent的bottom,这里默认隐藏。
下面是自定义UpAndDownView代码

public class UpAndDownView extends CustomUpAndDownLayout {
   
    private Context mContext;

    public UpAndDownView(Context context) {
        super(context);
        initView(context);
    }

    public UpAndDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void initView(final Context mContext) {
        this.mContext = mContext;
        View view = LayoutInflater.from(mContext).inflate(R.layout.up_down_layout, null, false);
        addView(view);
        InnerScrollView innerScrollView = (InnerScrollView) view.findViewById(R.id.scrollView);
        if (innerScrollView != null) {
            innerScrollView.parentView = UpAndDownView.this;
        }
        view.findViewById(R.id.textview).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "Click the TextView", 0).show();
            }
        });
    }

    public void setUpDownListener(UpAndDownListener listener) {
        setListener(listener);
    }

    public void setIsSlide(boolean isSlide) {
        isSlide(isSlide);
    }

    public boolean isSlide() {
        return isCanSlide();
    }
}

自定义View的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:background="#f00"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值