码迷,mamicode.com
首页 > 其他好文 > 详细

悬停view的使用

时间:2019-08-22 00:38:49      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ops   scale   prot   stat   pre   family   protected   extend   OLE   

首先是布局资源

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">



<com.example.stickview.MyScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ffff00"
/>
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:text="我是全局悬浮view"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@color/colorPrimaryDark"
/>
</LinearLayout>
</com.example.stickview.MyScrollView>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:text="我是假全局悬浮view"
android:visibility="gone"
/>
</RelativeLayout>






MainActivity代码
package com.example.stickview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

MyScrollView scroll;
TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scroll = (MyScrollView)findViewById(R.id.scroll);
t1 = (TextView)findViewById(R.id.t1);
t2 = (TextView)findViewById(R.id.t2);
scroll.setView(t1,t2);
}
}




自定义ScrollView代码
package com.example.stickview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;


public class MyScrollView extends ScrollView {

private static StopCall stopCall;
Context ctx;
TextView t1,t2;
//ScrollView向上滑动到顶部的距离
private int upH;

public MyScrollView(Context context) {

super(context);

}

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.ctx = context;
//赋值:300很重要,这个值是顶部2上面的高度,也就是本例中图片的高度
upH = dpToPx(ctx,200);//单位是dp
}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

}


public static void setCallback(StopCall c){
stopCall =c ;
}


@Override protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
super.onScrollChanged(l, t, oldl, oldt);
if(t>upH) {
stopSlide(true);
}
else{
stopSlide(false);
}

}

/**
* 接口实现,可不可就
*/
public void stopSlide(boolean isStop){
if (isStop) {
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.GONE);
} else {
t2.setVisibility(View.VISIBLE);
t1.setVisibility(View.GONE);
}
}


public interface StopCall {

public void stopSlide(boolean isStop);
}


public static int dpToPx(Context context,float dpValue) {//dp转换为px
float scale=context.getResources().getDisplayMetrics().density;//获得当前屏幕密度
return (int)(dpValue*scale+0.5f);
}


public void setView(TextView t1,TextView t2)
{
this.t1 = t1;
this.t2 = t2;
}
}









悬停view的使用

标签:ops   scale   prot   stat   pre   family   protected   extend   OLE   

原文地址:https://www.cnblogs.com/pushu/p/11392048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!