使用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());
}
}</pre>
這只是部分比較重要的代碼,在我們工程中,我們如何去調用它
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中添加我們需要跳轉的方法即可。然后我們就實現了列表的左右滑動
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!