关闭

用radioGroup和radioButton实现四个热点选项卡

标签: 标题栏
178人阅读 评论(0) 收藏 举报
分类:

MainActivity

import com.example.fragment.CommonUIFragment;
import com.example.fragment.LaunchUIFragment;


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.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;


public class MainActivity extends   FragmentActivity {


    public static final String ARGUMENTS_NAME = "arg";
    private RelativeLayout rl_nav;
    private HorizontalScrollView mHsv;
    private RadioGroup rg_nav_content;
    private ImageView iv_nav_indicator;
    private ImageView iv_nav_left;
    private ImageView iv_nav_right;
    private ViewPager mViewPager;
    private int indicatorWidth;
    public static String[] tabTitle = { "选项1", "选项2", "选项3", "选项4", "选项5" };    //标题
    private LayoutInflater mInflater;
    private TabFragmentPagerAdapter mAdapter;
    private int currentIndicatorLeft = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById();
        initView();
        setListener(); 
    }


    @SuppressWarnings("deprecation")
    private void setListener() {
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if(rg_nav_content!=null && rg_nav_content.getChildCount()>position){
                    ((RadioButton)rg_nav_content.getChildAt(position)).performClick();
                }
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        rg_nav_content.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(rg_nav_content.getChildAt(checkedId)!=null){

                    TranslateAnimation animation = new TranslateAnimation(
                            currentIndicatorLeft ,
                            ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft(), 0f, 0f);
                    animation.setInterpolator(new LinearInterpolator());
                    animation.setDuration(100);
                    animation.setFillAfter(true);

                    //执行位移动画
                    iv_nav_indicator.startAnimation(animation);

                    mViewPager.setCurrentItem(checkedId);   //ViewPager 跟随一起 切换

                    //记录当前 下标的距最左侧的 距离
                    currentIndicatorLeft = ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft();

                     mHsv.smoothScrollTo(
                            (checkedId > 1 ? ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg_nav_content.getChildAt(2)).getLeft(), 0);
                 }
            }
        });
    }


    private void initView() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        indicatorWidth = dm.widthPixels / 4;
        LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
        cursor_Params.width = indicatorWidth;// 初始化滑动下标的宽
        iv_nav_indicator.setLayoutParams(cursor_Params);
        //mHsv.setSomeParam(rl_nav, iv_nav_left, iv_nav_right, this);
        //获取布局填充器
        mInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        //另一种方式获取
//      LayoutInflater mInflater = LayoutInflater.from(this);  
        initNavigationHSV();
        mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
    }


    private void initNavigationHSV() {
        rg_nav_content.removeAllViews();
        for(int i=0;i<tabTitle.length;i++){
            RadioButton rb = (RadioButton) mInflater.inflate(R.layout.nav_radiogroup_item, null);
            rb.setId(i);
            rb.setText(tabTitle[i]);
            rb.setLayoutParams(new LayoutParams(indicatorWidth,
                    LayoutParams.MATCH_PARENT));

            rg_nav_content.addView(rb);
        }

    }


    private void findViewById() {
        rl_nav = (RelativeLayout) findViewById(R.id.rl_nav);
        mHsv = (HorizontalScrollView) findViewById(R.id.mHsv);
        rg_nav_content = (RadioGroup) findViewById(R.id.rg_nav_content);
        iv_nav_indicator = (ImageView) findViewById(R.id.iv_nav_indicator);
        iv_nav_left = (ImageView) findViewById(R.id.iv_nav_left);
        iv_nav_right = (ImageView) findViewById(R.id.iv_nav_right);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
    }

    public static class TabFragmentPagerAdapter extends FragmentPagerAdapter{


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


        @Override
        public Fragment getItem(int arg0) {
            Fragment ft = null;
            switch (arg0) {
            case 0:
                ft = new LaunchUIFragment();
                break;


            default:
                ft = new CommonUIFragment();

                Bundle args = new Bundle();
                args.putString(ARGUMENTS_NAME, tabTitle[arg0]);
                ft.setArguments(args);

                break;
            }
            return ft;
        }


        @Override
        public int getCount() {

            return tabTitle.length;
        }

    }
}

CommonUIFragment

public class CommonUIFragment extends Fragment {

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

        View rootView = inflater.inflate(R.layout.fragment_selection_common, container, false);

        TextView tv_tabName = (TextView) rootView.findViewById(R.id.tv_tabName);

        Bundle bundle = getArguments();

