下面列出了android.view.View#setOnTouchListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Example node of a layout
*
* @return
*/
private Node getExampleView() {
Node base = new Node();
base.setRenderable(exampleLayoutRenderable);
Context c = this;
// Add listeners etc here
View eView = exampleLayoutRenderable.getView();
eView.setOnTouchListener((v, event) -> {
Toast.makeText(
c, "Location marker touched.", Toast.LENGTH_LONG)
.show();
return false;
});
return base;
}
/**
* To bind different types of holder and solve different the bind events
*
* @param holder
* @param positions
* @see #getDefItemViewType(int)
*/
@Override
public void onBindViewHolder(K holder, int positions) {
super.onBindViewHolder(holder, positions);
int viewType = holder.getItemViewType();
if (mItemTouchHelper != null && itemDragEnabled && viewType != LOADING_VIEW && viewType != HEADER_VIEW
&& viewType != EMPTY_VIEW && viewType != FOOTER_VIEW) {
if (mToggleViewId != NO_TOGGLE_VIEW) {
View toggleView = ((BaseViewHolder) holder).getView(mToggleViewId);
if (toggleView != null) {
toggleView.setTag(R.id.BaseQuickAdapter_viewholder_support, holder);
if (mDragOnLongPress) {
toggleView.setOnLongClickListener(mOnToggleViewLongClickListener);
} else {
toggleView.setOnTouchListener(mOnToggleViewTouchListener);
}
}
} else {
holder.itemView.setTag(R.id.BaseQuickAdapter_viewholder_support, holder);
holder.itemView.setOnLongClickListener(mOnToggleViewLongClickListener);
}
}
}
public View getView(int i, View view, ViewGroup viewgroup)
{
f f1 = (f)infoList.get(i);
g g1 = new g(this);
View view1 = LayoutInflater.from(mContext).inflate(0x7f030050, null);
g1.a = (TextView)view1.findViewById(0x7f0a0165);
g1.a.setTypeface(Typeface.DEFAULT, 1);
g1.b = (TextView)view1.findViewById(0x7f0a0166);
g1.b.setTypeface(Typeface.DEFAULT, 1);
((ImageView)view1.findViewById(0x7f0a0164)).setVisibility(8);
view1.setOnTouchListener(new e(this));
g1.a.setText("");
g1.b.setText("");
view1.setTag(g1);
g1.a.setTag(f1);
return view1;
}
private void initHideBoard(View view) {
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v1, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (isTouchView(filterViewByIds(), ev)) return false;
if (hideSoftByEditViewIds() == null || hideSoftByEditViewIds().length == 0)
return false;
View v = activity.getCurrentFocus();
if (isFocusEditText(v, hideSoftByEditViewIds())) {
if (isTouchView(hideSoftByEditViewIds(), ev))
return false;
KeyBoardUtils.hideInputForce(activity);
}
}
return false;
}
});
}
public void init(IViewContainer parent, SeekBar seekBar, int minusViewId, int plusViewId) {
this.seekBar = seekBar;
View minus = parent.findViewById(minusViewId);
minus.setTag(Integer.valueOf(-1));
minus.setOnTouchListener(this);
minus.setOnClickListener(this);
minus.setOnLongClickListener(this);
View plus = parent.findViewById(plusViewId);
plus.setTag(Integer.valueOf(+1));
plus.setOnTouchListener(this);
plus.setOnClickListener(this);
plus.setOnLongClickListener(this);
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
floatingRecordButton = new FloatingRecordButton(getContext(), findViewById(R.id.quick_audio_fab));
lockDropTarget = new LockDropTarget (getContext(), findViewById(R.id.lock_drop_target));
View recordButton = ViewUtil.findById(this, R.id.quick_audio_toggle);
recordButton.setOnTouchListener(this);
}
private void setOnTouchedInternal(final View child) {
child.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mSlidingUpPanel = (ISlidingUpPanel) child;
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
v.performClick();
}
return false;
}
});
}
/**
* 设置触摸事件
* @param activity {@link Activity}
* @param onTouchListener {@link View.OnTouchListener}
* @param viewIds View id 数组
* @return {@code true} success, {@code false} fail
*/
public static boolean setOnTouchs(final Activity activity, final View.OnTouchListener onTouchListener, @IdRes final int... viewIds) {
if (activity != null && onTouchListener != null && viewIds != null) {
for (int i = 0, len = viewIds.length; i < len; i++) {
View findView = ViewUtils.findViewById(activity, viewIds[i]);
if (findView != null) {
findView.setOnTouchListener(onTouchListener);
}
}
return true;
}
return false;
}
@Override
public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder;
if (convertView == null || !(convertView.getTag() instanceof ChildViewHolder)) {
convertView = inflater.inflate(R.layout.local_child_item, parent, false);
holder = new ChildViewHolder();
holder.title = convertView.findViewById(R.id.child_title);
holder.divider = convertView.findViewById(R.id.child_divider);
holder.card = (CardView) holder.title.getParent();
convertView.setTag(holder);
} else
holder = (ChildViewHolder) convertView.getTag();
holder.lyrics = getChild(groupPosition, childPosition);
if (holder.lyrics != null) {
holder.title.setText(holder.lyrics.getTitle());
holder.card.setBackgroundColor(markedRows.contains(new int[]{groupPosition, childPosition}) ? childSelectedStateColor : childDefaultStateColor);
holder.title.setTextColor(markedRows.contains(new int[]{groupPosition, childPosition}) ? childDefaultStateColor : childSelectedStateColor);
convertView.setOnTouchListener(mTouchListener);
holder.groupPosition = groupPosition;
holder.divider.setVisibility(isLastChild ? View.GONE : View.VISIBLE);
convertView.setAlpha(1f);
convertView.setVisibility(View.VISIBLE);
} else
convertView.setVisibility(View.GONE);
convertView.setTranslationX(0f);
return convertView;
}
public static void disableKeyboardByClickView(View view){
View.OnTouchListener otl = new View.OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
view.setOnTouchListener(otl);
}
/**
* Clear all views which were previously used to initiate refresh requests.
*/
public void clearRefreshableViews() {
Set<View> views = mRefreshableViews.keySet();
for (View view : views) {
view.setOnTouchListener(null);
}
mRefreshableViews.clear();
}
@SuppressLint("ClickableViewAccessibility")
public void show(View parentView, View view, Article article) {
view.setOnTouchListener((v, event) -> {
if((event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) && isPress){
this.showAtLocation(parentView, Gravity.NO_GRAVITY, (int) event.getRawX(), (int) event.getRawY());
this.mTitle = article.getTitle();
this.mLink = article.getLink();
isPress = false;
}
return false;
});
isPress = true;
}
public static <T extends Parcelable> void setupOverflowEditMenu(Activity context, View anchor,
OnOperationListener<T> listener, int res, final T data, final int position) {
final PopupMenu menu = new PopupMenu(context, anchor);
menu.inflate(res);
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_edit:
if (listener != null) {
listener.update(data, position);
}
return true;
case R.id.action_delete:
if (listener != null) {
listener.delete(data, position);
}
return true;
}
return false;
}
});
anchor.setOnTouchListener(menu.getDragToOpenListener());
anchor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menu.show();
}
});
}
public static void removeDragEvent(View view) {
view.setOnDragListener(null);
view.setOnTouchListener(null);
view.setOnLongClickListener(null);
}
private void addViewToWindow(View view) {
int permissionFlag = PermissionCompat.getFlag();
WindowManager.LayoutParams paramsF = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
permissionFlag,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
// configure starting coordinates
if (fpsConfig.xOrYSpecified) {
paramsF.x = fpsConfig.startingXPosition;
paramsF.y = fpsConfig.startingYPosition;
paramsF.gravity = FPSConfig.DEFAULT_GRAVITY;
} else if (fpsConfig.gravitySpecified) {
paramsF.x = 0;
paramsF.y = 0;
paramsF.gravity = fpsConfig.startingGravity;
} else {
paramsF.gravity = FPSConfig.DEFAULT_GRAVITY;
paramsF.x = fpsConfig.startingXPosition;
paramsF.y = fpsConfig.startingYPosition;
}
// add view to the window
windowManager.addView(view, paramsF);
// create gesture detector to listen for double taps
GestureDetector gestureDetector = new GestureDetector(view.getContext(), simpleOnGestureListener);
// attach touch listener
view.setOnTouchListener(new DancerTouchListener(paramsF, windowManager, gestureDetector));
// disable haptic feedback
view.setHapticFeedbackEnabled(false);
// show the meter
show();
}
public ItemViewHelper setOnTouchListener(int viewId, View.OnTouchListener listener) {
View view = this.getView(viewId);
view.setOnTouchListener(listener);
return this;
}
/**
* 创建Float view
*
* @param context
* @return
*/
private View createView(final Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
// 从布局文件获取浮动窗口视图
View rootFloatView = inflater.inflate(R.layout.pj_widget_float_view, null);
mFlFloatLogo = (FrameLayout) rootFloatView.findViewById(R.id.pj_float_view);
mIvFloatLogo = (ImageView) rootFloatView.findViewById(R.id.pj_float_view_icon_imageView);
mIvFloatLoader = (ImageView) rootFloatView.findViewById(R.id.pj_float_view_icon_notify);
mLlFloatMenu = (LinearLayout) rootFloatView.findViewById(R.id.ll_menu);
recycle_horizontal = (RecyclerView) rootFloatView.findViewById(R.id.recycle_horizontal);
no_ad_tip = (TextView) rootFloatView.findViewById(R.id.no_ad_tip);
//设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recycle_horizontal.setLayoutManager(linearLayoutManager);
//设置适配器
mAdapter = new GalleryAdapter(getContext(), mDatas);
recycle_horizontal.setAdapter(mAdapter);
// mAdapter.setOnItemClickListener(new OnItemClickListener() {
// @Override
// public void OnItemClick(View view, int position) {
// if (mDatas.size() > 0) {
// Intent intent = new Intent();
// intent.setData(Uri.parse("http;//baidu.com"));//Url 就是你要打开的网址
// intent.setAction(Intent.ACTION_VIEW);
// intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(intent); //启动浏览器
// //隐藏悬浮窗
// hide();
// }
// }
// });
rootFloatView.setOnTouchListener(this);
rootFloatView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!mDraging) {
if (mLlFloatMenu.getVisibility() == View.VISIBLE) {
mLlFloatMenu.setVisibility(View.GONE);
} else {
mLlFloatMenu.setVisibility(View.VISIBLE);
}
}
}
});
rootFloatView.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED), MeasureSpec
.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
return rootFloatView;
}
/**
* Programmatically build up the view hierarchy to avoid the need for resources.
*
* @return View hierarchy
*/
private View generateHierarchy(Context context) {
Resources resources = getResources();
LayoutParams params;
int fivePx = dpToPx(5, resources);
int tenPx = dpToPx(10, resources);
int twentyPx = dpToPx(20, resources);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
0,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f);
tableLayoutParams.setMargins(0, 0, fivePx, 0);
LinearLayout seekWrapper;
FrameLayout root = new FrameLayout(context);
params = createLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(300, resources));
root.setLayoutParams(params);
FrameLayout container = new FrameLayout(context);
params = createMatchParams();
params.setMargins(0, twentyPx, 0, 0);
container.setLayoutParams(params);
container.setBackgroundColor(Color.argb(100, 0, 0, 0));
root.addView(container);
mSpringSelectorSpinner = new Spinner(context, Spinner.MODE_DIALOG);
params = createMatchWrapParams();
params.gravity = Gravity.TOP;
params.setMargins(tenPx, tenPx, tenPx, 0);
mSpringSelectorSpinner.setLayoutParams(params);
container.addView(mSpringSelectorSpinner);
LinearLayout linearLayout = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(0, 0, 0, dpToPx(80, resources));
params.gravity = Gravity.BOTTOM;
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.VERTICAL);
container.addView(linearLayout);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mTensionSeekBar = new SeekBar(context);
mTensionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mTensionSeekBar);
mTensionLabel = new TextView(getContext());
mTensionLabel.setTextColor(mTextColor);
params = createLayoutParams(
dpToPx(50, resources),
ViewGroup.LayoutParams.MATCH_PARENT);
mTensionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mTensionLabel.setLayoutParams(params);
mTensionLabel.setMaxLines(1);
seekWrapper.addView(mTensionLabel);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mFrictionSeekBar = new SeekBar(context);
mFrictionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mFrictionSeekBar);
mFrictionLabel = new TextView(getContext());
mFrictionLabel.setTextColor(mTextColor);
params = createLayoutParams(dpToPx(50, resources), ViewGroup.LayoutParams.MATCH_PARENT);
mFrictionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mFrictionLabel.setLayoutParams(params);
mFrictionLabel.setMaxLines(1);
seekWrapper.addView(mFrictionLabel);
View nub = new View(context);
params = createLayoutParams(dpToPx(60, resources), dpToPx(40, resources));
params.gravity = Gravity.TOP | Gravity.CENTER;
nub.setLayoutParams(params);
nub.setOnTouchListener(new OnNubTouchListener());
nub.setBackgroundColor(Color.argb(255, 0, 164, 209));
root.addView(nub);
return root;
}
@Override
protected void initView(View view) {
seekBar = $.id(R.id.app_video_seekBar).view();
seekBar.setMax(1000);
seekBar.setOnSeekBarChangeListener(seekListener);
$.id(R.id.app_video_play).clicked(onClickListener).imageView().setRotation(isRtl()?180:0);
$.id(R.id.app_video_fullscreen).clicked(onClickListener);
$.id(R.id.app_video_finish).clicked(onClickListener).imageView().setRotation(isRtl()?180:0);
$.id(R.id.app_video_replay_icon).clicked(onClickListener).imageView().setRotation(isRtl()?180:0);
$.id(R.id.app_video_clarity).clicked(onClickListener);
$.id(R.id.app_video_float_close).clicked(onClickListener);
$.id(R.id.app_video_float_full).clicked(onClickListener);
final GestureDetector gestureDetector = new GestureDetector(context, createGestureListener());
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (displayModel == GiraffePlayer.DISPLAY_FLOAT) {
return false;
}
if (gestureDetector.onTouchEvent(event)) {
return true;
}
// 处理手势结束
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
endGesture();
break;
}
return true;
}
});
}
private void setupTopCard() {
//TODO: maybe find a better solution this is kind of hacky
//if there's an extra card on screen that means the top card is still being animated
//in that case setup the next card along
int childOffset = getChildCount() - NUMBER_OF_CARDS + 1;
final View child = getChildAt(getChildCount() - childOffset);
//this calculation is to get the correct position in the adapter of the current top card
//the card position on setup top card is currently always the bottom card in the view
//at any given time.
int initialX = paddingLeft;
int initialY = paddingTop;
if (child != null) {
//make sure we have a card
swipeListener = new SwipeListener(child, new SwipeListener.SwipeCallback() {
@Override
public void cardSwipedLeft() {
int positionInAdapter = nextAdapterCard - getChildCount();
removeTopCard();
if (eventCallback != null) eventCallback.cardSwipedLeft(positionInAdapter);
addNextCard();
}
@Override
public void cardSwipedRight() {
int positionInAdapter = nextAdapterCard - getChildCount();
removeTopCard();
if (eventCallback != null) eventCallback.cardSwipedRight(positionInAdapter);
addNextCard();
}
@Override
public void cardOffScreen() {
}
@Override
public void cardActionDown() {
if(eventCallback!=null) eventCallback.cardActionDown();
cardInteraction = true;
}
@Override
public void cardActionUp() {
if(eventCallback!=null) eventCallback.cardActionUp();
cardInteraction = false;
}
}, initialX, initialY, ROTATION_DEGREES, OPACITY_END);
//if we specified these image resources, get the views and pass them to the swipe listener
//for the sake of animating them
View rightView = null;
View leftView = null;
if (!(rightImageResource == 0)) rightView = child.findViewById(rightImageResource);
if (!(leftImageResource == 0)) leftView = child.findViewById(leftImageResource);
swipeListener.setLeftView(leftView);
swipeListener.setRightView(rightView);
child.setOnTouchListener(swipeListener);
}
}