滑动视图ViewFlipper

实现几个布局的滑动切换功能的简单代码
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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值