レイアウトにフローティング アクション ボタンがあり、クリックすると別のビューが開くようにしたいと考えています。この回答から、クリックイベントを処理する良い方法は次のとおりであると読みました。
public class AllTasksFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_all_tasks, container, false);
FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View rootView) {
switch (rootView.getId()) {
case R.id.fab:
setContentView(R.layout.fragment_new_task);
break;
}
}}
ただし、setContentView
メソッドはフラグメントで使用できないため、Android Studio はエラーを返します: Cannot resolve method setContentView(int)
.
onClick メソッドを介して別のビューを表示するには、setContentView の代わりに何を使用できますか? それとも、まったく別の方法でこれにアプローチする必要がありますか?