使用swipemenulistview实现列表的左右滑动
生活随笔
收集整理的這篇文章主要介紹了
使用swipemenulistview实现列表的左右滑动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天從網上找到一個第三方控件swipemenulistview,封裝好的一個控件,可以實現列表的左右滑動,模仿qq的列表效果
下載地址為:https://github.com/baoyongzhang/SwipeMenuListView
我們下載好之后,將這個項目導入到我們的工程當中去,
我個人感覺比較重要的是SwipeMenuLayout這個類,在這個類當中重寫了好多方法,才能實現我們列表選項的左右滑動。
?
@Overridepublic 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);// ///// 這個是創建了一個滑動菜單的的listviewSwipeMenuCreator creator = new SwipeMenuCreator() {@Overridepublic 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 backgroundkankanItem.setBackground(new ColorDrawable(Color.rgb(0x33,0x66, 0xcc)));// 設置背景顏色// set item width// kankanItem.setWidth(dp2px(60));// 設置寬度 kankanItem.setWidth(SyllabusMethod.dp2px(60, getResources()));// set item titlekankanItem.setTitle("添加");// 設置第一個標題// set item title fontsizekankanItem.setTitleSize(18);// 設置標題文字的大小// set item title font colorkankanItem.setTitleColor(Color.WHITE);// 設置標題顏色// add to menumenu.addMenuItem(kankanItem);// 添加標題到menu類中SwipeMenuItem showItem = new SwipeMenuItem(getActivity().getApplicationContext());// set item backgroundshowItem.setBackground(new ColorDrawable(Color.rgb(0xC9,0xC9, 0xCE)));// 設置背景顏色// set item width// showItem.setWidth(dp2px(60));// 設置寬度showItem.setWidth(SyllabusMethod.dp2px(60, getResources()));// set item titleshowItem.setTitle("刪除");// 設置第一個標題// set item title fontsizeshowItem.setTitleSize(18);// 設置標題文字的大小// set item title font colorshowItem.setTitleColor(Color.WHITE);// 設置標題顏色// add to menumenu.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中添加我們需要跳轉的方法即可。然后我們就實現了列表的左右滑動
?
?
作者:杰瑞教育出處:http://www.cnblogs.com/jerehedu/?
本文版權歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
轉載于:https://www.cnblogs.com/jerehedu/p/4440602.html
總結
以上是生活随笔為你收集整理的使用swipemenulistview实现列表的左右滑动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Glusterfs入门
- 下一篇: Selenium WebDriver问题