ViewPager+Fragment的布局

package com.wzm.viewpager;


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


import android.graphics.Color;
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.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


public class MyFragmentActivity extends FragmentActivity {
    private ViewPager pager;
    private List<Fragment> list;
    private List<TextView> tvs;
    private List<View> vs;
    private TextView tv1,tv2,tv3;
    private View v1,v2,v3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_myfragment);
initTextView();
initView();
initData();
initpager();

}

private void initView() {
v1=(View) findViewById(R.id.v1);
v2=(View) findViewById(R.id.v2);
v3=(View) findViewById(R.id.v3);
v1.setBackgroundColor(Color.RED);
}


private void initTextView() {
tv1=(TextView) findViewById(R.id.tv1);
    tv2=(TextView) findViewById(R.id.tv2);
    tv3=(TextView) findViewById(R.id.tv3);
    tv1.setTextColor(Color.RED);
    tv1.setOnClickListener(new MyOnClickListener(0));
    tv2.setOnClickListener(new MyOnClickListener(1));
    tv3.setOnClickListener(new MyOnClickListener(2));
}
    private class MyOnClickListener implements OnClickListener{
private int index;
public MyOnClickListener(int index){
this.index=index;
}
    @Override
public void onClick(View v) {
    //设置切换页面
pager.setCurrentItem(index);
vs.get(index).setBackgroundColor(Color.RED);
}
   
    }
private void initData() {
list=new ArrayList<Fragment>();
list.add(new FirstFragment());
list.add(new SecondFragment());
list.add(new ThirdFragment());
tvs=new ArrayList<TextView>();
tvs.add(tv1);
tvs.add(tv2);
tvs.add(tv3);
vs=new ArrayList<View>();
vs.add(v1);
vs.add(v2);
vs.add(v3);
}


private void initpager() {
pager=(ViewPager) findViewById(R.id.pager);

//获取片段管理器对象
pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
pager.setOnPageChangeListener(new MyOnPageChangeListener());
}
    
private class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
//pager监听器
private class MyOnPageChangeListener implements OnPageChangeListener{
@Override
public void onPageScrollStateChanged(int arg0) {

}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}
@Override
public void onPageSelected(int arg0) {
for(int i=0;i<tvs.size();i++){
if(i==arg0){
tvs.get(i).setTextColor(Color.RED);
vs.get(i).setBackgroundColor(Color.RED);
}else{
tvs.get(i).setTextColor(Color.BLUE);
vs.get(i).setBackgroundColor(0xffd2d422);
}
}
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值