为啥片段中的晶圆厂不响应点击事件?
Posted
技术标签:
【中文标题】为啥片段中的晶圆厂不响应点击事件?【英文标题】:Why is fab in fragment not responding to click events?为什么片段中的晶圆厂不响应点击事件? 【发布时间】:2017-02-10 03:04:12 【问题描述】:我已向活动中的片段添加了一个浮动操作按钮。当用户点击它时,应该会启动一个新的活动,但什么也没有发生。
这是片段布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.neutronstar.revu.fragment.FeedFragment">
<android.support.v7.widget.RecyclerView
android:layout_
android:layout_
android:id="@+id/review_recycler_view"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_review_fab"
android:layout_
android:layout_
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_add_white_24dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
这里是片段类:
public class ReviewFragment extends Fragment
@BindView(R.id.add_review_fab) FloatingActionButton mAddReviewButton;
private OnReviewNext mListener;
public ReviewFragment()
// Required empty public constructor
public interface OnReviewNext
void finishReview(String description);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_review, container, false);
ButterKnife.bind(this, view);
mAddReviewButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(getActivity(), AddReviewActivity.class);
startActivity(intent);
);
return view;
@Override
public void onAttach(Context context)
super.onAttach(context);
if (context instanceof OnReviewNext)
mListener = (OnReviewNext) context;
else
throw new RuntimeException(context.toString()
+ " must implement OnReviewNext");
@Override
public void onDetach()
super.onDetach();
mListener = null;
为什么fab没有响应点击事件,应该由activity处理吗?
提前致谢。
【问题讨论】:
一个建议-您可以使用数据绑定并避免 findViewById 。无论如何尝试调试并在onClick中登录一些东西 【参考方案1】:就我而言,有 2 个 FAB。一个在 Activity 上,另一个在 Fragment 上。检查是否有 FAB 活动。 Fragments的FAB在活动的FAB下,没有收到任何的接触
【讨论】:
【参考方案2】:public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_review, container, false);
ButterKnife.bind(this, view);
mAddReviewButton = (FloatingActionButton) view.findViewByID(R.id.addReviewFab);
mAddReviewButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(getActivity(), AddReviewActivity.class);
startActivity(intent);
);
return view;
【讨论】:
【参考方案3】:检查项目级 build.gradle:
buildscript
repositories
mavenCentral()
dependencies
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
检查模块级 build.gradle:
apply plugin: 'android-apt'
android
...
dependencies
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View calcView = inflater.inflate(R.layout.content_main, container, false);
unbinder = ButterKnife.bind(this, calcView);
return calcView;
@Override
public void onDestroyView()
super.onDestroyView();
unbinder.unbind();
@OnClick(R.id.mAddReviewButton)
public void one()
Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
result.setText(result.getText().toString() + 1);
【讨论】:
【参考方案4】:尝试在xml文件中添加点击事件。
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_review_fab"
android:layout_
android:layout_
android:layout_gravity="bottom|end"
android:onClick="floatBtnClicked"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_add_white_24dp"
android:clickable="true"/>
然后将其添加到活动中
public void floatBtnClicked(View v)
【讨论】:
以上是关于为啥片段中的晶圆厂不响应点击事件?的主要内容,如果未能解决你的问题,请参考以下文章