ScrollView实现侧边导航控制(点击按钮页面翻动、页面翻动按钮变色)

package com.example.scroollview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements ScrollViewListener{  

	private ObservableScrollView scrollView1;
	private ImageButton imageButton_up,imageButton_down;

	@Override  
	protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.activity_main);  
		intView();
		intdata();
	} 

	private void intdata() {
		imageButton_down.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "down", 0).show();
				scrollView1.smoothScrollBy(//页面滚动
						1000, //滚动时间
						470);//滚动的距离
			}
		});
		imageButton_up.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "up", 0).show();
				scrollView1.smoothScrollBy(//页面滚动
						1000, //滚动时间
						-470);//滚动的距离
			}
		});
	}

	private void intView() {
		imageButton_up=(ImageButton) findViewById(R.id.up1);
		imageButton_down=(ImageButton) findViewById(R.id.down1);
		scrollView1 =(ObservableScrollView) findViewById(R.id.view1);
		imageButton_up.setBackgroundResource(R.drawable.button_up_unuse);//正常打开状态下 up按钮为按下
		scrollView1.setScrollViewListener(this);  
	}

	//获取页面移动的监听
	@Override  
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y,  
			int oldx, int oldy) {  
		//顶端以及滑出去的距离
		int scrollY=scrollView1.getScrollY();
		//界面的高度
		int height=scrollView1.getHeight();
		//scrollview所占的高度
		int scrollViewMeasuredHeight=scrollView.getChildAt(0).getMeasuredHeight();
		/*
		 * 判断页面的位置,从而显示对于的上下按钮
		 */
		if(scrollY==0){//在顶部
			imageButton_up.setBackgroundResource(R.drawable.button_up_unuse);//up按钮为按下
		}else if((scrollY+height)==scrollViewMeasuredHeight){//在底部
			imageButton_down.setBackgroundResource(R.drawable.button_down_unuse);
		}else {//在中间状态up、down为正常
			imageButton_down.setBackgroundResource(R.drawable.button_down);
			imageButton_up.setBackgroundResource(R.drawable.button_up);
		}
	}
}  
<pre name="code" class="html">package com.example.scroollview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {  
  
    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);  
        }  
    }  
  
}  


 
 
package com.example.scroollview;


public interface ScrollViewListener {


<span style="white-space:pre">	</span>void onScrollChanged(ObservableScrollView observableScrollView, int x,
<span style="white-space:pre">			</span>int y, int oldx, int oldy);


}
xml:button_down_unuse:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item><bitmap android:src="@drawable/downb" />
    </item>


</selector>
 
 
button_down:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"><bitmap android:src="@drawable/downb" />
    </item>
    <item><bitmap android:src="@drawable/downg" />
    </item>


</selector>
 
 
activity_main.xml:
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >


    <RelativeLayout
        android:id="@+id/dh"
        android:layout_width="32dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="3.5dp"
        android:orientation="vertical" >


        <ImageButton
            android:id="@+id/up1"
            android:layout_width="70px"
            android:layout_height="85px"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/button_up"
            android:clickable="true" />


        <ImageView
            android:id="@+id/yiny"
            android:layout_width="57px"
            android:layout_height="150dp"
            android:layout_below="@+id/up1"
            android:background="@drawable/dh" />


        <ImageButton
            android:id="@+id/down1"
            android:layout_width="70px"
            android:layout_height="85px"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/yiny"
            android:background="@drawable/button_down"
            android:clickable="true" />
    </RelativeLayout>


    <com.example.scroollview.ObservableScrollView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <LinearLayout
            android:layout_width="268dp"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/made5_1" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/made5_2" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/made5_3" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/made5_4" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/made5_5" />
        </LinearLayout>
    </com.example.scroollview.ObservableScrollView>


</LinearLayout>

你可能感兴趣的:(ScrollView实现侧边导航控制(点击按钮页面翻动、页面翻动按钮变色))