因项目需求要实现类似于百度地图上下拖动的效果,特此分享,百度地图效果:
地图显示为一个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">