質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

4743閲覧

[AndroidStudio] bottom navigationでページ切り替えができない。

TA-KEY

総合スコア21

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

1グッド

0クリップ

投稿2018/06/02 12:48

編集2018/06/03 09:43

すごく初歩的なのですが、いくら調べてもわからなかったので質問させてください。
画像のように、画面の下にナビゲーションをつけたのですが、practice、setdec、examinationのどれを選んでも同じ画面になっています。

・ナビゲーションでの画面の切り替えは、アクティビティを複数使って、切り替えるのでしょうか?
(この場合だと、mainとPracticeとSetDeckとExaminationの4つのアクティビティが必要?)

・もしくは、フラグメントを作成して、画面を切り替えるべきでしょうか。
(そうなると、mainアクティビティ1つと、フラグメントが3つ?)

全く違う画面に切り替える予定です。

イメージ説明

xml

1 2<?xml version="1.0" encoding="utf-8"?> 3<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:id="@+id/container" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context="@android:string/VideoView_error_button"> 10 11 <TextView 12 android:id="@+id/message" 13 android:layout_width="wrap_content" 14 android:layout_height="21dp" 15 android:layout_marginTop="12dp" 16 android:autoText="false" 17 android:text="@string/game_title" 18 android:textAppearance="@style/TextAppearance.AppCompat.Body2" 19 android:textSize="12sp" 20 app:fontFamily="serif" 21 app:layout_constraintTop_toTopOf="parent" 22 tools:layout_editor_absoluteX="16dp" /> 23 24 <android.support.design.widget.BottomNavigationView 25 android:id="@+id/navigation" 26 android:layout_width="0dp" 27 android:layout_height="56dp" 28 android:layout_marginBottom="8dp" 29 android:addStatesFromChildren="true" 30 android:background="#733c5b81" 31 app:layout_constraintBottom_toBottomOf="parent" 32 app:layout_constraintEnd_toEndOf="parent" 33 app:layout_constraintLeft_toLeftOf="parent" 34 app:layout_constraintRight_toRightOf="parent" 35 app:menu="@menu/navigation"> 36 37 </android.support.design.widget.BottomNavigationView> 38 39 <LinearLayout 40 android:layout_width="288dp" 41 android:layout_height="37dp" 42 android:orientation="horizontal" 43 tools:layout_editor_absoluteX="48dp" 44 tools:layout_editor_absoluteY="33dp"> 45 46 <ImageView 47 android:id="@+id/imageView8" 48 android:layout_width="match_parent" 49 android:layout_height="match_parent" 50 android:layout_weight="1" 51 app:srcCompat="@drawable/hatena" /> 52 53 <ImageView 54 android:id="@+id/imageView7" 55 android:layout_width="wrap_content" 56 android:layout_height="match_parent" 57 android:layout_weight="1" 58 app:srcCompat="@drawable/hatena" /> 59 60 <ImageView 61 android:id="@+id/imageView6" 62 android:layout_width="match_parent" 63 android:layout_height="match_parent" 64 android:layout_weight="1" 65 app:srcCompat="@drawable/hatena" /> 66 67 <ImageView 68 android:id="@+id/imageView5" 69 android:layout_width="match_parent" 70 android:layout_height="match_parent" 71 android:layout_weight="1" 72 app:srcCompat="@drawable/hatena" 73 tools:layout_editor_absoluteX="117dp" 74 tools:layout_editor_absoluteY="91dp" /> 75 76 <Button 77 android:id="@+id/button" 78 android:layout_width="62dp" 79 android:layout_height="33dp" 80 android:text="On/Off" 81 android:textSize="10sp" 82 tools:layout_editor_absoluteX="306dp" 83 tools:layout_editor_absoluteY="37dp" /> 84 85 </LinearLayout> 86 87 88 89 90</android.support.constraint.ConstraintLayout> 91

java

