Android_FragmentViewPager

代码块:

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;//声明对象
    private ArrayList<Fragment> fragments;//存储碎片对象
    private ArrayList<String> titles;//存储标题内容
    private RadioGroup rg;
    private PagerTabStrip tabStrip;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    //初始化
    private void init() {
        vp=(ViewPager)findViewById(R.id.vp);
        tabStrip=(PagerTabStrip)findViewById(R.id.tab);
        tabStrip.setBackgroundColor(Color.RED);// 背景颜色
        tabStrip.setTextColor(Color.WHITE);// 标题颜色,这里需要带透明度的颜色值
        tabStrip.setTabIndicatorColor(Color.WHITE);// 指示器颜色,这里需要带透明度的颜色值
        tabStrip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 26);// 字体大小
        rg=(RadioGroup)findViewById(R.id.rg);
        fragments=new ArrayList<Fragment>();
        titles=new ArrayList<String>();
        titles.add("英雄联盟");
        titles.add("Dota2");
        titles.add("王者荣耀");
        titles.add("红色警戒");
        /*for(int i=0;i<titles.size();i++)
        {*/
            MyFragment fragment1=new MyFragment();
            /*Bundle bundle1=new Bundle();
            bundle1.putString("content", titles.get(i));*/
            //fragment1.setArguments(bundle1);
            fragments.add(fragment1);
            Fragment2 fragment2=new Fragment2();
            fragments.add(fragment2);
            Fragment3 fragment3=new Fragment3();
            fragments.add(fragment3);
        //}
        MyFragmentAdapter adapter=new MyFragmentAdapter(
                getSupportFragmentManager(), fragments, titles);
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                //RadioButton rb=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
                Log.e("t1",position+"位置");
                for(int i=0;i<rg.getChildCount();i++){
                    RadioButton rb=(RadioButton) rg.getChildAt(i);
                    if(i!=position){
                        rb.setTextColor(Color.BLACK);
                    }else{
                        rb.setTextColor(Color.RED);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}

public class MyFragment extends Fragment{

	private TextView tv;
	private LinearLayout layout;
	private String msg;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_1, null);
		/*tv=(TextView)view.findViewById(R.id.tv_fragment);
		layout=(LinearLayout)view.findViewById(R.id.ly_fragment_1);
		Random rm=new Random();
		//设置布局的背景色
		layout.setBackgroundColor(Color.rgb(rm.nextInt(256), 
				rm.nextInt(256), rm.nextInt(256)));*/
		return view;
	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		/*Bundle bundle=getArguments();
		if(bundle!=null)
		{
			tv.setText(bundle.getString("content"));
		}*/
	}
}

public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment1, null);
        return view;
    }
}

public class Fragment3 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment2, null);
        return view;
    }
}


适配器:

public class MyFragmentAdapter extends FragmentPagerAdapter{

	private ArrayList<Fragment> list;
	private ArrayList<String> titles;
	public MyFragmentAdapter(FragmentManager fm,ArrayList<Fragment> list,ArrayList<String> titles) {
		super(fm);
		this.list=list;
		this.titles=titles;
	}
	//获取指定位置上的碎片对象
	@Override
	public Fragment getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}
	//获取碎片对象的个数
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}



	//获取对应位置的标题的内容
	@Override
	public CharSequence getPageTitle(int position) {
		// TODO Auto-generated method stub
		return titles.get(position);
	}

}



布局:

<LinearLayout 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"
    android:orientation="vertical" >
    <RadioGroup
        android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:button="@null"
            android:text="英雄联盟"
            android:gravity="center"
            android:textColor="@android:color/holo_red_dark"
            />
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:button="@null"
            android:text="Dota2"
            android:layout_weight="1"
            android:gravity="center"
            />
        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:button="@null"
            android:text="王者荣耀"
            android:gravity="center"
            />
        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:button="@null"
            android:layout_weight="1"
            android:text="红色警戒"
            android:gravity="center"
            />
    </RadioGroup>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp">
        <!--设置PagerTabStrip:
通过PagerTabStrip 对象的setTextColor()方法设置导航条文字颜色;
通过PagerTabStrip 对象的setBackgroundColor ()方法设置导航条背景颜色;
通过PagerTabStrip 对象的setDrawFullUnderline()方法设置导航条下方是否有完整下划线颜色;
通过PagerTabStrip 对象的setTabIndicatorColor()方法设置导航条文字下方的指示颜色;
通过PagerTabStrip 对象的setTextSpacing()方法设置导航条文字的间隔。-->
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v4.view.PagerTabStrip>
        <!-- 标题栏 -->
        <!--<android.support.v4.view.PagerTitleStrip
            android:layout_width="match_parent"
        	android:layout_height="wrap_content">
            
        </android.support.v4.view.PagerTitleStrip>-->
    </android.support.v4.view.ViewPager>

</LinearLayout>

<?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:id="@+id/ly_fragment_1">
    
	<TextView 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:textSize="40sp"
	    android:id="@+id/tv_fragment"
	    android:text="第一个页面"
	    android:layout_marginTop="100dp"
	    android:gravity="center"/>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#789938"
    android:gravity="center"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个页面"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"/>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AF7CB5"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三个页面"/>
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值