仿QQ空间,上滑渐变显示标题栏,重写scrollView

来源:互联网 发布:管家婆软件服装免费版 编辑:程序博客网 时间:2024/05/17 23:32





以下是实现上图中的代码:

重写ScrollView

public class ObservableScrollView extends ScrollView {    public interface ScrollViewListener {        void onScrollChanged(ObservableScrollView scrollView, int x, int y,                             int oldx, int oldy);    }    private ScrollViewListener scrollViewListener = null;    public ObservableScrollView(Context context) {        super(context);    }    public ObservableScrollView(Context context, AttributeSet attrs,                                int defStyle) {        super(context, attrs, defStyle);    }
    public ObservableScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public void setScrollViewListener(ScrollViewListener scrollViewListener) {        this.scrollViewListener = scrollViewListener;    }    @Override    protected void onScrollChanged(int x, int y, int oldx, int oldy) {        super.onScrollChanged(x, y, oldx, oldy);        if (scrollViewListener != null) {            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);        }    }}

重写listview

public class MyListView extends ListView {    public MyListView(Context context) {        super(context);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}
Activity中的代码:

public class Main3Activity extends Activity implements ObservableScrollView.ScrollViewListener {    private static final String TAG = "Main3Activity";    private ObservableScrollView scrollView;    private ListView listView;    private ImageView imageView;    private TextView textView;    private int imageHeight;
    @Override    protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //设置无标题    requestWindowFeature(Window.FEATURE_NO_TITLE);    //设置全屏    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,            WindowManager.LayoutParams.FLAG_FULLSCREEN);    setContentView(R.layout.activity_main3);    Log.e(TAG,"onCreate");    scrollView = (ObservableScrollView) findViewById(R.id.scrollview);    listView = (ListView) findViewById(R.id.listview);    imageView = (ImageView) findViewById(R.id.imageview);    textView = (TextView) findViewById(R.id.textview);    initListeners();    initData();    //这个是解决scrollview里面嵌套listview,scrollview不是滚到到最顶部的问题    scrollView.smoothScrollTo(0,20);    //如果上一行代码无效的话,就添加这行    listView.setFocusable(false);    }
   private void initListeners() {        // 获取顶部图片高度后,设置滚动监听//        view事件的观察者。要注意的是它的初始化就是调用View.getViewTreeObserver()//        包含下面6个事件:////        interface  ViewTreeObserver.OnDrawListener////        挡在一个视图树绘制时,所要调用的回调函数的接口类(level 16)////        interface          ViewTreeObserver.OnGlobalFocusChangeListener//        当在一个视图树中的焦点状态发生改变时,所要调用的回调函数的接口类////        interface          ViewTreeObserver.OnGlobalLayoutListener//        当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,所要调用的回调函数的接口类
////        interface          ViewTreeObserver.OnPreDrawListener//        当一个视图树将要绘制时,所要调用的回调函数的接口类////        interface          ViewTreeObserver.OnScrollChangedListener//        当一个视图树中的一些组件发生滚动时,所要调用的回调函数的接口类////        interface          ViewTreeObserver.OnTouchModeChangeListener//        当一个视图树的触摸模式发生改变时,所要调用的回调函数的接口类        ViewTreeObserver vto = imageView.getViewTreeObserver();        //获取控件的高度---在可见的时候记录图片的高度,并且开启监听滚动
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {        @Override        public void onGlobalLayout() {            Log.e(TAG,"---------8989898989898");            imageView.getViewTreeObserver().removeGlobalOnLayoutListener(                    this);            imageHeight = imageView.getHeight();            scrollView.setScrollViewListener(Main3Activity.this);        }    });}
private void initData() {    listView.setAdapter(new BaseAdapter() {        @Override        public int getCount() {            return 100;        }        @Override        public Object getItem(int position) {            return null;        }        @Override        public long getItemId(int position) {            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView tv = new TextView(Main3Activity.this);            tv.setText("条目"+position);            return tv;        }    });}
/** * * @param scrollView * @param x 当前横向滑动距离 * @param y 当前纵向滑动距离 * @param oldx 之前横向滑动距离 * @param oldy 之前纵向滑动距离 * */
@Override    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {        Log.e(TAG,"x="+x+"   y="+y+"    oldx="+oldx+"   oldy="+oldy+"   imageHeight="+imageHeight);        if (y <= 0) {            //Color.argb参数alpha、红(red)、绿(green)、蓝(blue)四个颜色值(ARGB)            textView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供        } else if (y > 0 && y <= imageHeight) {            float scale = (float) y / imageHeight;            float alpha = (255 * scale);            // 只是layout背景透明(仿知乎滑动效果)            textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));        } else {            textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));        }    }}
下面是layout布局文件:

<?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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.iwith.iwithdemo.Main3Activity">    <com.iwith.iwithdemo.ObservableScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none" >
<LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <ImageView            android:id="@+id/imageview"            android:layout_width="match_parent"            android:layout_height="200dp"            android:background="@mipmap/dance_ufo" />        <com.iwith.iwithdemo.MyListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content" >        </com.iwith.iwithdemo.MyListView>    </LinearLayout></com.iwith.iwithdemo.ObservableScrollView>
<TextView        android:id="@+id/textview"        android:layout_width="match_parent"        android:layout_height="48dp"        android:gravity="center"        android:text="我是标题"        android:textSize="18sp"        android:textColor="#ffffff"        android:background="#00000000" /></RelativeLayout>




0 0
原创粉丝点击