public void registerForContextMenu(View view, ContextMenuListener listener) { if (mContextMenuListeners == null) { mContextMenuListeners = new WeaklyMap<View, ContextMenuListener>(); } mContextMenuListeners.put(view, listener); view.setLongClickable(true); }
private void resetCell() { if (downPosition != ListView.INVALID_POSITION) { if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.VISIBLE); } frontView.setClickable(opened.get(downPosition)); frontView.setLongClickable(opened.get(downPosition)); frontView = null; backView = null; downPosition = ListView.INVALID_POSITION; } }
private View defineSectionItem(final Item item) { View view; SectionItem sectionItem = SectionItem.class.cast(item); view = this.layoutInflater.inflate(this.listItemSection, null); view.setOnClickListener(null); view.setOnLongClickListener(null); view.setLongClickable(false); final TextView sectionView = (TextView) view.findViewById(this.listItemSectionText); sectionView.setText(sectionItem.getTitle()); return view; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout mainView = null; mainView = new FrameLayout(this); setContentView(mainView); View view = null; view = new MyView(this); view.setLongClickable(true); view.setOnTouchListener(this); mainView.addView(view); textView = new TextView(this); mainView.addView(textView); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View result = inflater.inflate(R.layout.buttons, null); View coloredButton = result.findViewById(R.id.coloredButton); coloredButton.setLongClickable(true); // registerForContextMenu(coloredButton); // mListPopupButton = result.findViewById(R.id.listPopupButton); // mListPopupButton.setOnLongClickListener(mPopupListener); return result; }
@Override public View onCreateView( final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { // 加载book detail的layout View rootView = inflater.inflate(R.layout.fragment_book_detail, container, false); // 这行是必须的 rootView.setLongClickable(true); rootView.setOnTouchListener(myGestureListener); if (book != null) { // 设置图片 ImageView iv = (ImageView) rootView.findViewById(R.id.book_img); Common.IMAGE_CACHE.get(book.get("pic"), iv); // 设置点击图片弹出详情大图的功能 iv.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // 使用单独的布局 View imgEntryView = inflater.inflate(R.layout.dialog_photot_entry, null); // 通过alert窗口弹出显示 final AlertDialog dialog = new AlertDialog.Builder(container.getContext(), AlertDialog.THEME_HOLO_LIGHT) .create(); ImageView detailImage = (ImageView) imgEntryView.findViewById(R.id.large_image); // 还是要异步加载图片 Common.IMAGE_CACHE.get(book.get("pic"), detailImage); dialog.setView(imgEntryView); dialog.show(); imgEntryView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); } }); } }); // 设置标题 ((TextView) rootView.findViewById(R.id.book_title)).setText(book.get("title")); // 设置描述 TextView view = (TextView) rootView.findViewById(R.id.book_desc); view.setText(book.get("desc")); } return rootView; }
private void doSetClickable(View view, boolean clickable) { if (view == null) { return; } if (!clickable) { view.setOnClickListener( null); // This will set clickable to true in the view, so make sure it stays here so the // next line turns it off. view.setClickable(false); view.setOnLongClickListener(null); view.setLongClickable(false); } else if (!(view instanceof AdapterView)) { // n.b.: AdapterView throws if click listener set. // n.b.: setting onclicklistener automatically sets clickable to true. setOnClickListener(view); setOnLongClickListener(view); } }
/** * Registers an OnClickListener for a specific GUI Element. OnClick the function <code> * onButtonClicked</code> will be called with the given id * * @param v The view to register an OnClickListener for * @param id The item ID to register the listener for */ protected void registerOnClickListener(View v, final int id) { v.setLongClickable(true); v.setOnLongClickListener( new OnLongClickListener() { @Override public boolean onLongClick(View v) { onButtonClicked(id, true); return true; } }); v.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { onButtonClicked(id, false); } }); }
/** @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 public View getView(final int position, final View rowView, final ViewGroup parent) { if (inflater == null) { inflater = ((Activity) getContext()).getLayoutInflater(); } if (position > getCount()) { Log.w(Settings.tag, "CacheListAdapter.getView: Attempt to access missing item #" + position); return null; } cgCache cache = getItem(position); View v = rowView; if (v == null) { v = inflater.inflate(R.layout.cache, null); holder = new cgCacheView(); holder.checkbox = (CheckBox) v.findViewById(R.id.checkbox); holder.oneInfo = (RelativeLayout) v.findViewById(R.id.one_info); holder.oneCheckbox = (RelativeLayout) v.findViewById(R.id.one_checkbox); holder.logStatusMark = (ImageView) v.findViewById(R.id.log_status_mark); holder.text = (TextView) v.findViewById(R.id.text); holder.directionLayout = (RelativeLayout) v.findViewById(R.id.direction_layout); holder.distance = (cgDistanceView) v.findViewById(R.id.distance); holder.direction = (cgCompassMini) v.findViewById(R.id.direction); holder.dirImgLayout = (RelativeLayout) v.findViewById(R.id.dirimg_layout); holder.dirImg = (ImageView) v.findViewById(R.id.dirimg); holder.inventory = (RelativeLayout) v.findViewById(R.id.inventory); holder.favourite = (TextView) v.findViewById(R.id.favourite); holder.info = (TextView) v.findViewById(R.id.info); v.setTag(holder); } else { holder = (cgCacheView) v.getTag(); } if (cache.isOwn()) { if (Settings.isLightSkin()) { holder.oneInfo.setBackgroundResource(R.color.owncache_background_light); holder.oneCheckbox.setBackgroundResource(R.color.owncache_background_light); } else { holder.oneInfo.setBackgroundResource(R.color.owncache_background_dark); holder.oneCheckbox.setBackgroundResource(R.color.owncache_background_dark); } } else { if (Settings.isLightSkin()) { holder.oneInfo.setBackgroundResource(R.color.background_light); holder.oneCheckbox.setBackgroundResource(R.color.background_light); } else { holder.oneInfo.setBackgroundResource(R.color.background_dark); holder.oneCheckbox.setBackgroundResource(R.color.background_dark); } } final touchListener touchLst = new touchListener(cache.getGeocode(), cache.getName(), cache); v.setOnClickListener(touchLst); v.setOnLongClickListener(touchLst); v.setOnTouchListener(touchLst); v.setLongClickable(true); if (selectMode) { if (cache.isStatusCheckedView()) { moveRight(holder, cache, true); // move fast when already slided } else { moveRight(holder, cache, false); } } else if (cache.isStatusChecked()) { holder.checkbox.setChecked(true); if (cache.isStatusCheckedView()) { moveRight(holder, cache, true); // move fast when already slided } else { moveRight(holder, cache, false); } } else { holder.checkbox.setChecked(false); if (cache.isStatusCheckedView()) { moveLeft(holder, cache, false); } else { holder.oneInfo.clearAnimation(); } } holder.checkbox.setOnClickListener(new checkBoxListener(cache)); distances.add(holder.distance); holder.distance.setContent(cache.getCoords()); compasses.add(holder.direction); holder.direction.setContent(cache.getCoords()); if (cache.isFound() && cache.isLogOffline()) { holder.logStatusMark.setImageResource(R.drawable.mark_green_orange); holder.logStatusMark.setVisibility(View.VISIBLE); } else if (cache.isFound()) { holder.logStatusMark.setImageResource(R.drawable.mark_green_more); holder.logStatusMark.setVisibility(View.VISIBLE); } else if (cache.isLogOffline()) { holder.logStatusMark.setImageResource(R.drawable.mark_orange); holder.logStatusMark.setVisibility(View.VISIBLE); } else { holder.logStatusMark.setVisibility(View.GONE); } if (cache.getNameSp() == null) { cache.setNameSp((new Spannable.Factory()).newSpannable(cache.getName())); if (cache.isDisabled() || cache.isArchived()) { // strike cache .getNameSp() .setSpan( new StrikethroughSpan(), 0, cache.getNameSp().toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } holder.text.setText(cache.getNameSp(), TextView.BufferType.SPANNABLE); if (gcIconDrawables.containsKey(cache.getType())) { // cache icon holder.text.setCompoundDrawablesWithIntrinsicBounds( gcIconDrawables.get(cache.getType()), null, null, null); } else { // unknown cache type, "mystery" icon holder.text.setCompoundDrawablesWithIntrinsicBounds( gcIconDrawables.get(CacheType.MYSTERY), null, null, null); } if (holder.inventory.getChildCount() > 0) { holder.inventory.removeAllViews(); } ImageView tbIcon = null; if (cache.getInventoryItems() > 0) { tbIcon = (ImageView) inflater.inflate(R.layout.trackable_icon, null); tbIcon.setImageResource(R.drawable.trackable_all); holder.inventory.addView(tbIcon); holder.inventory.setVisibility(View.VISIBLE); } else { holder.inventory.setVisibility(View.GONE); } boolean setDiDi = false; if (cache.getCoords() != null) { holder.direction.setVisibility(View.VISIBLE); holder.direction.updateAzimuth(azimuth); if (coords != null) { holder.distance.update(coords); holder.direction.updateCoords(coords); } setDiDi = true; } else { if (cache.getDistance() != null) { holder.distance.setDistance(cache.getDistance()); setDiDi = true; } if (cache.getDirection() != null) { holder.direction.setVisibility(View.VISIBLE); holder.direction.updateAzimuth(azimuth); holder.direction.updateHeading(cache.getDirection()); setDiDi = true; } } if (setDiDi) { holder.directionLayout.setVisibility(View.VISIBLE); holder.dirImgLayout.setVisibility(View.GONE); } else { holder.directionLayout.setVisibility(View.GONE); holder.distance.clear(); final Bitmap dirImgPre = BitmapFactory.decodeFile( cgDirectionImg.getDirectionFile(cache.getGeocode(), false).getPath()); final Bitmap dirImg; if (dirImgPre != null) { // null happens for invalid caches (not yet released) dirImg = dirImgPre.copy(Bitmap.Config.ARGB_8888, true); dirImgPre.recycle(); } else { dirImg = null; } if (dirImg != null) { if (!Settings.isLightSkin()) { int length = dirImg.getWidth() * dirImg.getHeight(); int[] pixels = new int[length]; dirImg.getPixels( pixels, 0, dirImg.getWidth(), 0, 0, dirImg.getWidth(), dirImg.getHeight()); for (int i = 0; i < length; i++) { if (pixels[i] == 0xff000000) { // replace black with white pixels[i] = 0xffffffff; } } dirImg.setPixels( pixels, 0, dirImg.getWidth(), 0, 0, dirImg.getWidth(), dirImg.getHeight()); } holder.dirImg.setImageBitmap(dirImg); holder.dirImgLayout.setVisibility(View.VISIBLE); } else { holder.dirImg.setImageBitmap(null); holder.dirImgLayout.setVisibility(View.GONE); } } holder.favourite.setText(String.format("%d", cache.getFavoritePoints())); int favoriteBack; // set default background, neither vote nor rating may be available if (Settings.isLightSkin()) { favoriteBack = R.drawable.favourite_background_light; } else { favoriteBack = R.drawable.favourite_background_dark; } float myVote = cache.getMyVote(); if (myVote > 0) { // use my own rating for display, if I have voted if (myVote >= 4) { favoriteBack = ratingBcgs[2]; } else if (myVote >= 3) { favoriteBack = ratingBcgs[1]; } else if (myVote > 0) { favoriteBack = ratingBcgs[0]; } } else { float rating = cache.getRating(); if (rating >= 3.5) { favoriteBack = ratingBcgs[2]; } else if (rating >= 2.1) { favoriteBack = ratingBcgs[1]; } else if (rating > 0.0) { favoriteBack = ratingBcgs[0]; } } holder.favourite.setBackgroundResource(favoriteBack); if (cacheListType == CacheListType.HISTORY && cache.getVisitedDate() > 0) { StringBuilder cacheInfo = new StringBuilder(50); cacheInfo.append(cgBase.formatTime(cache.getVisitedDate())); cacheInfo.append("; "); cacheInfo.append(cgBase.formatDate(cache.getVisitedDate())); holder.info.setText(cacheInfo.toString()); } else { ArrayList<String> infos = new ArrayList<String>(); if (StringUtils.isNotBlank(cache.getGeocode())) { infos.add(cache.getGeocode()); } if (cache.hasDifficulty()) { infos.add("D " + String.format("%.1f", cache.getDifficulty())); } if (cache.hasTerrain()) { infos.add("T " + String.format("%.1f", cache.getTerrain())); } // don't show "not chosen" for events and virtuals, that should be the normal case if (cache.getSize() != CacheSize.UNKNOWN && cache.showSize()) { infos.add(cache.getSize().getL10n()); } else if (cache.isEventCache() && cache.getHiddenDate() != null) { infos.add(cgBase.formatShortDate(cache.getHiddenDate().getTime())); } if (cache.isMembers()) { infos.add(res.getString(R.string.cache_premium)); } if (cacheListType != CacheListType.OFFLINE && cacheListType != CacheListType.HISTORY && cache.getReason() > 0) { infos.add(res.getString(R.string.cache_offline)); } holder.info.setText(StringUtils.join(infos, SEPARATOR)); } return v; }