悬停view的使用

非 Y 不嫁゛ 提交于 2019-11-28 04:18:09

首先是布局资源

 

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;        }    }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!