使用swipemenulistview实现列表的左右滑动
生活随笔
收集整理的這篇文章主要介紹了
使用swipemenulistview实现列表的左右滑动
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天從網(wǎng)上找到一個第三方控件swipemenulistview,封裝好的一個控件,可以實現(xiàn)列表的左右滑動,模仿qq的列表效果
下載地址為:https://github.com/baoyongzhang/SwipeMenuListView
我們下載好之后,將這個項目導(dǎo)入到我們的工程當(dāng)中去,
我個人感覺比較重要的是SwipeMenuLayout這個類,在這個類當(dāng)中重寫了好多方法,才能實現(xiàn)我們列表選項的左右滑動。
?
@Overridepublic void computeScroll() {if (state == STATE_OPEN) {if (mOpenScroller.computeScrollOffset()) {// 要是沒有滾動完 就啟動滾動的動畫 swipe(mOpenScroller.getCurrX());postInvalidate();// //必須調(diào)用該方法,否則不一定能看到滾動效果 }} 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();}// // 創(chuàng)建open動畫public void smoothOpenMenu() {state = STATE_OPEN;mOpenScroller.startScroll(-mContentView.getLeft(), 0,mMenuView.getWidth(), 0, 350);postInvalidate();}// 創(chuàng)建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());}}?
這只是部分比較重要的代碼,在我們工程中,我們?nèi)绾稳フ{(diào)用它
private void InitcehuaListView(View view) {// 初始化listView = (SwipeMenuListView) view.findViewById(R.id.listView);// ///// 這個是創(chuàng)建了一個滑動菜單的的listviewSwipeMenuCreator creator = new SwipeMenuCreator() {@Overridepublic void create(SwipeMenu menu) {ListViewMenuCreate(menu);}};// set creator listView.setMenuCreator(creator);// listview要添加menu }我們在創(chuàng)建的時候跳轉(zhuǎn)到我們的ListViewMenuCreate這個方法 ?
// 值得注意的是 每一個listview的item創(chuàng)建的時候 SwipeMenu就創(chuàng)建了一次private void ListViewMenuCreate(SwipeMenu menu) {SwipeMenuItem kankanItem = new SwipeMenuItem(getActivity().getApplicationContext());// set item backgroundkankanItem.setBackground(new ColorDrawable(Color.rgb(0x33,0x66, 0xcc)));// 設(shè)置背景顏色// set item width// kankanItem.setWidth(dp2px(60));// 設(shè)置寬度 kankanItem.setWidth(SyllabusMethod.dp2px(60, getResources()));// set item titlekankanItem.setTitle("添加");// 設(shè)置第一個標(biāo)題// set item title fontsizekankanItem.setTitleSize(18);// 設(shè)置標(biāo)題文字的大小// set item title font colorkankanItem.setTitleColor(Color.WHITE);// 設(shè)置標(biāo)題顏色// add to menumenu.addMenuItem(kankanItem);// 添加標(biāo)題到menu類中SwipeMenuItem showItem = new SwipeMenuItem(getActivity().getApplicationContext());// set item backgroundshowItem.setBackground(new ColorDrawable(Color.rgb(0xC9,0xC9, 0xCE)));// 設(shè)置背景顏色// set item width// showItem.setWidth(dp2px(60));// 設(shè)置寬度showItem.setWidth(SyllabusMethod.dp2px(60, getResources()));// set item titleshowItem.setTitle("刪除");// 設(shè)置第一個標(biāo)題// set item title fontsizeshowItem.setTitleSize(18);// 設(shè)置標(biāo)題文字的大小// set item title font colorshowItem.setTitleColor(Color.WHITE);// 設(shè)置標(biāo)題顏色// add to menumenu.addMenuItem(showItem);// 添加標(biāo)題到menu類中 }我們將item加入到我們的menu中來,然后我們再去設(shè)置我們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();}}我們在相應(yīng)的value中添加我們需要跳轉(zhuǎn)的方法即可。然后我們就實現(xiàn)了列表的左右滑動
?
?
作者:杰瑞教育出處:http://www.cnblogs.com/jerehedu/?
本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
轉(zhuǎn)載于:https://www.cnblogs.com/jerehedu/p/4440602.html
總結(jié)
以上是生活随笔為你收集整理的使用swipemenulistview实现列表的左右滑动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Glusterfs入门
- 下一篇: 基于mysql 5.5+mysql-ma