注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

罔少年

QQ 332256483

 
 
 

日志

 
 

ViewPager+Fragment  

2016-07-21 09:35:39|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
fragment_info_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
MainInfoFragment.class
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Toast;

import com.zjcpo.mobileapp.info.InfoFragmentAdapter;
import com.zjcpo.mobileapp.info.InfoFragmentOne;

import java.util.ArrayList;

/**
* Created by jiemiao.zhang on 2016/6/23.
*/
public class MainInfoFragment extends Fragment implements ViewPager.OnPageChangeListener, InfoFragmentOne.OnFragmentInteractionListener{

private ViewPager viewpage;

private ArrayList<Fragment> fragmentList;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_info_home, container, false);

viewpage = (ViewPager) view.findViewById(R.id.viewpager);

initViewPage();

return view;
}



private void initViewPage() {

fragmentList = new ArrayList<Fragment>();

Fragment fragment1 = InfoFragmentOne.newInstance("我是one1","我是one2");

Fragment fragment2 = InfoFragmentOne.newInstance("我是two1","我是two2");

Fragment fragment3 = InfoFragmentOne.newInstance("我是two1","我是two2");

Fragment fragment4 = InfoFragmentOne.newInstance("我是three1","我是three2");

Fragment fragment5 = InfoFragmentOne.newInstance("我是four1","我是four2");

fragmentList.add(fragment1);

fragmentList.add(fragment2);

fragmentList.add(fragment3);

fragmentList.add(fragment4);

fragmentList.add(fragment5);

viewpage.setAdapter(new InfoFragmentAdapter(getFragmentManager(), fragmentList));

viewpage.addOnPageChangeListener(this);


}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

Toast.makeText(getActivity(), position + "", Toast.LENGTH_SHORT).show();

}

@Override
public void onPageScrollStateChanged(int state) {

}


@Override
public void onFragmentInteraction(Uri uri) {

}
}
InfoFragmentAdapter.class
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
* Created by jiemiao.zhang on 2016-7-20.
*/
public class InfoFragmentAdapter extends FragmentPagerAdapter {


private List<Fragment> fragments;

public InfoFragmentAdapter(FragmentManager fm, ArrayList<Fragment> mFragments) {

super(fm);

this.fragments = mFragments;
}

@Override
public Fragment getItem(int position) {

return fragments.get(position);

}

@Override
public int getCount() {

return fragments.size();
}
}

有几个地方要注意:
1、xml中必须是<android.support.v4.view.viewPager 
2、在父Fragment中,必须实现OnFragmentInteractionListener 接口
3、Fragment的OnCreate()只会执行一次,但OnCreateView()会在每次滑动后都执行
  评论这张
 
阅读(10)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017