Last active
February 6, 2016 15:09
-
-
Save Khanashima/16b5dbb0a20c116bd1ec to your computer and use it in GitHub Desktop.
deprecatedのないTab画面の作成方法 ref: http://qiita.com/kiimiiis/items/34a6fc3df279300b60bd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main2Activity extends AppCompatActivity implements Tab1Fragment.OnFragmentInteractionListener, Tab2Fragment.OnFragmentInteractionListener { | |
/** | |
* The {@link android.support.v4.view.PagerAdapter} that will provide | |
* fragments for each of the sections. We use a | |
* {@link FragmentPagerAdapter} derivative, which will keep every | |
* loaded fragment in memory. If this becomes too memory intensive, it | |
* may be best to switch to a | |
* {@link android.support.v4.app.FragmentStatePagerAdapter}. | |
*/ | |
private SectionsPagerAdapter mSectionsPagerAdapter; | |
/** | |
* The {@link ViewPager} that will host the section contents. | |
*/ | |
private ViewPager mViewPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main2); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
//フラグメントの設定 | |
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); | |
//viewPagerの設定 | |
mViewPager = (ViewPager) findViewById(R.id.container); | |
mViewPager.setAdapter(mSectionsPagerAdapter); | |
//タブの設定 | |
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); | |
//Tabを横スクロールで切り替えれるようにViewpagerに設定 | |
tabLayout.setupWithViewPager(mViewPager); | |
//Tabのタイトルを設定 | |
tabLayout.getTabAt(0).setText("Tab1"); | |
//Tabのタイトルに画像を設定 | |
tabLayout.getTabAt(0).setIcon(R.drawable.ic_launcher); | |
tabLayout.getTabAt(1).setText("Tab2"); | |
} | |
@Override | |
public void onFragmentInteraction(Uri uri) { | |
} | |
@Override | |
public void tab1onFragmentInteraction(Uri uri) { | |
} | |
/** | |
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to | |
* one of the sections/tabs/pages. | |
*/ | |
public class SectionsPagerAdapter extends FragmentPagerAdapter { | |
public SectionsPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
//タブによってどのフラグメントを使用するか設定 | |
Fragment fragment; | |
switch (position) { | |
case 0: | |
fragment = Tab1Fragment.newInstance("text1", "text2"); | |
break; | |
case 1: | |
fragment = Tab2Fragment.newInstance("text3", "text4"); | |
break; | |
default: | |
fragment = Tab1Fragment.newInstance("text1", "text2"); | |
break; | |
} | |
return fragment; | |
} | |
@Override | |
public int getCount() { | |
//Tabの画面数を設定 | |
return 2; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple {@link Fragment} subclass. | |
* Activities that contain this fragment must implement the | |
* {@link Tab1Fragment.OnFragmentInteractionListener} interface | |
* to handle interaction events. | |
* Use the {@link Tab1Fragment#newInstance} factory method to | |
* create an instance of this fragment. | |
* | |
*/ | |
public class Tab1Fragment extends Fragment { | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private static final String ARG_PARAM1 = "param1"; | |
private static final String ARG_PARAM2 = "param2"; | |
// TODO: Rename and change types of parameters | |
private String mParam1; | |
private String mParam2; | |
private OnFragmentInteractionListener mListener; | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param param1 Parameter 1. | |
* @param param2 Parameter 2. | |
* @return A new instance of fragment Tab1Fragment. | |
*/ | |
// TODO: Rename and change types and number of parameters | |
public static Tab1Fragment newInstance(String param1, String param2) { | |
Tab1Fragment fragment = new Tab1Fragment(); | |
Bundle args = new Bundle(); | |
args.putString(ARG_PARAM1, param1); | |
args.putString(ARG_PARAM2, param2); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
public Tab1Fragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
mParam1 = getArguments().getString(ARG_PARAM1); | |
mParam2 = getArguments().getString(ARG_PARAM2); | |
} | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_tab1, container, false); | |
} | |
// TODO: Rename method, update argument and hook method into UI event | |
public void onButtonPressed(Uri uri) { | |
if (mListener != null) { | |
mListener.tab1onFragmentInteraction(uri); | |
} | |
} | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
if (context instanceof OnFragmentInteractionListener) { | |
mListener = (OnFragmentInteractionListener) context; | |
} else { | |
throw new RuntimeException(context.toString() | |
+ " must implement OnFragmentInteractionListener"); | |
} | |
} | |
@Override | |
public void onDetach() { | |
super.onDetach(); | |
mListener = null; | |
} | |
/** | |
* This interface must be implemented by activities that contain this | |
* fragment to allow an interaction in this fragment to be communicated | |
* to the activity and potentially other fragments contained in that | |
* activity. | |
* <p> | |
* See the Android Training lesson <a href= | |
* "http://developer.android.com/training/basics/fragments/communicating.html" | |
* >Communicating with Other Fragments</a> for more information. | |
*/ | |
public interface OnFragmentInteractionListener { | |
// TODO: Update argument type and name | |
void tab1onFragmentInteraction(Uri uri); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment