Android source code analysis-SwipeMenuListView imitation QQ chat left slide
Introduction:
I haven't written a blog for a long time. I have been lazy recently and don't want to write a blog. But I am reading a book and reading some advanced Android books. Here I will also recommend some books suitable for advanced reading, I believe it will help you.
1. The English biography of Android group-Xu Yisheng
2. Android development art exploration-ren yugang
3. Android source code design model-He honghui and Guo aimin
The above are for Android development. If you have thoroughly understood the above three items, I believe you will be able to soar in the Android world.
The following are some classic books that are essential for your programming:
1. Clean code
2. refactoring to improve code-based Design
3. MacTalk crossing boundaries
Now, let's go to the topic. What I want to share with you today is: QQ-like chat to achieve a left-sliding effect. Let's take a look at the effect first:
Implementation:
1. Define the sliding option view
The sliding tab is displayed by listening to the sliding gesture (the tab is created first)
Here, we only paste the core code:
Public boolean onSwipe (MotionEvent event) {mGestureDetector. onTouchEvent (event); switch (event. getAction () {case MotionEvent. ACTION_DOWN: mDownX = (int) event. getX (); isFling = false; break; case MotionEvent. ACTION_MOVE: int dis = (int) (mDownX-event. getX (); if (state = STATE_OPEN) {dis + = mMenuView. getWidth () ;}swipe (dis); break; case MotionEvent. ACTION_UP: if (isFling | (mDownX-event. getX ()> (mMenuView. getWidth ()/2) {// open the smoothOpenMenu ();} else {// close the smoothCloseMenu (); return false;} break;} return true ;}
2. Create a tab:
public void createMenu(SwipeMenu menu) { // Test Code SwipeMenuItem item = new SwipeMenuItem(mContext); item.setTitle("Item 1"); item.setBackground(new ColorDrawable(Color.GRAY)); item.setWidth(300); menu.addMenuItem(item); item = new SwipeMenuItem(mContext); item.setTitle("Item 2"); item.setBackground(new ColorDrawable(Color.RED)); item.setWidth(300); menu.addMenuItem(item); }
3. Add the tab to the layout:
private void addItem(SwipeMenuItem item, int id) { LayoutParams params = new LayoutParams(item.getWidth(), LayoutParams.MATCH_PARENT); LinearLayout parent = new LinearLayout(getContext()); parent.setId(id); parent.setGravity(Gravity.CENTER); parent.setOrientation(LinearLayout.VERTICAL); parent.setLayoutParams(params); parent.setBackgroundDrawable(item.getBackground()); parent.setOnClickListener(this); addView(parent); if (item.getIcon() != null) { parent.addView(createIcon(item)); } if (!TextUtils.isEmpty(item.getTitle())) { parent.addView(createTitle(item)); } }
4. Call in Activity:
Required creator = new ISwipeMenuCreator () {@ Override public void create (SwipeMenu menu) {// create the slide option SwipeMenuItem showItem = new SwipeMenuItem (getApplicationContext ()); // set the option background showItem. setBackground (new ColorDrawable (Color. rgb (0xC9, 0xC9, 0xCE); // you can specify showItem as the option width. setWidth (HankkinUtil. dp2px (90, MainActivity. this); // set the option title showItem. setTitle ("Show"); // set the option title showItem. setTitleSize (18); // set the option title Color showItem. setTitleColor (Color. WHITE); // Add option menu. addMenuItem (showItem); // creates the delete option SwipeMenuItem deleteItem = new SwipeMenuItem (getApplicationContext (); deleteItem. setBackground (new ColorDrawable (Color. rgb (0xF9, 0x3F, 0x25); deleteItem. setWidth (HankkinUtil. dp2px (90, MainActivity. this); deleteItem. setIcon (R. drawable. ic_delete); menu. addMenuItem (deleteItem) ;}}; menuListView. setMenuCreator (creator );
5. Tab click event:
MenuListView. setOnMenuItemClickListener (new SwipeMenuListView. onMenuItemClickListener () {@ Override public void onMenuItemClick (int position, SwipeMenu menu, int index) {switch (index) {case 0: // The first option HankkinUtil. showToast (MainActivity. this, data. get (position ). getTitle (); break; case 1: // The second option data. remove (position); adapter. notifyDataSetChanged (); break ;}}});
The pull-down refresh in the Code is pulled and loaded. The small Editor uses SwipeRefreshLayout. Note that sliding conflicts occur between the left slide and pull-down refresh of the tab. Here, the small Editor hasn't found a better solution.