欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

多个Fragmen之间滑动切换实例源代码下载

程序员文章站 2022-03-01 13:45:14
...

用Viewpager和fragment实现了滑动切换页面和点击切换的功能、功能简单、适合初学者、有兴趣的哥们可以下载源代码看看、虽然代码简单、但功能还是非常不错的、效果图如下

多个Fragmen之间滑动切换实例源代码下载


onCreate代码

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	setlinearLayouts();
	settextview();		
	
	viewPagers = (ViewPager) findViewById(R.id.viewPager);
	List<Fragment> totalFragment = new ArrayList<Fragment>();
	//把页面添加到ViewPager里
	totalFragment.add(new AccountFragment01());
	totalFragment.add(new AccountFragment02());
	totalFragment.add(new AccountFragment03());
	
	viewPagers.setAdapter(new FragmentAdapter(getSupportFragmentManager(),
			totalFragment));
	//设置显示哪页
	viewPagers.setCurrentItem(0);
	viewPagers.setOnPageChangeListener(new OnPageChangeListener() {
		@Override
		public void onPageSelected(int arg0) {
			resetlaybg();
			linearLayouts[arg0]
			.setBackgroundResource(R.drawable.linearlayout01s);
			textViews[arg0].setTextColor(getResources().getColor(
					R.color.textcolor));
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}
	});
}


初始化linerlayout

public void setlinearLayouts() {
	linearLayouts = new LinearLayout[3];
	linearLayouts[0] = (LinearLayout) findViewById(R.id.lay1);
	linearLayouts[1] = (LinearLayout) findViewById(R.id.lay2);
	linearLayouts[2] = (LinearLayout) findViewById(R.id.lay3);
	linearLayouts[0].setBackgroundResource(R.drawable.linearlayout01s);

}


初始化textview

public void settextview() {
	textViews = new TextView[3];
	textViews[0] = (TextView) findViewById(R.id.fratext1);
	textViews[1] = (TextView) findViewById(R.id.fratext2);
	textViews[2] = (TextView) findViewById(R.id.fratext3);
	textViews[1].setTextColor(getResources()
			.getColor(R.color.textcolor));
}


最后贴上Demo的源代码、下载就可以运行的哈

源代码链接: http://dwtedx.com/download.html?bdkey=s/1ntOd1sl 密码: n6jy