1Main.Activity.java 2 3package com.example.taiki.grasphand; 4 5import android.os.Bundle; 6import android.support.annotation.NonNull; 7import android.support.design.widget.BottomNavigationView; 8import android.support.v7.app.AppCompatActivity; 9import android.view.MenuItem; 10import android.widget.TextView; 11 12public class MainActivity extends AppCompatActivity { 13 14 private TextView mTextMessage; 15 16 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 17 = new BottomNavigationView.OnNavigationItemSelectedListener() { 18 19 @Override 20 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 21 //Fragment selectedFragment = null; 22 switch (item.getItemId()) { 23 case R.id.navigation_home: 24 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 25 transaction.replace(R.id.container, page1.newInstance()); 26 transaction.commit(); 27 return true; 28 case R.id.navigation_dashboard: 29 return true; 30 case R.id.navigation_notifications: 31 return true; 32 } 33 return false; 34 } 35 }; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 42 mTextMessage = (TextView) findViewById(R.id.message); 43 BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 44 navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 45 } 46 47} 48

java

1page1.java 2 3package com.example.taiki.grasphand; 4 5import android.content.Context; 6import android.net.Uri; 7import android.os.Bundle; 8import android.support.v4.app.Fragment; 9import android.view.LayoutInflater; 10import android.view.View; 11import android.view.ViewGroup; 12 13 14/** 15* A simple {@link Fragment} subclass. 16* Activities that contain this fragment must implement the 17* {@link page1.OnFragmentInteractionListener} interface 18* to handle interaction events. 19* Use the {@link page1#newInstance} factory method to 20* create an instance of this fragment. 21*/ 22public class page1 extends Fragment { 23 // TODO: Rename parameter arguments, choose names that match 24 // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 25 private static final String ARG_PARAM1 = "param1"; 26 private static final String ARG_PARAM2 = "param2"; 27 28 // TODO: Rename and change types of parameters 29 private String mParam1; 30 private String mParam2; 31 32 private OnFragmentInteractionListener mListener; 33 34 public page1() { 35 // Required empty public constructor 36 } 37 38 /** 39 * Use this factory method to create a new instance of 40 * this fragment using the provided parameters. 41 * 42 * @param param1 Parameter 1. 43 * @param param2 Parameter 2. 44 * @return A new instance of fragment page1. 45 */ 46 // TODO: Rename and change types and number of parameters 47 public static page1 newInstance(String param1, String param2) { 48 page1 fragment = new page1(); 49 Bundle args = new Bundle(); 50 args.putString(ARG_PARAM1, param1); 51 args.putString(ARG_PARAM2, param2); 52 fragment.setArguments(args); 53 return fragment; 54 } 55 56 57 58 @Override 59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 if (getArguments() != null) { 62 mParam1 = getArguments().getString(ARG_PARAM1); 63 mParam2 = getArguments().getString(ARG_PARAM2); 64 } 65 } 66 67 @Override 68 public View onCreateView(LayoutInflater inflater, ViewGroup container, 69 Bundle savedInstanceState) { 70 // Inflate the layout for this fragment 71 return inflater.inflate(R.layout.fragment_page1, container, false); 72 } 73 74 // TODO: Rename method, update argument and hook method into UI event 75 public void onButtonPressed(Uri uri) { 76 if (mListener != null) { 77 mListener.onFragmentInteraction(uri); 78 } 79 } 80 81 @Override 82 public void onAttach(Context context) { 83 super.onAttach(context); 84 if (context instanceof OnFragmentInteractionListener) { 85 mListener = (OnFragmentInteractionListener) context; 86 } else { 87 throw new RuntimeException(context.toString() 88 + " must implement OnFragmentInteractionListener"); 89 } 90 } 91 92 @Override 93 public void onDetach() { 94 super.onDetach(); 95 mListener = null; 96 } 97 98 /** 99 * This interface must be implemented by activities that contain this 100 * fragment to allow an interaction in this fragment to be communicated 101 * to the activity and potentially other fragments contained in that 102 * activity. 103 * <p> 104 * See the Android Training lesson <a href= 105 * "http://developer.android.com/training/basics/fragments/communicating.html" 106 * >Communicating with Other Fragments</a> for more information. 107 */ 108 public interface OnFragmentInteractionListener { 109 // TODO: Update argument type and name 110 void onFragmentInteraction(Uri uri); 111 } 112} 113
xenbeat👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

xenbeat

2018/06/02 14:08

ソースコードを追記しましょう
TA-KEY

2018/06/02 14:23

xmlファイルのコードを追加しました!
xenbeat

2018/06/02 21:10

Activityもです!
TA-KEY

2018/06/03 02:37

すいません、追加しました。よろしくお願いします
guest

回答1

0

ベストアンサー

画像のように、画面の下にナビゲーションをつけたのですが、practice、setdec、examinationのどれを選んでも同じ画面になっています。

選択されたときに画面遷移の処理が入ってない(テキストだけ設定している)ためそうなります。

Java

1 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 2 switch (item.getItemId()) { 3 case R.id.navigation_home: 4 mTextMessage.setText(R.string.title_home); 5 return true; 6 case R.id.navigation_dashboard: 7 mTextMessage.setText(R.string.title_dashboard); 8 return true; 9 case R.id.navigation_notifications: 10 mTextMessage.setText(R.string.title_notifications); 11 return true; 12 } 13 return false; 14 }

・ナビゲーションでの画面の切り替えは、アクティビティを複数使って、切り替えるのでしょうか?

・もしくは、フラグメントを作成して、画面を切り替えるべきでしょうか。

どちらでも出来ますがフラグメントの方が容易に実現できるかと思います。

全く違う画面に切り替える予定です。

BottomNavigation以前に、ActivityやFragmentによる遷移がわからないのであれば、まずはそちらから学習されたほうが良いと思います。
一応、今回の参考になりそうなページだけは記載しておきます。

投稿2018/06/03 03:45

xenbeat

総合スコア4258

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TA-KEY

2018/06/03 09:41

回答ありがとうございます。 フラグメントで切り替えることにして、フラグメントを継承したクラスとそれに対応したxmlファイルを作成しました。また、選択された時の処理を追記したのですが、newInstance()の引数に何を渡せばいいのか調べてもわかりません。また、引数なしのnewInstance()を作成の仕方も結局わからず止まってしまっています。助けていただけると幸いです。
TA-KEY

2018/06/04 14:21

すみません、解決しました。page1()というコンストラクがちゃんとありました。 また、 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問