Created
September 3, 2015 13:21
-
-
Save Haldir65/3232f83f657dc9902045 to your computer and use it in GitHub Desktop.
跟着慕课网老师后面敲的,看到QQ滑动界面做出来,好幸福
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
这里有两个package: | |
com.Harris.qqsliding_view/SlidingMenu.java | |
com.Harris.qqslidingview/MainActivity.java | |
SlidingMenu.java代码部分 | |
package com.Harris.qqsliding_view; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
import android.view.MotionEvent; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
import android.widget.HorizontalScrollView; | |
import android.widget.LinearLayout; | |
public class SlidingMenu extends HorizontalScrollView { | |
private LinearLayout mWapper; | |
private ViewGroup mMenu; | |
private ViewGroup mContent; | |
private int mScreenWidth; | |
private int mMenuWidth; | |
private int mMenuRightPadding =50; | |
//避免onMeasure方法被多次调用 | |
private boolean once = false; | |
public SlidingMenu(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
//一下代码用于获得屏幕宽度 | |
WindowManager WM = (WindowManager) context. | |
getSystemService(context.WINDOW_SERVICE); | |
DisplayMetrics outMetrics = new DisplayMetrics(); | |
WM.getDefaultDisplay().getMetrics(outMetrics); | |
mScreenWidth = outMetrics.widthPixels; | |
//将50dp转化为像素值px | |
mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources(). | |
getDisplayMetrics()); | |
} | |
//在onMeasure方法里面设置View的宽和高,以及自己的宽和高 | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
if(!once){//设置子View的宽和高 | |
mWapper = (LinearLayout) getChildAt(0); | |
mMenu = (ViewGroup) mWapper.getChildAt(0); | |
mContent = (ViewGroup) mWapper.getChildAt(1); | |
mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding; | |
mContent.getLayoutParams().width = mScreenWidth; | |
mWapper.getLayoutParams().width = mScreenWidth; | |
once=true;//这样onMeasure方法只会被调用一次,不会被多次调用 | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
//通过设置偏移量将menu隐藏 | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
super.onLayout(changed, l, t, r, b); | |
//如果布局不发生变化就不调用这个方法 | |
if (changed){ | |
this.scrollTo(mMenuWidth, 0); | |
}//已经将Menu隐藏到左侧 | |
} | |
public boolean onTouchEvent(MotionEvent ev) { | |
int action = ev.getAction(); | |
switch (action) { | |
case MotionEvent.ACTION_UP: | |
//ScrollX是隐藏在左边的宽度 | |
int ScrollX = getScrollX(); | |
if(ScrollX>=mMenuWidth/2) | |
{//菜单完全显示 | |
this.smoothScrollTo(mMenuWidth, 0); | |
}else{//菜单隐藏 | |
this.smoothScrollTo(0, 0); | |
} | |
return true; | |
} | |
return super.onTouchEvent(ev); | |
} | |
} | |
MainActivity部分,唯一需要做的就是导入这个包: | |
package com.Harris.qqslidingview; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//main.xml部分 | |
<RelativeLayout 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" | |
> | |
<com.Harris.qqsliding_view.SlidingMenu | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<include layout="@layout/left_menu" | |
/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/qq" | |
></LinearLayout> | |
</LinearLayout> | |
</com.Harris.qqsliding_view.SlidingMenu > | |
</RelativeLayout> | |
//layout文件夹里面还需要有一个left_menu.xml,代码如下 | |
<RelativeLayout 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:background="@drawable/img_frame_background" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="TRUE" | |
android:orientation="vertical"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/id_img1" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_centerVertical="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:src="@drawable/img_1"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="第一个item" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_marginLeft="20dp" | |
android:layout_toRightOf="@id/id_img1" | |
android:layout_centerVertical="true" | |
/> | |
</RelativeLayout> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/id_img2" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_centerVertical="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:src="@drawable/img_2"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="第2个item" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_marginLeft="20dp" | |
android:layout_toRightOf="@id/id_img2" | |
android:layout_centerVertical="true" | |
/> | |
</RelativeLayout> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/id_img3" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_centerVertical="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:src="@drawable/img_3"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="第3个item" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_marginLeft="20dp" | |
android:layout_toRightOf="@id/id_img3" | |
android:layout_centerVertical="true" | |
/> | |
</RelativeLayout> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/id_img4" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_centerVertical="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:src="@drawable/img_4"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="第4个item" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_marginLeft="20dp" | |
android:layout_toRightOf="@id/id_img4" | |
android:layout_centerVertical="true" | |
/> | |
</RelativeLayout> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/id_img5" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_centerVertical="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:src="@drawable/img_5"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="第5个item" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_marginLeft="20dp" | |
android:layout_toRightOf="@id/id_img5" | |
android:layout_centerVertical="true" | |
/> | |
</RelativeLayout> | |
</LinearLayout> | |
</RelativeLayout> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment