使用swipemenulistview實現列表的左右滑動,swipemenulistview

來源:互聯網
上載者:User

使用swipemenulistview實現列表的左右滑動,swipemenulistview

  今天從網上找到一個第三方控制項swipemenulistview,封裝好的一個控制項,可以實現列表的左右滑動,模仿qq的列表效果

為:https://github.com/baoyongzhang/SwipeMenuListView

我們下載好之後,將這個項目匯入到我們的工程當中去,

我個人感覺比較重要的是SwipeMenuLayout這個類,在這個類當中重寫了好多方法,才能實現我們列表選項的左右滑動。

 

    @Override    public void computeScroll() {        if (state == STATE_OPEN) {            if (mOpenScroller.computeScrollOffset()) {// 要是沒有滾動完 就啟動滾動的動畫                swipe(mOpenScroller.getCurrX());                postInvalidate();// //必須調用該方法,否則不一定能看到滾動效果            }        } else {            if (mCloseScroller.computeScrollOffset()) {                swipe(mBaseX - mCloseScroller.getCurrX());                postInvalidate();            }        }    }    public void smoothCloseMenu() {        state = STATE_CLOSE;//        mBaseX = -mContentView.getLeft();//        mCloseScroller.startScroll(0, 0, mBaseX, 0, 350);// 滾動的final位置        postInvalidate();    }    // ////////////////////////////////////////////    // 建立open動畫    public void smoothOpenMenu() {        state = STATE_OPEN;        mOpenScroller.startScroll(-mContentView.getLeft(), 0,                mMenuView.getWidth(), 0, 350);        postInvalidate();    }    // 建立close動畫    public void closeMenu() {        if (mCloseScroller.computeScrollOffset()) {            mCloseScroller.abortAnimation();        }        if (state == STATE_OPEN) {            state = STATE_CLOSE;            swipe(0);        }    }    public void openMenu() {        if (state == STATE_CLOSE) {            state = STATE_OPEN;            swipe(mMenuView.getWidth());        }    }

 

  這隻是部分比較重要的代碼,在我們工程中,我們如何去調用它

private void InitcehuaListView(View view) {// 初始化        listView = (SwipeMenuListView) view.findViewById(R.id.listView);        // ///////////////////////////////////////////////////////////////////        // 這個是建立了一個滑動菜單的的listview        SwipeMenuCreator creator = new SwipeMenuCreator() {            @Override            public void create(SwipeMenu menu) {                ListViewMenuCreate(menu);            }        };        // set creator        listView.setMenuCreator(creator);// listview要添加menu}

  我們在建立的時候跳轉到我們的ListViewMenuCreate這個方法  

    // 值得注意的是 每一個listview的item建立的時候 SwipeMenu就建立了一次    private void ListViewMenuCreate(SwipeMenu menu) {                            SwipeMenuItem kankanItem = new SwipeMenuItem(getActivity()                            .getApplicationContext());                    // set item background                    kankanItem.setBackground(new ColorDrawable(Color.rgb(0x33,                            0x66, 0xcc)));// 設定背景顏色                    // set item width                    // kankanItem.setWidth(dp2px(60));// 設定寬度                    kankanItem.setWidth(SyllabusMethod                            .dp2px(60, getResources()));                    // set item title                    kankanItem.setTitle("添加");// 設定第一個標題                    // set item title fontsize                    kankanItem.setTitleSize(18);// 設定標題文字的大小                    // set item title font color                    kankanItem.setTitleColor(Color.WHITE);// 設定標題顏色                    // add to menu                    menu.addMenuItem(kankanItem);// 添加標題到menu類中                    SwipeMenuItem showItem = new SwipeMenuItem(getActivity()                            .getApplicationContext());                    // set item background                    showItem.setBackground(new ColorDrawable(Color.rgb(0xC9,                            0xC9, 0xCE)));// 設定背景顏色                    // set item width                    // showItem.setWidth(dp2px(60));// 設定寬度                    showItem.setWidth(SyllabusMethod.dp2px(60, getResources()));                    // set item title                    showItem.setTitle("刪除");// 設定第一個標題                    // set item title fontsize                    showItem.setTitleSize(18);// 設定標題文字的大小                    // set item title font color                    showItem.setTitleColor(Color.WHITE);// 設定標題顏色                    // add to menu                    menu.addMenuItem(showItem);// 添加標題到menu類中                    }

  我們將item加入到我們的menu中來,然後我們再去設定我們item的點擊事件

listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {            public void onMenuItemClick(int position, SwipeMenu menu, int index) {                ListmenuTimes = -1;                String value = menu.getMenuItem(index).getTitle().toString();                if (value.equals("添加")) {                    appliction.setCurrentchickpos(position + 1);                    Intent addkchengintent = new Intent(getActivity(),                            AddsyllabusActivity.class);                    startActivityForResult(addkchengintent,                            CommonCS.INTENT_GOTO_ADDSYLLABUS_CODE);                } else if (value.equals("刪除")) {                    boolean flag = SomeSqliteMethod.deleteCurrentItem(                            getActivity(), appliction.getCurrentdate(),                            (position + 1));                    if (flag) {                        Toast.makeText(getActivity().getApplicationContext(),                                "刪除成功", Toast.LENGTH_SHORT).show();                        if (!Todaysyllabuslist.isEmpty()) {                            Todaysyllabuslist.clear();                        }                        int currentdate = appliction.getCurrentdate();                        InitMYTodayListData(currentdate);                        showlist();                    }                }

  我們在相應的value中添加我們需要跳轉的方法即可。然後我們就實現了列表的左右滑動

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.