布局文件main.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">
<MyHorizontalScrollView
android:id="@+id/layout"
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"/>
<ListView
android:id="@+id/listView_msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="65dp"></ListView>
</LinearLayout>
</MyHorizontalScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/quit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quit"/>
</LinearLayout>
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;
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);
Button quitButton = (Button) myMenu.findViewById(R.id.quit_button);
quitButton.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();
}
});
myContent = (ViewGroup) linearLayout.getChildAt(1);
myMenuWidth = myMenu.getLayoutParams().width = screenWidth
- myMenuPaddingRight;
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();
if (scrollX >= myMenuWidth / 2) {
this.smoothScrollTo(myMenuWidth, 0);
} else {
this.smoothScrollTo(0, 0);
}
return true;
}
return super.onTouchEvent(ev);
}
}