首页 > 代码库 > Android中ViewPager+Fragment的基本使用
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.最后放上效果图
Android中ViewPager+Fragment的基本使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。