实现几个布局的滑动切换功能的简单代码
mainframe.xml文件:
<FrameLayout
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:orientation="vertical"
android:gravity="center">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myViewFlipper">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pic06"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期"
android:id="@+id/bt_date"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textdate"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pic05"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间"
android:id="@+id/bt_time"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/texttime"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pic07"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表"
android:id="@+id/bt_list"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gridview"
android:id="@+id/bt_gridview"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看图片"
android:id="@+id/bt_pic"/>
</LinearLayout>
</ViewFlipper>
</FrameLayout>
Activity类:
public class MainFrameActivity extends Activity {
private ViewFlipper viewFlipper=null;
private float startX,endX;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainframe);
viewFlipper=(ViewFlipper) findViewById(R.id.myViewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action=event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
startX=event.getX();
break;
case MotionEvent.ACTION_UP:
endX=event.getX();
if(startX-endX>20){//向左滑动
viewFlipper.showNext();
}else if(endX-startX>20){//向右滑动
viewFlipper.showPrevious();
}
break;
}
return super.onTouchEvent(event);
}
}