侧边栏成效
侧边栏效果
使用横向滚动栏HorizontalScrollView。利用加载一个透明的View占位置,然后使用scrollView.smoothScrollTo(left, 0);来滚动,监听手势动作。
获取水平滚动栏直接子View
LinearLayout parent=(LinearLayout) getChildAt(0); parent.addView(children[0], 410, 880); parent.addView(children[1], 536, 880);
一、MainActivity布局文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="2px" android:background="#00ffffff" android:padding="0px" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/menu" android:layout_width="fill_parent" android:background="@drawable/desktop_list_item" android:layout_height="wrap_content" > <ImageView android:id="@+id/head" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitStart" android:src="@drawable/acount_head_photo" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@+id/head" android:layout_marginLeft="10dip" android:layout_toRightOf="@+id/head" android:text="沈阳师范大学" android:textColor="@color/white" android:textSize="17dip" /> </RelativeLayout> <ListView android:id="@+id/HomeListView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="@android:color/transparent" android:divider="#000" /> </LinearLayout> <grimbo.android.demo.slidingmenu.MyHorizontalScrollView android:id="@+id/myScrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="0px" android:background="#00ffffff" android:fadingEdge="none" android:fadingEdgeLength="0px" android:padding="0px" > <LinearLayout android:id="@+id/top" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="0px" android:background="#00ffffff" android:orientation="horizontal" android:padding="0px" > </LinearLayout> </grimbo.android.demo.slidingmenu.MyHorizontalScrollView> </FrameLayout>
二、监听手势
public class MyGestureDetector extends SimpleOnGestureListener { //滑动 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { float touchLength = Math.abs(e1.getY() - e2.getY()); System.out.println("touchLength="+touchLength); if (touchLength > SWIPE_MAX_OFF_PATH) return false; // 从右向左滑动 if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { System.out.println("SSSSSSSSSSSleftS"); int menuWidth = menu.getMeasuredWidth(); menu.setVisibility(View.VISIBLE); int left = menuWidth; scrollView.smoothScrollTo(left, 0); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { int left = 0; scrollView.smoothScrollTo(left, 0); System.out.println("SSSSSSSSSSSleftS"+left); } } catch (Exception e) { Log.e("detector", "excetpion:" + e.getMessage()); } return false; } } @Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) return true; else return false; }
三、侧边栏布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/app" android:layout_width="1dp" android:layout_height="1dp" android:orientation="vertical" android:background="#ffffffff" android:padding="2px" android:layout_margin="2px"> <LinearLayout android:id="@+id/tabBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/BtnSlide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="0px" android:layout_margin="0px" android:src="@drawable/ic_launcher" /> <TextView android:text="<- Press icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff00ff00" android:cacheColorHint="#ff00ff00"> </ListView> </LinearLayout>