如何禁用查看点击事件Framelayout
在这里我有一个查看传呼机活动,有一个imageview和2个覆盖酒吧。 有覆盖酒吧我使用android xml文件布局本身。
这里我的要求是这样的
1)单击浏览器的imageview第一次=显示顶部和底部的矩形覆盖。 2)单击浏览器的imageview第二次=隐藏这些叠加。
这些都是像android的gallary视图types的function。
但是,当这些顶部和底部的布局栏显示的时候,我只想使用button,只需要在这个布局中声明哪些button被声明。
但是我没有成功实现这件事情。
问题
1)当top or bottom bar
在那里,如果我可以点击next or previous
button比它的事件后面imageview单击触摸事件,而我的酒吧是隐形的。 2)只需要声明button事件3)避免imageview点击时,我触摸覆盖栏。
简而言之,当我的顶部和底部图像栏出现的时候,顶部和底部图像栏上的图像视图没有发生图像事件。 我可以点击imageview,但不能点击时,我实际上点击下一个或上一个或分享button。
所以这些是我面临的问题,请帮助我。
源代码 :
activity_pager_image.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <RelativeLayout android:id="@+id/rl_top_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/slideshow_bar" android:visibility="gone" > <TextView android:id="@+id/tv_top_overlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textIsSelectable="false" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_bottom_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/slideshow_bar" android:visibility="visible" > <Button android:id="@+id/btn_left_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="35dp" android:background="@drawable/ic_left_arrow" /> <Button android:id="@+id/btn_below_share" style="@style/normalText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="35dp" android:background="@drawable/ic_share" android:visibility="visible" /> <Button android:id="@+id/btn_right_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginLeft="50dp" android:layout_toRightOf="@id/btn_left_arrow" android:background="@drawable/ic_right_arrow" /> </RelativeLayout> </FrameLayout>
item_pager_image.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <demo.android.library.imagezoom.ImageViewTouch android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:contentDescription="@string/descr_image" android:scaleType="fitXY" /> </FrameLayout>
JAVA代码
public class ImagePagerActivity extends BaseActivity { private static final String STATE_POSITION = "STATE_POSITION"; private DisplayImageOptions options; private String[] imageUrls; private ViewPager pager; private static int sCounter = 0; private RelativeLayout mRlTopOverlayBar = null; private RelativeLayout mRlBottomOverlayBar = null; private TextView mPageNumberText = null; private Button mLeftArrow = null; private Button mRightArrow = null; int mPageCounter = 0; int mTotalImages = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_pager); mRlTopOverlayBar = (RelativeLayout) findViewById(R.id.rl_top_overlay); mRlBottomOverlayBar = (RelativeLayout) findViewById(R.id.rl_bottom_overlay); mPageNumberText = (TextView) findViewById(R.id.tv_top_overlay); mLeftArrow = (Button) findViewById(R.id.btn_left_arrow); mRightArrow = (Button) findViewById(R.id.btn_right_arrow); Bundle bundle = getIntent().getExtras(); String[] imageUrls = bundle .getStringArray(Constants.GALLARY_IMAGES_IMAGE_BUNDLE_KEY); mTotalImages = imageUrls.length; mPageCounter = bundle.getInt( Constants.GALLARY_IMAGE_POSITION_BUNDLE_KEY, 0); Log.d("TAG", "Pre Poistion " + mPageCounter); if (savedInstanceState != null) { mPageCounter = savedInstanceState.getInt(STATE_POSITION); } options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.photo_default) .showImageOnFail(R.drawable.ic_error).resetViewBeforeLoading() .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY) .bitmapConfig(Bitmap.Config.RGB_565) .displayer(new FadeInBitmapDisplayer(300)).build(); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new ImagePagerAdapter(imageUrls)); pager.setCurrentItem(mPageCounter); mLeftArrow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // int setCounter = mPageCounter - 1; // if (setCounter >= 0) { // } pager.setCurrentItem(pager.getCurrentItem() - 1); } }); mRightArrow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pager.setCurrentItem(pager.getCurrentItem() + 1); /* * int setCounter = mPageCounter + 1; if (setCounter < * mTotalImages) { pager.setCurrentItem(mPageCounter + 1); } */ } }); } @Override public void onSaveInstanceState(Bundle outState) { outState.putInt(STATE_POSITION, pager.getCurrentItem()); } private class ImagePagerAdapter extends PagerAdapter { private String[] images; private LayoutInflater inflater; ImagePagerAdapter(String[] images) { this.images = images; inflater = getLayoutInflater(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public void finishUpdate(View container) { } @Override public int getCount() { return images.length; } @Override public Object instantiateItem(ViewGroup view, int position) { View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false); Log.d("TAG", "Poistion " + position); final ImageViewTouch imageView = (ImageViewTouch) imageLayout .findViewById(R.id.image); final DeactivableViewPager viewPager = new DeactivableViewPager( ImagePagerActivity.this); imageView.setOnScaleListener(new OnPageScaleListener() { @Override public void onScaleBegin() { viewPager.deactivate(); } @Override public void onScaleEnd(float scale) { if (scale > 1.0) { viewPager.deactivate(); } else { viewPager.activate(); } } }); imageView .setSingleTapListener(new OnImageViewTouchSingleTapListener() { @Override public void onSingleTapConfirmed() { Log.d("TAG", "setSingleTapListener"); sCounter++; if (sCounter % 2 == 0) { mRlTopOverlayBar.setVisibility(View.GONE); mRlBottomOverlayBar.setVisibility(View.GONE); } else { mRlTopOverlayBar.setVisibility(View.VISIBLE); mRlBottomOverlayBar.setVisibility(View.VISIBLE); mRlBottomOverlayBar.setClickable(false); mRlTopOverlayBar.setClickable(false); } } }); imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { // spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { String message = null; switch (failReason.getType()) { case IO_ERROR: message = "Input/Output error"; break; case DECODING_ERROR: message = "Image can't be decoded"; break; case NETWORK_DENIED: message = "Downloads are denied"; break; case OUT_OF_MEMORY: message = "Out Of Memory error"; break; case UNKNOWN: message = "Unknown error"; break; } Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show(); // spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // spinner.setVisibility(View.GONE); } }); ((ViewPager) view).addView(imageLayout, 0); return imageLayout; } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } @Override public void restoreState(Parcelable state, ClassLoader loader) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View container) { } } }
![在这里input图片描述] [1] image:
谢谢
imageView.setSingleTapListener(new OnImageViewTouchSingleTapListener() { @Override public void onSingleTapConfirmed() { Log.d("TAG", "setSingleTapListener"); sCounter++; if (sCounter % 2 == 0) { mRlTopOverlayBar.setVisibility(View.GONE); mRlBottomOverlayBar.setVisibility(View.GONE); pager.requestFocus(); } else { mRlTopOverlayBar.setVisibility(View.VISIBLE); mRlBottomOverlayBar.setVisibility(View.VISIBLE); mRlTopOverlayBar.requestFocus(); mRlBottomOverlayBar.requestFocus(); mRlBottomOverlayBar.setClickable(true); mRlTopOverlayBar.setClickable(true); } } });
更好的方法是将顶部和底部框架设置为可点击的:
android:clickable="true"
这样做将确保视图/框架本身将捕获所有点击事件,并且不会通过它后面的视图。 请注意,此方法适用于所有布局/视图/控件,但许多控件(如button)默认已经具有此function。
android:clickable="true"
为顶部和底部栏。
或者给每个FrameLayout
一个onClickListener
。
@ gordon1hd1的答案是正确的,但对于那些仍然困惑,我添加我的布局,其中包含一个FrameLayout
作为父母和一个LinearLayout
和两个ImageViews
作为孩子。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/scroll_parent" android:orientation="horizontal" /> <ImageView android:id="@+id/ivArrowLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:src="@drawable/actionbar_back" android:layout_gravity="left|center_vertical" android:background="#3f808080" android:clickable="true" /> <ImageView android:id="@+id/ivArrowRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="4dp" android:src="@drawable/actionbar_back" android:layout_gravity="right|center_vertical" android:background="#3f808080" android:rotation="180" android:clickable="true" /> </FrameLayout>
此前, Linearlayout
也在拦截触摸事件时按下任一ImageViews
。 向两个ImageViews
添加android:clickable="true"
解决了这个问题。
如果您还面临这种types的问题,请将android:clickable="true"
到要捕获单击事件的视图。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/llSettings" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="right" android:background="#ff106279" android:minHeight="25px" android:minWidth="25px" android:onClick="click" android:orientation="vertical" android:visibility="visible"> <LinearLayout android:id="@+id/llSettings1" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#ff000f" android:clickable="true" android:minHeight="25px" android:minWidth="25px" android:orientation="vertical" android:visibility="visible"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="buttonclick" android:text="New Button" /> </LinearLayout> </RelativeLayout>
和
public void click(View v) { Toast.makeText(this, "((RelativeLayout)v).toString()", Toast.LENGTH_SHORT).show(); } public void buttonclick(View v) { Toast.makeText(this, "Button", Toast.LENGTH_SHORT).show(); }