丽-pc

导航

 

首先设置布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.myradiobutton.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></android.support.v4.view.ViewPager>
    
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/xinwen"
            android:text="新闻"
            android:drawableTop="@drawable/select_main_xinwen"
            android:checked="true"
            style="@style/ab_style" />
        <RadioButton
            android:id="@+id/pinglun"
            android:text="评论"
            android:drawableTop="@drawable/select_main_pinlun"
            android:checked="true"
            style="@style/ab_style" />
        <RadioButton
            android:id="@+id/yonghu"
            android:text="用户"
            android:drawableTop="@drawable/select_main_yonghu"
            android:checked="true"
            style="@style/ab_style" />
        <RadioButton
            android:id="@+id/shezhi"
            android:text="设置"
            android:drawableTop="@drawable/select_main_setting"
            android:checked="true"
            style="@style/ab_style" />


    </RadioGroup>
</LinearLayout>
其中style样式设置:右键生成公共样式

然后设置每个Fragment页面

然后设置adapter适配器:

package com.gfd.tab;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.gfd.tab.ui.fragment.HomeFragment;
import com.gfd.tab.ui.fragment.LoveFragment;
import com.gfd.tab.ui.fragment.RenFragment;
import com.gfd.tab.ui.fragment.VedioFragment;

/*
*  PagerAdapter
*
*  FragmentPagerAdapter
*
*  FragmentStatePagerAdapter
*
*
*
 */
public class HomePagerAdapter extends FragmentPagerAdapter {

    public HomePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            case 1:
                fragment = new LoveFragment();
                break;
            case 2:
                fragment = new RenFragment();
                break;
            case 3:
                fragment = new VedioFragment();
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }


   /* @Override
    public int getCount() {
        return 5;
    }

    *//**
     * 作用:是否创建一个新页面 : true:不创建 false :创建
     * @param view : 当前页面展示的视图
     * @param object :将要滑进来的视图
     * *//*
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.e("tag","instantiateItem -----" + position);
        TextView view = new TextView(container.getContext());
        view.setText("fffffffffff");
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Log.e("tag","destroyItem -----" + position);
    }*/
}
最后activity设置

package com.gfd.tab.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gfd.tab.HomePagerAdapter;
import com.gfd.tab.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.vp_home)
    ViewPager vpHome;
    @BindView(R.id.rb_mian_home)
    RadioButton rbMianHome;
    @BindView(R.id.rb_mian_love)
    RadioButton rbMianLove;
    @BindView(R.id.rb_mian_ren)
    RadioButton rbMianRen;
    @BindView(R.id.rb_mian_vedio)
    RadioButton rbMianVedio;
    @BindView(R.id.radiogroup)
    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        vpHome.setAdapter(new HomePagerAdapter(getSupportFragmentManager()));
        vpHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(position);
                radioButton.setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @OnClick({R.id.rb_mian_home, R.id.rb_mian_love, R.id.rb_mian_ren, R.id.rb_mian_vedio})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_mian_home://首页
                vpHome.setCurrentItem(0,false);
                break;
            case R.id.rb_mian_love://关注
                vpHome.setCurrentItem(1,false);
                break;
            case R.id.rb_mian_ren://联系人
                vpHome.setCurrentItem(2,false);
                break;
            case R.id.rb_mian_vedio://视频
                vpHome.setCurrentItem(3,false);
                break;
        }
    }
}
--------------------

以上列子与之前的不同,代码少了哈哈,之前将Fragment放入了集合中使用,具体详看另个文件!!!

2.静态页面,点击改变上面页面不滑动代码:

需改变,布局:

activity:

使用事务完成:

package com.gfd.tab.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gfd.tab.R;
import com.gfd.tab.ui.fragment.HomeFragment;
import com.gfd.tab.ui.fragment.LoveFragment;
import com.gfd.tab.ui.fragment.RenFragment;
import com.gfd.tab.ui.fragment.VedioFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SecondActivity extends AppCompatActivity {


    @BindView(R.id.frame_layout)
    FrameLayout layout;
    @BindView(R.id.rb_mian_home)
    RadioButton rbMianHome;
    @BindView(R.id.rb_mian_love)
    RadioButton rbMianLove;
    @BindView(R.id.rb_mian_ren)
    RadioButton rbMianRen;
    @BindView(R.id.rb_mian_vedio)
    RadioButton rbMianVedio;
    @BindView(R.id.radiogroup)
    RadioGroup radioGroup;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_layout,new HomeFragment()).commit();

    }

    @OnClick({R.id.rb_mian_home, R.id.rb_mian_love, R.id.rb_mian_ren, R.id.rb_mian_vedio})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_mian_home://首页
                fragmentManager.beginTransaction().replace(R.id.frame_layout,new HomeFragment()).commit();
                break;
            case R.id.rb_mian_love://关注
                fragmentManager.beginTransaction().replace(R.id.frame_layout,new LoveFragment()).commit();
                break;
            case R.id.rb_mian_ren://联系人
                fragmentManager.beginTransaction().replace(R.id.frame_layout,new RenFragment()).commit();
                break;
            case R.id.rb_mian_vedio://视频
                fragmentManager.beginTransaction().replace(R.id.frame_layout,new VedioFragment()).commit();
                break;
        }
    }
}
-------------除去adapter,-----

posted on 2017-02-13 18:04  丽丽博客园  阅读(238)  评论(0编辑  收藏  举报