欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发教程之实现滑动关闭fragment示例

程序员文章站 2023-10-28 20:13:34
主要代码:(有注释) 复制代码 代码如下:package com.example.checkboxtest; import android.annotation.sup...

主要代码:(有注释)

复制代码 代码如下:

package com.example.checkboxtest;

import android.annotation.suppresslint;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.os.handler;
import android.os.message;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.widget.toast;

public class myview extends viewgroup {
    view leftview = null;
    view rightview = null;

    public myview(context context, attributeset attrs) {
        super(context, attrs);
        view view = new view(context, attrs);
        view.setbackgroundcolor(color.black);
        this.addview(view, 0);
    }

    /**
     * 测量
     */
    @suppresslint("drawallocation")
    @override
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
        if (getchildcount() != 2) {
            try {
                // 自定义exception
                throw new exception() {
                    @override
                    public void printstacktrace() {
                        system.err.println("myview中只能存在一个view");
                        super.printstacktrace();
                    }

                };
            } catch (exception e) {
                e.printstacktrace();
            }
        }
        leftview = getchildat(0);
        // 设置leftview的高和宽
        leftview.measure(widthmeasurespec, heightmeasurespec);
        rightview = getchildat(1);
        // 设置rightview的高和宽
        rightview.measure(widthmeasurespec, heightmeasurespec);
        super.onmeasure(widthmeasurespec, heightmeasurespec);
    }

    /**
     * 布局
     */
    @suppresslint("newapi")
    @override
    protected void onlayout(boolean changed, int l, int t, int r, int b) {
        system.out.println(l + "," + t + "," + r + "," + b);
        if (leftview != null & rightview != null) {
            // 设置leftview的位置,在屏幕右侧(初始不可见)
            leftview.layout(-r, 0, 0, b);
            // 设置rightview的位置,在屏幕中
            rightview.layout(l, t, r, b);
        }
    }

    @override
    protected void ondraw(canvas canvas) {
        super.ondraw(canvas);

    }

    @suppresslint("newapi")
    @override
    public boolean ontouchevent(motionevent event) {
        final float x = event.getx();
        float y = event.gety();
        switch (event.getaction()) {

        case motionevent.action_move:
            system.out.println("x:" + x);
            if (x < 100) {
                scrollto(0, 0);
            } else if (x > rightview.getmeasuredwidth() - 100) {// 当用户滑动至离右边缘100时,页面关闭
                new thread(new runnable() {// 新建线程,滑动关闭
                            @override
                            public void run() {
                                for (int i = 0;; i++) {
                                    try {
                                        thread.sleep(10);// rightview每隔10ms右移3,保证滑动流畅
                                    } catch (interruptedexception e) {
                                        e.printstacktrace();
                                    }
                                    int len = (int) (x + 3 * i);
                                    // system.out.println("len:" + len);
                                    message message = new message();// android中非ui线程不允许直接操作控件,可以将消息发送至主线程的handler类
                                    if (len >= rightview.getmeasuredwidth()) {
                                        message.what = 1;
                                        handler.sendmessage(message);// 发送消息
                                                                        // 关闭view
                                        break;
                                    } else {
                                        message.what = 0; // 发送消息 自动滑动
                                        handler.sendmessage(message);
                                    }
                                }
                            }
                        }).start();
            } else {
                scrollto((int) -x, 0);
                // 计算透明度信息
                float alpha = (float) (1.0 - (float) (1.0 / 400) * x);
                // system.out.println("alpha:" + al);
                // 设置透明度
                leftview.setalpha(alpha);
            }
            break;
        }
        // 设置true,消费event事件,不在向外传递
        return true;
    }

    @suppresslint("handlerleak")
    handler handler = new handler() {
        @suppresslint("newapi")
        @override
        public void handlemessage(message msg) {
            if (msg.what == 0) {
                scrollby(-3, 0);// viewgroup向右滑动3
            } else if (msg.what == 1) {
                toast.maketext(getcontext(), "关闭", 50).show();
                setvisibility(view.gone);// 设置viewgroup不可见(隐藏)
            }
        }

    };
}

使用:

复制代码 代码如下:

<com.example.checkboxtest.myview
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <view
            android:id="@+id/rightview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ff0000" />
    </com.example.checkboxtest.myview>

要显示底下的activity界面需要使用fragment的add方法

复制代码 代码如下:

//新建一个fragment
fragment bfragment = new bfragment();
getfragmentmanager().begintransaction().add(r.id.fragment, bfragment).commit();