今天要來寫tablayout,我要寫出以下功能,如圖:
首先,先在案子中建立一個tabed activity
選擇action bar tabs:
執行完後,他就會出現基本的程式碼:
Mytab.java:
public class mytab extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mytab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mytab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mytab, container, false);
// TextView textView = (TextView) rootView.findViewById(R.id.section_label);
// textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
}
}
這時建立一個 Frag1.java 和 frag_layout.xml
Frag1.java:
public class Frag1 extends Fragment {
Button bt1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1_layout, null);
bt1= (Button)view.findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
btlink(v);}
});
return view;
// return inflater.inflate(R.layout.frag1_layout, container, false);
}
public void btlink(View view)
{
Intent intent=new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://akkyschool.com"));
startActivity(intent);
}
}
frag_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
以上frag.java和xml的程式再複製第二個(frag2)
這時,再到mytab.java改點東西,將Fragment getItem改成以下:
@Override public Fragment getItem(int position) { Fragment fragment = null; switch (position) { case 0: fragment = new Frag1(); break; case 1: fragment = new Frag2(); break; } return fragment; // return PlaceholderFragment.newInstance(position + 1); }
這樣就OK了~~
以上
文章標籤
全站熱搜
留言列表