[英]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? 我错过了什么吗?
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: 现在,做一些检查:
And finally do some the following operations: 最后做一些以下操作:
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.