1、首先创建layout文件,代码如下:
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff3390"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#eeead6"
>
这里需要注意的是我用一个HorizontalScrollView作为滚动菜单的面板,而且作为菜单项的FrameLayout的宽度设置成统一的固定宽度,方便实现根据菜单面板的宽度调整菜单项的宽度和显示的菜单项的个数。
2、程序调整菜单项的宽度,在activity的onWindowFocusChanged方法中获取滚动菜单面板的宽度和每个菜单项的宽度,并且调整菜单项的宽度以便占满菜单面板,代码如下:
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
hsvWidth=mta_hsv.getWidth();
System.out.println("onWindowFocusChanged--->hsvWidth:"+hsvWidth);
btWidth=fl[0].getWidth();
System.out.println("onWindowFocusChanged--->btWidth:"+btWidth);
//System.out.println("testLl.getWidth():"+testLl.getWidth());
count=hsvWidth/(btWidth);
System.out.println("onWindowFocusChanged--->count:"+count);
int cz=hsvWidth-(btWidth)*count;
System.out.println("onWindowFocusChanged--->cz:"+cz);
int pd=(cz/count);
btWidth+=pd;
System.out.println("onWindowFocusChanged--->pd:"+pd);
for(int i=0;i<10;i++){
//fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
LayoutParams lp=fl[i].getLayoutParams();
lp.width=btWidth;
fl[i].setLayoutParams(lp);
}
if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
3、注册HorizontalScrollView的OnTouch事件监听器,代码如下:
mta_hsv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_MOVE){
}
else if(event.getAction()==MotionEvent.ACTION_DOWN){
}
else if(event.getAction()==MotionEvent.ACTION_UP){
scrollX(v);
final View fv=v;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
scrollX(fv);
}
}, 500);//500毫秒后再次滚动一次,解决由于惯性滑动后不能滑动到合适位置的问题
}
return false;
}
private void scrollX(View v){//滑动到合适位置
int endSx=v.getScrollX();
System.out.println("endSx="+endSx);
if(endSx<=(btWidth/2)){//如果滑动距离小于半个菜单项的宽度,则滑动距离0
v.scrollTo(0, 0);
}
else if(endSx<=btWidth*1+(btWidth/2)){//如果滑动距离大于半个菜单项的宽度且小于一个半菜单项宽度,则滑动距离一个菜单项宽度
v.scrollTo(btWidth*1, 0);
}
else if(endSx<=btWidth*2+(btWidth/2)){//如果滑动距离大于一个半菜单项的宽度且小于两个半菜单项宽度,则滑动距离两个菜单项宽度
v.scrollTo(btWidth*2, 0);
}
else if(endSx<=btWidth*3+(btWidth/2)){//后面依次类推
v.scrollTo(btWidth*3, 0);
}
else if(endSx<=btWidth*4+(btWidth/2)){
v.scrollTo(btWidth*4, 0);
}
else if(endSx<=btWidth*5+(btWidth/2)){
v.scrollTo(btWidth*5, 0);
}
else if(endSx<=btWidth*6+(btWidth/2)){
v.scrollTo(btWidth*6, 0);
}
else if(endSx<=btWidth*7+(btWidth/2)){
v.scrollTo(btWidth*7, 0);
}
else if(endSx<=btWidth*8+(btWidth/2)){
v.scrollTo(btWidth*8, 0);
}
else if(endSx<=btWidth*9+(btWidth/2)){
v.scrollTo(btWidth*9, 0);
}
else if(endSx<=btWidth*10+(btWidth/2)){
v.scrollTo(btWidth*10, 0);
}
System.out.println("btWidth:"+btWidth+",count:"+count);
System.out.println("mta_hsv.getScrollX():"+mta_hsv.getScrollX());
if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
});
这里通过监听松开按压事件来调整滚动位置。
Activity的完整代码如下:
package luoye.test.styletest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MenuTestActivity extends Activity {
int hsvWidth=0;
HorizontalScrollView mta_hsv=null;
LinearLayout testLl=null;
FrameLayout []fl=new FrameLayout[10];
int btWidth=0;
int count=0;
ImageView mainNavbar_mmlIv=null;
ImageView mainNavbar_mmrIv=null;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
hsvWidth=mta_hsv.getWidth();
System.out.println("onWindowFocusChanged--->hsvWidth:"+hsvWidth);
btWidth=fl[0].getWidth();
System.out.println("onWindowFocusChanged--->btWidth:"+btWidth);
//System.out.println("testLl.getWidth():"+testLl.getWidth());
count=hsvWidth/(btWidth);
System.out.println("onWindowFocusChanged--->count:"+count);
int cz=hsvWidth-(btWidth)*count;
System.out.println("onWindowFocusChanged--->cz:"+cz);
int pd=(cz/count);
btWidth+=pd;
System.out.println("onWindowFocusChanged--->pd:"+pd);
for(int i=0;i<10;i++){
//fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
LayoutParams lp=fl[i].getLayoutParams();
lp.width=btWidth;
fl[i].setLayoutParams(lp);
}
if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_test);
//取得显示边上有隐藏菜单的图片控件
mainNavbar_mmlIv=(ImageView) findViewById(R.id.mainNavbar_mmlIv);
mainNavbar_mmrIv=(ImageView) findViewById(R.id.mainNavbar_mmrIv);
//取得菜单项
fl[0]=(FrameLayout) findViewById(R.id.mta_syFl);
fl[1]=(FrameLayout) findViewById(R.id.mta_sy1Fl);
fl[2]=(FrameLayout) findViewById(R.id.mta_sy2Fl);
fl[3]=(FrameLayout) findViewById(R.id.mta_sy3Fl);
fl[4]=(FrameLayout) findViewById(R.id.mta_sy4Fl);
fl[5]=(FrameLayout) findViewById(R.id.mta_sy5Fl);
fl[6]=(FrameLayout) findViewById(R.id.mta_wdxjmFl);
fl[7]=(FrameLayout) findViewById(R.id.mta_wdscFl);
fl[8]=(FrameLayout) findViewById(R.id.mta_gwcFl);
fl[9]=(FrameLayout) findViewById(R.id.mta_mjzxFl);
//取得滚动控件
mta_hsv=(HorizontalScrollView) findViewById(R.id.mta_hsv);
// fl[0].getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
//
// @Override
// public boolean onPreDraw() {
// // TODO Auto-generated method stub
// if(btWidth==0){
// System.out.println("fl[0]:"+fl[0].getMeasuredWidth());
// btWidth=fl[0].getMeasuredWidth();
//
// count=hsvWidth/(btWidth);
// System.out.println("onWindowFocusChanged--->count:"+count);
// int cz=hsvWidth-(btWidth)*count;
// System.out.println("onWindowFocusChanged--->cz:"+cz);
// int pd=(cz/count);
// btWidth+=pd;
// System.out.println("onWindowFocusChanged--->pd:"+pd);
// for(int i=0;i<10;i++){
// //fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
// LayoutParams lp=fl[i].getLayoutParams();
// lp.width=btWidth;
// fl[i].setLayoutParams(lp);
// }
//
// if(mta_hsv.getScrollX()>0){
// System.out.println("left:"+mta_hsv.getScrollX());
// }
// else{
// System.out.println("no-left:"+mta_hsv.getScrollX());
// }
// if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
// System.out.println("right:"+mta_hsv.getScrollX());
// }
// else{
// System.out.println("no-right:"+mta_hsv.getScrollX());
// }
//
// }
// return true;
// }
// });
//
// mta_hsv.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
//
// @Override
// public boolean onPreDraw() {
// // TODO Auto-generated method stub
// if(hsvWidth==0){
// System.out.println("mta_hsv:"+mta_hsv.getMeasuredWidth());
// hsvWidth=mta_hsv.getMeasuredWidth();
// }
// return true;
// }
// });
mta_hsv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_MOVE){
}
else if(event.getAction()==MotionEvent.ACTION_DOWN){
}
else if(event.getAction()==MotionEvent.ACTION_UP){
scrollX(v);
final View fv=v;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
scrollX(fv);
}
}, 500);//500毫秒后再次滚动一次,解决由于惯性滑动后不能滑动到合适位置的问题
}
return false;
}
private void scrollX(View v){//滑动到合适位置
int endSx=v.getScrollX();
System.out.println("endSx="+endSx);
if(endSx<=(btWidth/2)){//如果滑动距离小于半个菜单项的宽度,则滑动距离0
v.scrollTo(0, 0);
}
else if(endSx<=btWidth*1+(btWidth/2)){//如果滑动距离大于半个菜单项的宽度且小于一个半菜单项宽度,则滑动距离一个菜单项宽度
v.scrollTo(btWidth*1, 0);
}
else if(endSx<=btWidth*2+(btWidth/2)){//如果滑动距离大于一个半菜单项的宽度且小于两个半菜单项宽度,则滑动距离两个菜单项宽度
v.scrollTo(btWidth*2, 0);
}
else if(endSx<=btWidth*3+(btWidth/2)){//后面依次类推
v.scrollTo(btWidth*3, 0);
}
else if(endSx<=btWidth*4+(btWidth/2)){
v.scrollTo(btWidth*4, 0);
}
else if(endSx<=btWidth*5+(btWidth/2)){
v.scrollTo(btWidth*5, 0);
}
else if(endSx<=btWidth*6+(btWidth/2)){
v.scrollTo(btWidth*6, 0);
}
else if(endSx<=btWidth*7+(btWidth/2)){
v.scrollTo(btWidth*7, 0);
}
else if(endSx<=btWidth*8+(btWidth/2)){
v.scrollTo(btWidth*8, 0);
}
else if(endSx<=btWidth*9+(btWidth/2)){
v.scrollTo(btWidth*9, 0);
}
else if(endSx<=btWidth*10+(btWidth/2)){
v.scrollTo(btWidth*10, 0);
}
System.out.println("btWidth:"+btWidth+",count:"+count);
System.out.println("mta_hsv.getScrollX():"+mta_hsv.getScrollX());
if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
});
}
}
完整源码下载地址:http://download.csdn.net/detail/luoye007001/5551659