一、使用步骤
3.布局中需要包含自定义的horizontal.xml
(自定义侧滑菜单内容)
二、代码
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class MyHorizontalScrollView extends HorizontalScrollView {
private LinearLayout linearLayout;
private ViewGroup myMenu;
private ViewGroup myContent;
private int myMenuWidth;
private int screenWidth;
private int myMenuPaddingRight = 50;
private boolean once = false;
private boolean isOpen;
private int mBaseScroll;
String myId = EMClient.getInstance().getCurrentUser();
private ArrayList<FunctionItem> functionItems = new ArrayList<>();
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
myMenuPaddingRight = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()
.getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!once) {
linearLayout = (LinearLayout) this.getChildAt(0);
myMenu = (ViewGroup) linearLayout.getChildAt(0);
TextView quitText = (TextView) myMenu.findViewById(R.id.quit_text);
TextView accountText = (TextView) myMenu.findViewById(R.id.msg_name_textView);
accountText.setText(myId);
quitText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("MyHorizontalScrollView", "quit");
EMClient.getInstance().logout(true);
Intent intent = new Intent(getContext(), LoginActivity.class);
getContext().startActivity(intent);
((Activity) getContext()).finish();
}
});
ListView listView = (ListView) myMenu.findViewById(R.id.info_show_list);
functionItems.add(new FunctionItem(1, R.drawable.frs, "开通会员"));
functionItems.add(new FunctionItem(2, R.drawable.fcd, "我的空间"));
functionItems.add(new FunctionItem(3, R.drawable.fcc, "我的相册"));
functionItems.add(new FunctionItem(4, R.drawable.fdm, "我的文件"));
FunctionAdapter adapter = new FunctionAdapter(getContext(), R.layout.horizontalscrollview_item, functionItems);
listView.setAdapter(adapter);
myContent = (ViewGroup) linearLayout.getChildAt(1);
ImageView imageView = (ImageView) myContent.findViewById(R.id.head);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toggle();
}
});
myMenuWidth = myMenu.getLayoutParams().width = screenWidth
- myMenuPaddingRight;
mBaseScroll = myMenuWidth;
myContent.getLayoutParams().width = screenWidth;
once = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
this.scrollTo(myMenuWidth, 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
int scrollX = this.getScrollX();
int relativeScroll = scrollX - mBaseScroll;
Log.i("MyHorizontal", scrollX+":"+myMenuWidth+":"+relativeScroll);
if (relativeScroll >= myMenuWidth / 6) {
this.smoothScrollTo(myMenuWidth, 0);
mBaseScroll = myMenuWidth;
} else if(-relativeScroll >= (myMenuWidth/5)){
this.smoothScrollTo(0, 0);
mBaseScroll = 0;
} else if(mBaseScroll > myMenuWidth/2){
this.smoothScrollTo(myMenuWidth, 0);
mBaseScroll = myMenuWidth;
} else{
this.smoothScrollTo(0, 0);
mBaseScroll = 0;
}
return true;
}
return super.onTouchEvent(ev);
}
public void openMenu() {
if (isOpen) return;
this.smoothScrollTo(0, 0);
isOpen = true;
}
public void closeMenu() {
if (!isOpen) return;
this.smoothScrollTo(myMenuWidth, 0);
isOpen = false;
}
public void toggle() {
if (isOpen) {
closeMenu();
} else {
openMenu();
}
}
}
2. main.xml(包含horizontal)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.lenovo.qqdemos.Main.MyHorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include layout="@layout/horizontalscrollview" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/main_head_panel"
android:layout_above="@id/main_bottom_panel"></FrameLayout>
</LinearLayout>
</com.example.lenovo.qqdemos.Main.MyHorizontalScrollView>
</LinearLayout>
3. horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.pkmmte.view.CircularImageView
android:id="@+id/head"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:src="@drawable/feather"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="1dp"
app:shadow="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/msg_name_textView"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="帅猎羽"
android:textColor="@color/black"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="@+id/info_show_textView"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginTop="10dp"
android:singleLine="true"
android:text="hello" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/info_trend_textView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="30dp"
android:singleLine="true"
android:text="愿再无灾难" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">
<ListView
android:id="@+id/info_show_list"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="30dp"></ListView>
<TextView
android:layout_below="@+id/info_show_list"
android:id="@+id/quit_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="退出"
android:textColor="@android:color/white"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>