仿微信侧滑菜单slidemenu

仿微信侧滑菜单slidemenu,第1张

概述直接上代码 slidemenupackagecom.example.myapplication.customview;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.LayoutInflater;importandroid.view.

直接上代码 

slIDemenu

package com.example.myapplication.customvIEw;import androID.content.Context;import androID.content.res.TypedArray;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.linearLayout;import androID.Widget.Scroller;import androID.Widget.TextVIEw;import com.example.myapplication.R;public class SlIDeMenu extends VIEwGroup implements VIEw.OnClickListener {    private static final String TAG = "SlIDeMenu";    private int function;    private VIEw contentVIEw;    private linearLayout functionVIEw;    private Scroller scroller;    float downX = 0;    float downY = 0;    public boolean isopen() {        return isOpen;    }    private boolean isOpen = false;    private Direction direction = Direction.NONE;    private float interceptDownX;    enum Direction {        left, RIGHT, NONE    }    public SlIDeMenu(Context context) {        this(context, null);    }    public SlIDeMenu(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public SlIDeMenu(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        // 1、添加属性        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlIDeMenu);        function = a.getInt(R.styleable.SlIDeMenu_function, 0);        a.recycle();    }    @OverrIDe    protected voID onFinishInflate() {        super.onFinishInflate();        ///2、将VIEw加入进来        contentVIEw = getChildAt(0);        functionVIEw = (linearLayout) LayoutInflater.from(getContext()).inflate(R.layout.slIDe_item_layout, this, false);        addVIEw(functionVIEw);        scroller = new Scroller(getContext());        initFunctionVIEw();        Log.d(TAG, "get child count===> " + getChildCount());    }    private voID initFunctionVIEw() {        TextVIEw read = functionVIEw.findVIEwByID(R.ID.read);        TextVIEw delete = functionVIEw.findVIEwByID(R.ID.delete);        TextVIEw top = functionVIEw.findVIEwByID(R.ID.top);        delete.setonClickListener(this);        read.setonClickListener(this);        top.setonClickListener(this);        initSlIDeVIEwVisible(delete, read, top);    }    private voID initSlIDeVIEwVisible(TextVIEw delete, TextVIEw read, TextVIEw top) {        final int top = 0x03;        final int DELETE = 0x30;        final int READ = 0x50;        final int top_DELETE = top | DELETE;        final int top_READ = top | READ;        final int READ_DELETE = READ | DELETE;        final int top_READ_DELETE = top | READ | DELETE;        switch (function) {            case DELETE:                delete.setVisibility(VISIBLE);                break;            case READ:                read.setVisibility(VISIBLE);                break;            case top:                top.setVisibility(VISIBLE);                break;            case top_DELETE:                top.setVisibility(VISIBLE);                delete.setVisibility(VISIBLE);                break;            case top_READ:                top.setVisibility(VISIBLE);                read.setVisibility(VISIBLE);                break;            case READ_DELETE:                read.setVisibility(VISIBLE);                delete.setVisibility(VISIBLE);                break;            case top_READ_DELETE:                top.setVisibility(VISIBLE);                read.setVisibility(VISIBLE);                delete.setVisibility(VISIBLE);                break;        }    }    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);        int parentHeightSizeSpec = MeasureSpec.getSize(heightmeasureSpec);        //3、测量第一个孩子        LayoutParams layoutParams = contentVIEw.getLayoutParams();        int height = layoutParams.height;        int heightSpec;        if (height == LayoutParams.MATCH_PARENT) {            heightSpec = MeasureSpec.makeMeasureSpec(parentHeightSizeSpec, MeasureSpec.AT_MOST);        } else if (height == MeasureSpec.EXACTLY) {            heightSpec = MeasureSpec.makeMeasureSpec(parentHeightSizeSpec, MeasureSpec.EXACTLY);        } else {            heightSpec = MeasureSpec.makeMeasureSpec(parentHeightSizeSpec, MeasureSpec.AT_MOST);        }        measureChild(contentVIEw, wIDthMeasureSpec, heightSpec);        //4、测量第二个孩子        int measuredHeight = contentVIEw.getMeasuredHeight();        // 第二个孩子的宽度为最大宽度的3/4        // 根据具体的孩子来显示具体的宽度        int visibleCount = 0;        for (int i = 0; i < functionVIEw.getChildCount(); i++) {            if (functionVIEw.getChildAt(i).getVisibility() == VIEw.VISIBLE) {                visibleCount++;            }        }        Log.d(TAG, "child count ==== >" + visibleCount);        int functionSize = wIDthMeasureSpec * visibleCount / 4;        int functionWIDth = MeasureSpec.makeMeasureSpec(functionSize, MeasureSpec.EXACTLY);        int functionHeight = MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY);        measureChild(functionVIEw, functionWIDth, functionHeight);    }    @OverrIDe    protected voID onLayout(boolean changed, int l, int t, int r, int b) {        // 5、布局第一个孩子        int contentleft = 0;        int contenttop = 0;        int contentRight = contentleft + contentVIEw.getMeasureDWIDth();        int contentBottom = contentVIEw.getMeasuredHeight();        contentVIEw.layout(contentleft, contenttop, contentRight, contentBottom);        int functiontop = 0;        int functionRight = contentRight + functionVIEw.getMeasureDWIDth();        functionVIEw.layout(contentRight, functiontop, functionRight, contentBottom);    }    @OverrIDe    public boolean ontouchEvent(MotionEvent event) {        //6、计算按下的位置与移动的位置移动当前的视图        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                downX = event.getX();                downY = event.getY();                break;            case MotionEvent.ACTION_MOVE:                float moveX = event.getX();                float moveY = event.getY();                int dx = (int) (moveX - downX);                if (dx > 0) {                    direction = Direction.RIGHT;                } else if (dx < 0) {                    direction = Direction.left;                }                float hasBeenScrolled = getScrollX();                int resultX = (int) (hasBeenScrolled - dx);                if (resultX <= 0) {                    scrollTo(0, 0);                } else if (resultX > functionVIEw.getMeasureDWIDth()) {                    scrollTo(functionVIEw.getMeasureDWIDth(), 0);                } else {                    scrollBy(-dx, 0);                }                downX = moveX;                downY = moveY;                break;            case MotionEvent.ACTION_UP:                int scrollX = getScrollX();                Log.d(TAG, "scroll > >" + scrollX);                if (isOpen) {                    //若是打开的                    if (direction == Direction.left) {                        open();                    } else if (direction == Direction.RIGHT) {                        if (scrollX <= functionVIEw.getMeasureDWIDth() * 4 / 5) {                            close();                        } else {                            open();                        }                    }                } else {                    //默认是关闭的                    if (direction == Direction.left) {                        //判断当前滑动的距离                        if (scrollX >= functionVIEw.getMeasureDWIDth() / 5) {                            open();                        } else {                            close();                        }                    } else if (direction == Direction.RIGHT) {                        close();                    }                }                break;        }        return true;    }    @OverrIDe    public boolean onIntercepttouchEvent(MotionEvent ev) {        //事件冲突        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                interceptDownX = ev.getX();                float interceptDownY = ev.getY();                break;            case MotionEvent.ACTION_MOVE:                float interceptMoveX = ev.getX();                float interceptMoveY = ev.getY();                int intercept = (int) (interceptMoveX - interceptDownX);                if (Math.abs(intercept) > 0) {                    return true;                }                break;            case MotionEvent.ACTION_UP:                break;        }        return super.onIntercepttouchEvent(ev);    }    private voID open() {        scroller.startScroll(getScrollX(), 0, functionVIEw.getMeasureDWIDth() - getScrollX(), 0, 500);        isOpen = true;        invalIDate();    }    private voID close() {        scroller.startScroll(getScrollX(), 0, -getScrollX(), 0, 500);        isOpen = false;        invalIDate();    }    @OverrIDe    public voID onClick(VIEw v) {        close();        switch (v.getID()) {            case R.ID.read:                onItemClickListener.onReadClick();                break;            case R.ID.delete:                onItemClickListener.onDeleteClick();                break;            case R.ID.top:                onItemClickListener.ontopClick();                break;        }    }    @OverrIDe    public voID computeScroll() {        super.computeScroll();        if (scroller.computeScrollOffset()) {            int currX = scroller.getCurrX();            scrollTo(currX, 0);            invalIDate();        }    }    // 通过接口将事件传递出去    private onItemClickListener onItemClickListener;    public voID setonItemClickListener(SlIDeMenu.onItemClickListener onItemClickListener) {        this.onItemClickListener = onItemClickListener;    }    public interface onItemClickListener {        voID onDeleteClick();        voID onReadClick();        voID ontopClick();    }}

属性 

<declare-styleable name="SlIDeMenu">    <attr name="function" format="flags">        <flag name="delete" value="0x30" />        <flag name="read" value="0x50" />        <flag name="top" value="0x03" />    </attr></declare-styleable>

 

布局

activity_main

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    tools:context=".MainActivity">    <com.example.myapplication.customvIEw.SlIDeMenu        androID:ID="@+ID/slIDe_menu"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:function="top|delete">        <TextVIEw            androID:ID="@+ID/content_tv"            androID:layout_wIDth="match_parent"            androID:layout_height="100dp"            androID:background="#dddddd"            androID:gravity="center"            androID:text="群成员"            androID:textcolor="#000"            androID:textSize="20sp" />    </com.example.myapplication.customvIEw.SlIDeMenu></linearLayout>

slIDe_item_layout.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="horizontal">    <TextVIEw        androID:ID="@+ID/read"        androID:layout_wIDth="0dp"        androID:visibility="gone"        androID:layout_height="match_parent"        androID:layout_weight="1"        androID:background="#008800"        androID:gravity="center"        androID:text="已读"        androID:textcolor="#fff"        androID:textSize="20dp" />    <TextVIEw        androID:ID="@+ID/top"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"        androID:background="#666666"        androID:visibility="gone"        androID:gravity="center"        androID:text="置顶"        androID:textcolor="#fff"        androID:textSize="20dp" />    <TextVIEw        androID:visibility="gone"        androID:ID="@+ID/delete"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"        androID:background="#ff0000"        androID:gravity="center"        androID:text="删除"        androID:textcolor="#fff"        androID:textSize="20dp" /></linearLayout>

MainActivity

 

package com.example.myapplication;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.linearLayout;import androID.Widget.Toast;import com.example.myapplication.customvIEw.FlowLayout;import com.example.myapplication.customvIEw.SlIDeMenu;import com.example.myapplication.customvIEw.TextBanner;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    private String TAG = "MainActivity";    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        SlIDeMenu slIDeMenu = findVIEwByID(R.ID.slIDe_menu);        slIDeMenu.setonItemClickListener(new SlIDeMenu.onItemClickListener() {            @OverrIDe            public voID onDeleteClick() {                Log.d(TAG, "ON DELETE");            }            @OverrIDe            public voID onReadClick() {                Log.d(TAG, "ON READ");            }            @OverrIDe            public voID ontopClick() {                Log.d(TAG, "ON top");            }        });    }}

效果图  

 

总结

以上是内存溢出为你收集整理的仿微信侧滑菜单slidemenu全部内容,希望文章能够帮你解决仿微信侧滑菜单slidemenu所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存