/** * Close item * * @param position Position of list */ protected void closeAnimate(int position) { closeAnimate( swipeListView .getChildAt(position - swipeListView.getFirstVisiblePosition()) .findViewById(swipeFrontView), position); }
/** Adds new items when adapter is modified */ public void resetItems() { if (swipeListView.getAdapter() != null) { int count = swipeListView.getAdapter().getCount(); for (int i = opened.size(); i <= count; i++) { opened.add(false); openedRight.add(false); checked.add(false); } } }
/** Close all opened items */ void closeOpenedItems() { if (opened != null) { int start = swipeListView.getFirstVisiblePosition(); int end = swipeListView.getLastVisiblePosition(); for (int i = start; i <= end; i++) { if (opened.get(i)) { closeAnimate(swipeListView.getChildAt(i - start).findViewById(swipeFrontView), i); } } } }
/** Unselected choice state in item */ protected int dismiss(int position) { int start = swipeListView.getFirstVisiblePosition(); int end = swipeListView.getLastVisiblePosition(); View view = swipeListView.getChildAt(position - start); ++dismissAnimationRefCount; if (position >= start && position <= end) { performDismiss(view, position, false); return view.getHeight(); } else { pendingDismisses.add(new PendingDismissData(position, null)); return 0; } }
/** Unselected choice state in item */ protected void unselectedChoiceStates() { int start = swipeListView.getFirstVisiblePosition(); int end = swipeListView.getLastVisiblePosition(); for (int i = 0; i < checked.size(); i++) { if (checked.get(i) && i >= start && i <= end) { reloadChoiceStateInView( swipeListView.getChildAt(i - start).findViewById(swipeFrontView), i); } checked.set(i, false); } swipeListView.onChoiceEnded(); returnOldActions(); }
/** * Constructor * * @param swipeListView SwipeListView * @param swipeFrontView front view Identifier * @param swipeBackView back view Identifier */ public SwipeListViewTouchListener( SwipeListView swipeListView, int swipeFrontView, int swipeBackView) { this.swipeFrontView = swipeFrontView; this.swipeBackView = swipeBackView; ViewConfiguration vc = ViewConfiguration.get(swipeListView.getContext()); slop = vc.getScaledTouchSlop(); minFlingVelocity = vc.getScaledMinimumFlingVelocity(); maxFlingVelocity = vc.getScaledMaximumFlingVelocity(); configShortAnimationTime = swipeListView .getContext() .getResources() .getInteger(android.R.integer.config_shortAnimTime); animationTime = configShortAnimationTime; this.swipeListView = swipeListView; }
private void removePendingDismisses(int originalHeight) { // No active animations, process all pending dismisses. // Sort by descending position Collections.sort(pendingDismisses); int[] dismissPositions = new int[pendingDismisses.size()]; for (int i = pendingDismisses.size() - 1; i >= 0; i--) { dismissPositions[i] = pendingDismisses.get(i).position; } swipeListView.onDismiss(dismissPositions); ViewGroup.LayoutParams lp; for (PendingDismissData pendingDismiss : pendingDismisses) { // Reset view presentation if (pendingDismiss.view != null) { setAlpha(pendingDismiss.view, 1f); setTranslationX(pendingDismiss.view, 0); lp = pendingDismiss.view.getLayoutParams(); lp.height = originalHeight; pendingDismiss.view.setLayoutParams(lp); } } resetPendingDismisses(); }
/** * Swap choice state in item * * @param position position of list */ private void swapChoiceState(int position) { int lastCount = getCountSelected(); boolean lastChecked = checked.get(position); checked.set(position, !lastChecked); int count = lastChecked ? lastCount - 1 : lastCount + 1; if (lastCount == 0 && count == 1) { swipeListView.onChoiceStarted(); closeOpenedItems(); setActionsTo(SwipeListView.SWIPE_ACTION_CHOICE); } if (lastCount == 1 && count == 0) { swipeListView.onChoiceEnded(); returnOldActions(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { swipeListView.setItemChecked(position, !lastChecked); } swipeListView.onChoiceChanged(position, !lastChecked); reloadChoiceStateInView(frontView, position); }
/** * Moves the view * * @param deltaX delta */ public void move(float deltaX) { swipeListView.onMove(downPosition, deltaX); float posX = ViewHelper.getX(frontView); if (opened.get(downPosition)) { posX += openedRight.get(downPosition) ? -viewWidth + rightOffset : viewWidth - leftOffset; } if (posX > 0 && !swipingRight) { Log.d("SwipeListView", "change to right"); swipingRight = !swipingRight; swipeCurrentAction = swipeActionRight; if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } else { backView.setVisibility(View.VISIBLE); } } if (posX < 0 && swipingRight) { Log.d("SwipeListView", "change to left"); swipingRight = !swipingRight; swipeCurrentAction = swipeActionLeft; if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } else { backView.setVisibility(View.VISIBLE); } } if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_DISMISS) { setTranslationX(parentView, deltaX); setAlpha(parentView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / viewWidth))); } else if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { if ((swipingRight && deltaX > 0 && posX < DISPLACE_CHOICE) || (!swipingRight && deltaX < 0 && posX > -DISPLACE_CHOICE) || (swipingRight && deltaX < DISPLACE_CHOICE) || (!swipingRight && deltaX > -DISPLACE_CHOICE)) { setTranslationX(frontView, deltaX); } } else { setTranslationX(frontView, deltaX); } }
/** @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) */ @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (!isSwipeEnabled()) { return false; } if (viewWidth < 2) { viewWidth = swipeListView.getWidth(); } switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (paused && downPosition != ListView.INVALID_POSITION) { return false; } swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE; int childCount = swipeListView.getChildCount(); int[] listViewCoords = new int[2]; swipeListView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = swipeListView.getChildAt(i); child.getHitRect(rect); int childPosition = swipeListView.getPositionForView(child); // dont allow swiping if this is on the header or footer or IGNORE_ITEM_VIEW_TYPE or // enabled is false on the adapter boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition) && swipeListView.getAdapter().getItemViewType(childPosition) >= 0; if (allowSwipe && rect.contains(x, y)) { setParentView(child); setFrontView(child.findViewById(swipeFrontView)); downX = motionEvent.getRawX(); downPosition = childPosition; frontView.setClickable(!opened.get(downPosition)); frontView.setLongClickable(!opened.get(downPosition)); velocityTracker = VelocityTracker.obtain(); velocityTracker.addMovement(motionEvent); if (swipeBackView > 0) { setBackView(child.findViewById(swipeBackView)); } break; } } view.onTouchEvent(motionEvent); return true; } case MotionEvent.ACTION_UP: { if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) { break; } float deltaX = motionEvent.getRawX() - downX; velocityTracker.addMovement(motionEvent); velocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(velocityTracker.getXVelocity()); if (!opened.get(downPosition)) { if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) { velocityX = 0; } if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) { velocityX = 0; } } float velocityY = Math.abs(velocityTracker.getYVelocity()); boolean swap = false; boolean swapRight = false; if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX) { swapRight = velocityTracker.getXVelocity() > 0; Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " + swipingRight); if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) { swap = false; } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) { swap = false; } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) { swap = false; } else { swap = true; } } else if (Math.abs(deltaX) > viewWidth / 2) { swap = true; swapRight = deltaX > 0; } generateAnimate(frontView, swap, swapRight, downPosition); if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { swapChoiceState(downPosition); } velocityTracker.recycle(); velocityTracker = null; downX = 0; // change clickable front view // if (swap) { // frontView.setClickable(opened.get(downPosition)); // frontView.setLongClickable(opened.get(downPosition)); // } swiping = false; break; } case MotionEvent.ACTION_MOVE: { if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) { break; } velocityTracker.addMovement(motionEvent); velocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(velocityTracker.getXVelocity()); float velocityY = Math.abs(velocityTracker.getYVelocity()); float deltaX = motionEvent.getRawX() - downX; float deltaMode = Math.abs(deltaX); int swipeMode = this.swipeMode; int changeSwipeMode = swipeListView.changeSwipeMode(downPosition); if (changeSwipeMode >= 0) { swipeMode = changeSwipeMode; } if (swipeMode == SwipeListView.SWIPE_MODE_NONE) { deltaMode = 0; } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) { if (opened.get(downPosition)) { if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) { deltaMode = 0; } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) { deltaMode = 0; } } else { if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) { deltaMode = 0; } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) { deltaMode = 0; } } } if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE && velocityY < velocityX) { swiping = true; swipingRight = (deltaX > 0); Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " + swipingRight); if (opened.get(downPosition)) { swipeListView.onStartClose(downPosition, swipingRight); swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL; } else { if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) { swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS; } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) { swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS; } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHOICE) { swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE; } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) { swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE; } else { swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL; } swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight); } swipeListView.requestDisallowInterceptTouchEvent(true); MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (MotionEventCompat.getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); swipeListView.onTouchEvent(cancelEvent); if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } } if (swiping && downPosition != ListView.INVALID_POSITION) { if (opened.get(downPosition)) { deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset; } move(deltaX); return true; } break; } } return false; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.menu); if (PlaylistActivity.isConsumer == true) { findViewById(R.id.layout_add_song).setVisibility(View.GONE); findViewById(R.id.coverartfond).setVisibility(View.GONE); findViewById(R.id.layoutPlayListName).setVisibility(View.GONE); findViewById(R.id.layoutMenuTitle).setVisibility(View.GONE); findViewById(R.id.add_Button_song).setBackgroundColor(View.VISIBLE); TextView textView = (TextView) findViewById(R.id.joinPlayListName); textView.setText(MusicService.PlaylistName); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText( getApplicationContext(), "You just joined the playlist : " + MusicService.PlaylistName, duration); toast.show(); } else if (PlaylistActivity.isAdmin == true) { findViewById(R.id.layout_add_song).setVisibility(View.VISIBLE); findViewById(R.id.coverartfond).setVisibility(View.VISIBLE); findViewById(R.id.layoutPlayListName).setVisibility(View.VISIBLE); findViewById(R.id.layoutMenuTitle).setVisibility(View.VISIBLE); findViewById(R.id.add_Button_song).setBackgroundColor(View.GONE); } intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); swipelistview = (SwipeListView) findViewById(R.id.playlist); itemSong = new ArrayList<Song>(); adapter = new SongAdapter(this, R.layout.song, itemSong, swipelistview); manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); channel = manager.initialize(this, getMainLooper(), null); // The Manager which is looking for other devices manager.discoverPeers( channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() {} @Override public void onFailure(int reason) {} }); // Register filter for on connect callback LocalBroadcastManager.getInstance(this) .registerReceiver(connect, new IntentFilter("onConnect")); swipelistview.setSwipeListViewListener( new BaseSwipeListViewListener() { @Override public void onOpened(int position, boolean toRight) {} @Override public void onClosed(int position, boolean fromRight) {} @Override public void onListChanged() {} @Override public void onMove(int position, float x) {} @Override public void onStartOpen(int position, int action, boolean right) { ListView listView = (ListView) findViewById(R.id.playlist); View v = listView.getChildAt(position - listView.getFirstVisiblePosition()); Button remove_addButton = (Button) v.findViewById(R.id.button_add_remove); remove_addButton.setVisibility(View.VISIBLE); v.findViewById(R.id.layout_vote).setVisibility(View.VISIBLE); if (lastPosition != -1 && lastPosition != position) { swipelistview.closeAnimate(lastPosition); ListView listView2 = (ListView) findViewById(R.id.playlist); View v2 = listView2.getChildAt(lastPosition - listView2.getFirstVisiblePosition()); Button remove_addButton2 = (Button) v2.findViewById(R.id.button_add_remove); remove_addButton2.setVisibility(View.GONE); v2.findViewById(R.id.layout_vote).setVisibility(View.GONE); } lastPosition = position; } @Override public void onStartClose(int position, boolean right) { ListView listView = (ListView) findViewById(R.id.playlist); View v = listView.getChildAt(position - listView.getFirstVisiblePosition()); Button remove_addButton = (Button) v.findViewById(R.id.button_add_remove); remove_addButton.setVisibility(View.GONE); v.findViewById(R.id.layout_vote).setVisibility(View.GONE); lastPosition = -1; } @Override public void onClickFrontView(int position) { ListView listView = (ListView) findViewById(R.id.playlist); View v = listView.getChildAt(position - listView.getFirstVisiblePosition()); Button remove_addButton = (Button) v.findViewById(R.id.button_add_remove); remove_addButton.setVisibility(View.VISIBLE); v.findViewById(R.id.layout_vote).setVisibility(View.VISIBLE); if (lastPosition != -1 && lastPosition != position) { swipelistview.closeAnimate(lastPosition); ListView listView2 = (ListView) findViewById(R.id.playlist); View v2 = listView2.getChildAt(lastPosition - listView2.getFirstVisiblePosition()); Button remove_addButton2 = (Button) v2.findViewById(R.id.button_add_remove); remove_addButton2.setVisibility(View.GONE); v2.findViewById(R.id.layout_vote).setVisibility(View.GONE); } swipelistview.openAnimate(position); // when you touch front view it will open lastPosition = position; } @Override public void onClickBackView(int position) { ListView listView = (ListView) findViewById(R.id.playlist); View v = listView.getChildAt(position - listView.getFirstVisiblePosition()); Button remove_addButton = (Button) v.findViewById(R.id.button_add_remove); remove_addButton.setVisibility(View.GONE); v.findViewById(R.id.layout_vote).setVisibility(View.GONE); swipelistview.closeAnimate(position); // when you touch back view it will close lastPosition = -1; } @Override public void onDismiss(int[] reverseSortedPositions) {} }); swipelistview.setSwipeActionLeft( SwipeListView.SWIPE_ACTION_REVEAL); // there are four swipe actions swipelistview.setOffsetLeft(convertDpToPixel(420f)); // left side offset swipelistview.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT); // there are five swiping modes swipelistview.setAnimationTime(200); // animarion time swipelistview.setSwipeOpenOnLongPress(true); // enable or disable SwipeOpenOnLongPress swipelistview.setAdapter(adapter); adapter.notifyDataSetChanged(); if (musicServiceIntent == null) { musicServiceIntent = new Intent(this, MusicService.class); bindService(musicServiceIntent, musicConnection, Context.BIND_AUTO_CREATE); startService(musicServiceIntent); } ImageView firstcoverart = (ImageView) findViewById(R.id.coverart); Drawable myDrawable = this.getResources().getDrawable(R.drawable.ic_fond_welcome); firstcoverart.setImageBitmap(((BitmapDrawable) myDrawable).getBitmap()); }