        tv_tabName.setText(bundle.getString(MainActivity.ARGUMENTS_NAME, ""));

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
}

**LaunchUIFragment**

public class LaunchUIFragment extends Fragment {

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

        View rootView = inflater.inflate(R.layout.fragment_selection_launch, container, false);

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

}

布局文件

activity_main.xml

<RelativeLayout 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" >

    <RelativeLayout
        android:id="@+id/rl_tab"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#F2F2F2" >

        <HorizontalScrollView
            android:id="@+id/mHsv"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:fadingEdge="none"
            android:scrollbars="none" >

            <RelativeLayout
                android:id="@+id/rl_nav"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:background="#5AB0EB" >

                <RadioGroup
                    android:id="@+id/rg_nav_content"
                    android:layout_width="fill_parent"
                    android:layout_height="38dip"
                    android:layout_alignParentTop="true"
                    android:background="#F2F2F2"
                    android:orientation="horizontal" >
                </RadioGroup>

                <ImageView
                    android:id="@+id/iv_nav_indicator"
                    android:layout_width="1dip"
                    android:layout_height="5dip"
                    android:layout_alignParentBottom="true"
                    android:background="#0f0"
                    android:contentDescription="@string/nav_desc"
                    android:scaleType="matrix" />
            </RelativeLayout>
        </HorizontalScrollView>

        <ImageView
            android:id="@+id/iv_nav_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/nav_desc"
            android:paddingBottom="1dip"
            android:src="@drawable/iv_navagation_scroll_left"
            android:visibility="gone" >
        </ImageView>

        <ImageView
            android:id="@+id/iv_nav_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/nav_desc"
            android:paddingBottom="1dip"
            android:src="@drawable/iv_navagation_scroll_right"
            android:visibility="visible" >
        </ImageView>
    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/mViewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/rl_tab"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</RelativeLayout>

fragment的布局文件

fragment_selection_common.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" >

    <TextView 
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="@string/common_intro"
        />

    <TextView 
        android:id="@+id/tv_tabName"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:textSize="20sp"
        />

</LinearLayout>

fragment_selection_launch.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" >

    <TextView android:id="@+id/tv_intro"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="16dp"
            android:text="@string/launch_intro"/>

    <Button android:id="@+id/bt_click"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/launch_click_me"/>

</LinearLayout>

RadioGroup布局文件

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:background="#F2F2F2"
    android:button="@null"
    android:checked="true"
    android:gravity="center"
    android:text=""
    android:textColor="@drawable/rb_blue_bg"
    android:textSize="14.0dip" />

values文件下的文件内容

attrs.xml

<resources>

    <declare-styleable name="SlidingMenu">
        <attr name="mode">
            <enum name="left" value="0" />
            <enum name="right" value="1" />
        </attr>
        <attr name="viewAbove" format="reference" />
        <attr name="viewBehind" format="reference" />
        <attr name="behindOffset" format="dimension" />
        <attr name="behindWidth" format="dimension" />
        <attr name="behindScrollScale" format="float" />
        <attr name="touchModeAbove">
            <enum name="margin" value="0" />
            <enum name="fullscreen" value="1" />
            <enum name="none" value="2" />
        </attr>
        <attr name="touchModeBehind">
            <enum name="margin" value="0" />
            <enum name="fullscreen" value="1" />
            <enum name="none" value="2" />
        </attr>
        <attr name="shadowDrawable" format="reference" />
        <attr name="shadowWidth" format="dimension" />
        <attr name="fadeEnabled" format="boolean" />
        <attr name="fadeDegree" format="float" />
        <attr name="selectorEnabled" format="boolean" />
        <attr name="selectorDrawable" format="reference" />
    </declare-styleable>

</resources>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="holo_blue_bright">#ff00ddff</color>
   <color name="red">#900</color>
    <color name="green">#090</color>
    <color name="blue">#009</color>
    <color name="white">#FFF</color>
    <color name="black">#000</color>
</resources>

ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="selected_view" type="id"/>

</resources>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MonthTest1</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_home">HomeActivity</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="action_settings">Settings</string>
    <string name="nav_desc">nav_desc</string>
    <string name="launch_intro">这是第一个Fragment界面</string>
    <string name="launch_click_me">我是一个按钮</string>
    <string name="common_intro">这是CommonUIFragment界面</string>

</resources>

drawable-hdpi文件
rb_blue_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#5AB0EB" />
    <item android:state_checked="false" android:color="#000000"/>
</selector>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:844次
    • 积分:49
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:4篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论