标签:
效果图:
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v4.view.PagerTabStrip android:id="@+id/pagerTab" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/fragment_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
java代码
public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private PagerTabStrip pagerTab; private List<String> titles; //标签 private List<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化页面 initView(); //初始化标题数组 initTitle(); //初始化fragments initFragment(); FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); } private void initView() { viewPager = (ViewPager) findViewById(R.id.viewpager); pagerTab = (PagerTabStrip) findViewById(R.id.pagerTab); pagerTab.setBackgroundColor(Color.GREEN); pagerTab.setTabIndicatorColor(Color.RED); pagerTab.setDrawFullUnderline(true); } private void initTitle() { titles = new ArrayList<String>(); for(int i = 0;i<10;i++){ titles.add("第"+i+"个标签"); } } private void initFragment() { fragments = new ArrayList<Fragment>(); for(int i = 0;i<titles.size();i++){ MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("content","第"+i+"个fragment"); myFragment.setArguments(bundle); fragments.add(myFragment); } } class FragmentAdapter extends FragmentPagerAdapter{ public FragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } } }
public class MyFragment extends Fragment { private String content; private TextView fragment_textview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle bundle = getArguments(); content = bundle.getString("content","没有内容"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.myfragment,container,false); fragment_textview = (TextView) view.findViewById(R.id.fragment_textview); return view; } @Override public void onActivityCreated( Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); fragment_textview.setText(content); } }
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5396632.html