すごく初歩的なのですが、いくら調べてもわからなかったので質問させてください。
画像のように、画面の下にナビゲーションをつけたのですが、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
回答1件
あなたの回答
tips
プレビュー