玩手机QQ多了,越来越喜欢他的侧滑效果,所以就自己模仿做了一个效果。文章大不凡来源于点击打开链接,感谢这位兄弟的例子。
package com.example.sidelip;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity implements OnTouchListener {
private android.widget.LinearLayout.LayoutParams menuParams;
private android.widget.LinearLayout.LayoutParams contentParams;
LinearLayout menuLayout;
LinearLayout contentLayout;
private int size ; //设备的宽度
private float startX;
private float endX;
private float translateX;
private float moveX;
private boolean menushow=false;
private int menuPanding=400;
private int maxMove=400;
private ListView menulist;
private ListView contentlist;
private String[] menus={"list1","list2","list3"};
private int[] Ids={R.drawable.tab1,
R.drawable.tab2,
R.drawable.tab3};
private String[] contents={"content1","content2","content3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("ceshi", "ceshoi");
init();
}
private void init() {
// TODO Auto-generated method stub
/*
* 关于布局的
*/
menuLayout=(LinearLayout) findViewById(R.id.menu);
contentLayout=(LinearLayout) findViewById(R.id.content);
menuParams=(LinearLayout.LayoutParams) menuLayout.getLayoutParams();
contentParams=(LinearLayout.LayoutParams) contentLayout.getLayoutParams();
WindowManager wm=(WindowManager) getSystemService(WINDOW_SERVICE);
size=wm.getDefaultDisplay().getWidth();
menuParams.width=menuPanding;
menuParams.leftMargin=-menuPanding;
contentParams.width=size;
contentLayout.setOnTouchListener(this);
/*
* 数据绑定的
*/
menulist=(ListView) findViewById(R.id.menuList);
contentlist=(ListView) findViewById(R.id.contentList);
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getApplicationContext(),
R.layout.contentitem,
contents);
contentlist.setAdapter(arrayAdapter);
List<Map<String,Object>> menulists= new ArrayList<Map<String,Object>>();
for(int i=0;i<Ids.length;i++){
Map item=new HashMap<String, Object>();
item.put("image", Ids[i]);
item.put("text", contents[i]);
menulists.add(item);
}
SimpleAdapter sm=new SimpleAdapter(getApplicationContext(),
menulists,
R.layout.menuitem,
new String[]{"image","text"},
new int[]{R.id.menuimage,R.id.menuText}
);
menulist.setAdapter(sm);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
startX=event.getRawX();
Log.d("show", "手指按下!");
break;
case MotionEvent.ACTION_MOVE:
moveX=event.getRawX();
translateX=moveX-startX;
if(!menushow&&translateX>0){
if(translateX>maxMove){
menuParams.leftMargin=0;
menushow=true;
}else{
menuParams.leftMargin=(int) (translateX-menuPanding);
}
};
if(menushow&&translateX<0){
if(translateX<-maxMove){
menuParams.leftMargin=-menuPanding;
menushow=false;
}else{
menuParams.leftMargin=(int) translateX;
}
};
menuLayout.setLayoutParams(menuParams);
Log.d("show", "手指移动!");
break;
case MotionEvent.ACTION_UP:
endX=event.getRawX();
translateX=endX-startX;
if(translateX<maxMove&&translateX>-maxMove){
menuParams.leftMargin=-menuPanding;
menushow=false;
}
menuLayout.setLayoutParams(menuParams);
break;
}
return true;
}
private void showMenu(float x) {
// TODO Auto-generated method stub
if(x>0&&!menushow){
menuParams.leftMargin=(int) (x-menuPanding);
menuLayout.setLayoutParams(menuParams);
}if(x<0&&menushow){
menuParams.leftMargin=(int) x;
menuLayout.setLayoutParams(menuParams);
}
}
}
下面是activity的main布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity"
android:background="@drawable/backgroud" >
<LinearLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/menuList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff">
<ListView
android:id="@+id/contentList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
该例子中menu在左,content在右,以LinearLayout 布局排列,主要通过设置menuParams.leftMargin改变左边界,实现滑动效果。