简体   繁体   English

片段中的onClick方法永远不会被调用

[英]onClick method in fragment never gets called

I tried to set up a onClickListener inside my fragment. 我试图在我的片段中设置一个onClickListener。

public class HomeFragment extends Fragment implements View.OnClickListener {

Button btn_eventList;
public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    btn_eventList = (Button) view.findViewById(R.id.buttonEventList);
    btn_eventList.setOnClickListener(this);

    return view;
}

@Override
public void onClick(View v) {
    btn_eventList.setText("TEST");
    Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
}

}

Here is my layout: 这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nvd"
    android:layout_height="match_parent"
    android:layout_width="match_parent">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:fontFamily="sans-serif"
                    android:gravity="center"
                    android:text="test"
                    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                    />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView2"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="1dp"
                    android:gravity="center"
                    android:text="test"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="45dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/buttonEventList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/lst_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        >

    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>

Does anyone know why the onClick method never gets called when I hit my button? 有没有人知道为什么当我按下按钮时onClick方法永远不会被调用? Did I miss anything? 我错过了什么吗?

EDIT 编辑

Since Sevin made me notice I was not giving you a solution but an alternative way for reaching your goal, I'm editing this. 由于Sevin让我注意到我没有给你一个解决方案,而是另一种达到目标的方法,我正在编辑这个。

First: your code must work 第一:你的代码必须工作

The code you posted is correct 您发布的代码是正确的

Now, do some checks: 现在,做一些检查:

  • Check if in debug mode the button is not null (should have throwed exception) 检查在调试模式下按钮是否为空(应该抛出异常)
  • Check if in debug mode you are reaching the onClick, even if the text doesn't change and even if the toast doesn't show up. 检查在调试模式下是否到达onClick,即使文本没有改变,即使吐司没有显示。
  • Check if you are showing the fragment properly. 检查您是否正确显示片段。 Here is the official documentation about how to create a fragment. 以下是有关如何创建片段的官方文档。
  • Check if the fragment is clickable and if it has anything over it (simply, the button once clicked shows the classic "click" animation?) 检查片段是否可点击以及它是否有任何内容(简单地说,单击按钮会显示经典的“点击”动画?)
  • Check in the xaml code if the id you used is assigned to the correct button. 如果您使用的ID分配给正确的按钮,请检查xaml代码。

And finally do some the following operations: 最后做一些以下操作:

  • Clean your project 清理您的项目
  • Rebuild your project 重建您的项目
  • Check if in both xaml and java code you have any alert (in basic configurations, yellow mark over the line) 检查xaml和java代码中是否有任何警报(在基本配置中,线上有黄色标记)
  • Uninstall the app and re-install it (pretty much the same, but since we don't know your project, it can still help) 卸载应用程序并重新安装(几乎相同,但由于我们不知道您的项目,它仍然可以帮助)

After those, let us know 在那之后,让我们知道

Possible solution: 可能的方法:

You have a recycleview which is on top of the button . 您有一个位于button顶部的recycleview you have this view set with match parent both height and width inside a relativelayout , it means that this view will cover the button. 你有这个视图设置match parent身高和宽度都在relativelayout ,这意味着这个视图将覆盖按钮。

Remove this empty recycleview and the code will work 删除此空recycleview ,代码将起作用

EDIT 编辑

after posting your layout I can conclude with total certainty that your REcyclerView is taking all the space of your layout thus taking all the touch inputs from whats behind. 在发布您的布局之后,我可以完全确定您的REcyclerView占用了布局的所有空间,从而从后面的所有触摸输入中获取。

Solutions: 解决方案:

You can either delete the recycler view or re-arrange it and add it to last RelativeLayout and use layout_below and placing it below the a LinearLayout 您可以删除回收站视图或重新排列它并将其添加到layout_below并使用layout_below并将其放在LinearLayout下面

The problem is that you didn't implement this method right. 问题是您没有正确实现此方法。 onClick() method doesn't know, what view concretely has been clicked. onClick()方法不知道,具体点击了什么视图。 Change your onClick() method to the following form: 将onClick()方法更改为以下格式:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.your_button_id: 
            btn_eventList.setText("TEST");
            Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM