Android中ViewPager+Fragment的基本使用
这几天学习了一下ViewPager+Fragement的基本使用方法并写了个Demo。现将代码和效果图放上。
首先是布局文件
<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" tools:context=".MainActivity" > <!-- ViewPager组件 --> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- PagerTabStrip是标签页的切换效果 --> <android.support.v4.view.PagerTabStrip android:id="@+id/pagertab" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v4.view.ViewPager> </RelativeLayout> <!--注意事项: 1.这里ViewPager和 PagerTabStrip都要把包名写全了,不然会ClassNotFount 2.API中说:在布局xml把PagerTabStrip当做ViewPager的一个子标签来用,不能拿出来,不然还是会报错 -->
2.MainActivity.java
MainActivity主要就做了一些加载控件和实例化Fragment的事情,重点要注意的是MyViewPagerAdapter这个适配器的内部类。
package com.xlp.myviewpagerfragment; import java.util.ArrayList; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends FragmentActivity { private ViewPager m_vp; // 通过pagerTabStrip可以设置标题的属性 private PagerTabStrip pagerTabStrip; private Fragment1 mfragment1; private Fragment2 mfragment2; private Fragment3 mfragment3; // 页面列表 private ArrayList<Fragment> fragmentList; // 标题列表 private ArrayList<String> titleList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } public void initView() { m_vp = (ViewPager) findViewById(R.id.viewpager); pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagertab); // 设置下划线颜色 pagerTabStrip.setTabIndicatorColor(getResources().getColor( android.R.color.holo_green_dark)); pagerTabStrip.setBackgroundColor(getResources().getColor( android.R.color.holo_red_dark)); mfragment1 = new Fragment1(); mfragment2 = new Fragment2(); mfragment3 = new Fragment3(); fragmentList = new ArrayList<Fragment>(); fragmentList.add(mfragment1); fragmentList.add(mfragment2); fragmentList.add(mfragment3); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); m_vp.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager())); } public class MyViewPagerAdapter extends FragmentPagerAdapter { public MyViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { return fragmentList.get(arg0); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
3.Fragment1.java
其中的一个Fragment页面,这里只展示一个,其他写法相同
package com.xlp.myviewpagerfragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { private View mMainView; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Log.i("xlp", "fragment1-->oncreate()"); //动态加载布局文件 LayoutInflater inflater = getActivity().getLayoutInflater(); mMainView = inflater.inflate(R.layout.fragment1, (ViewGroup) getActivity().findViewById(R.id.viewpager), false); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i("xlp", "fragment1-->onCreateView()"); ViewGroup viewGroup = (ViewGroup) mMainView.getParent(); return mMainView; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.v("xlp", "fragment1-->onDestroy()"); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); Log.v("xlp", "fragment1-->onPause()"); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); Log.v("xlp", "fragment1-->onResume()"); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); Log.v("xlp", "fragment1-->onStart()"); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); Log.v("xlp", "fragment1-->onStop()"); } }
4.最后放上效果图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。