android - 按钮单击及其父布局单击冲突
问题描述
我在 Framelayout 中有按钮。我的目标是在按钮单击和按钮单击之外的任何位置更改视图寻呼机项目,我想在底部显示一个菜单。我面临的问题是当我在父框架布局上添加 onclick 列表时,我的按钮不起作用。而不是按钮,framelayout click 采取行动并显示我的菜单。
这是我的 onclick 监听器代码:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.source1:
// Change viewpager page
if (((MainActivity) getActivity()).getViewPager() != null) {
SourceCommunicator sc = new SourceCommunicator();
sc.referenceUrl = news.getSources().get(0).getSiteUrl();
EventBus.getDefault().post(sc);
((MainActivity) getActivity()).getViewPager().setCurrentItem(2);
}
break;
case R.id.container:
// Show menu at the bottom
EventBus.getDefault().post(new Communicator());
if (showContentWriter) {
referenceView.setVisibility(View.VISIBLE);
writterView.setVisibility(View.GONE);
} else {
referenceView.setVisibility(View.GONE);
writterView.setVisibility(View.VISIBLE);
}
showContentWriter = !showContentWriter;
break;
default:
break;
}
}
我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.ShortFragment">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="15dp">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/previewView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/previewImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/gradient_bg" />
</RelativeLayout>
<LinearLayout
android:id="@+id/newsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/relatedNewsView"
android:layout_below="@+id/previewView"
android:layout_margin="20dp"
android:orientation="vertical">
<com.shongkhipto.shonkhipto.Utlis.BanglaTextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three astronauts return to Earth after 168 days in space"
android:textColor="@android:color/black"
android:textSize="24dp"
android:textStyle="bold" />
<com.shongkhipto.shonkhipto.Utlis.BanglaTextView
android:id="@+id/descriptionView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="After spending 168 days aboard the International Space Station, NASA astronaut Scott Tingle, Japan's Norishige Kanai and Russian cosmonaut Anton Shkaplerov landed in Kazakhstan on Sunday. While Tingle and Kanai conducted separate spacewalks, Shkaplerov did a record-setting 8-hour-long spacewalk. It was Tingle and Kanai's first mission, while Shkaplerov has logged 532 days on three flights."
android:textSize="18dp" />
<LinearLayout
android:id="@+id/referenceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical">
<com.shongkhipto.shonkhipto.Utlis.BanglaTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="আরো পড়ুন"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/source1"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:visibility="gone"
app:srcCompat="@drawable/baseline_keyboard_arrow_left_24" />
<ImageButton
android:id="@+id/source2"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:visibility="gone"
app:srcCompat="@drawable/baseline_keyboard_arrow_left_24" />
<ImageButton
android:id="@+id/source3"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:visibility="gone"
app:srcCompat="@drawable/baseline_keyboard_arrow_left_24" />
<ImageButton
android:id="@+id/source4"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:visibility="gone"
app:srcCompat="@drawable/baseline_keyboard_arrow_left_24" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/writterView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<com.shongkhipto.shonkhipto.Utlis.EnglishTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Short by"
android:textSize="18dp"
android:textStyle="bold" />
<com.shongkhipto.shonkhipto.Utlis.BanglaTextView
android:id="@+id/writerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Shifa Naseer"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/relatedNewsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
android:orientation="vertical">
<ImageView
android:id="@+id/relatedNewsImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/error" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/full_gradient_bg" />
<com.shongkhipto.shonkhipto.Utlis.BanglaTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:text="World must stand up to US 'bullying': Iran Foreign Minister"
android:textColor="@android:color/white"
android:textSize="12dp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
解决方案
推荐阅读
- angular - 错误:将 Angular 8 转换为 Angular Universal 时“无法读取未定义的属性‘种类’”
- python - python脚本中的正则表达式
- html - 为什么css文件正在加载空白页?
- excel - 有多个数据表时如何使用VBA修改所选Excel数据表中的记录
- python - 如何将浮点元组列表转换为Python中的字符串列表?
- java - XSLT FO 程序,将 XML 数据结构化问题转换为 PDF
- spring - 如何使用 netflix-eureka 获得注册的微服务
- jquery - 从数据表 SearchPanes 中剥离 HTML
- python - 如何从任何计算机编辑我的 MySQL 数据库?
- javascript - 无法将 SVG 编译为 Handlebars HTML 模板