アクティビティに保持されているフラグメントに浮動アクションボタンを追加しました。ユーザーがクリックすると、新しいアクティビティが開始されますが、何も起こりません。ここでフラグメント内のfabがクリックイベントに応答しないのはなぜですか?
は、フラグメントレイアウトである:ここで
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/review_recycler_view"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_review_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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が、これは、アクティビティによって処理されなければならないのですか?
ありがとうございます。
A sugg estion-あなたはデータバインディングを使用し、findViewByIdを避けることができます。とにかくデバッグして、onClickに何かを記録してみてください – Raghunandan