按钮单击及其父布局单击“冲突”

时间:2018-06-06 08:34:40

标签: android onclicklistener

我在Framelayout中有按钮。我的目标是在点击按钮时更改视图寻呼机项目,在按钮点击之外的任何地方,我想在底部显示一个菜单。我面临的问题是当我在父框架布局上添加onclick lister时,我的按钮不起作用。而不是按钮,framelayout点击采取行动并显示我的菜单。

这是我的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>

0 个答案:

没有答案