21 ViewPager RadioGroup
- 结构 
 
MainActivity.java
package com.qf.day21_viewpagerfragmentrg_demo4;
import java.util.ArrayList;
import java.util.List;
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.text.Layout;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;
public class MainActivity extends FragmentActivity {
    private ViewPager viewPager;
    private RadioGroup rgMain;
    // 数据集合
    private List<Fragment> list = new ArrayList<Fragment>();
    private String[] titles = { "新闻", "娱乐", "军事", "体育" };
    private RadioButton[] rbs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化View
        initView();
        // 初始化ViewPager
        initViewPager();
        // 初始化导航书签
        initTab();
    }
    // 初始化导航书签
    public void initTab() {
        rbs = new RadioButton[titles.length];
        for (int i = 0; i < titles.length; i++) {
            // 动态创建RadioButton
            rbs[i] = new RadioButton(getApplicationContext());
            // rbs[i].setText(titles[i]);
            rbs[i].setGravity(Gravity.CENTER);
            BitmapDrawable a = null;
            rbs[i].setButtonDrawable(a);
            rbs[i].setBackgroundResource(R.drawable.selector_main);
            int screenWith = getResources().getDisplayMetrics().widthPixels;
            int eachWith = screenWith / titles.length;
            rbs[i].setWidth(eachWith);
            rgMain.addView(rbs[i]);
        }
        // 默认第0个元素是被选中的
        rbs[0].setChecked(true);
        rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for (int i = 0; i < titles.length; i++) {
                    if (rbs[i].getId() == checkedId) {
                        // Viewpager滑动到 点击的RadioButton上
                        viewPager.setCurrentItem(i);
                    }
                }
            }
        });
    }
    // 初始化ViewPager
    public void initViewPager() {
        // 获取数据源
        for (int i = 0; i < titles.length; i++) {
            MyFragment myFragment = MyFragment.getInstance(i + 1);
            list.add(myFragment);
        }
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                rbs[arg0].setChecked(true);
            }
            @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
            }
        });
    }
    // 初始化View
    public void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rgMain = (RadioGroup) findViewById(R.id.rg_main);
    }
    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }
        @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();
        }
    }
}
MyFragment.java
package com.qf.day21_viewpagerfragmentrg_demo4;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyFragment extends ListFragment {
    private  TextView tvShow;
    private int index =0;
    public static MyFragment getInstance(int index){
        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        myFragment.setArguments(args);
        return myFragment;
    }
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        Bundle bundle = getArguments();
        if(bundle!=null){
            index = bundle.getInt("index");
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_layout, container, false);
        tvShow = (TextView) v.findViewById(R.id.tv_show);
        return v;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        switch (index) {
        case 1:
            tvShow.setText("您点击了书签1");
            break;
        case 2:
            tvShow.setText("您点击了书签2");
            break;
        case 3:
            tvShow.setText("您点击了书签3");
            break;
        case 4:
            tvShow.setText("您点击了书签4");
            break;
        default:
            break;
        }
        SimpleAdapter adapter = new SimpleAdapter(
                getActivity(),
                loadNetWorkData(),
                R.layout.item,
                new String[]{"icon","title","content"},
                new int[]{R.id.iv_item,R.id.title_item,R.id.content_item});
        setListAdapter(adapter);
    }
    /**
     * 假设从网络获取数据
     * @return
     */
    private List<Map<String,Object>> loadNetWorkData(){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for(int i=0;i<20;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("icon", R.drawable.ic_launcher);
            map.put("title", "郭XX大战曹XXX"+i+"tab"+index);
            map.put("content", "降龙十八掌赢"+i+"tab"+index);
            list.add(map);
        }
        return list;
    }
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
    }
}
selector_main.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@android:drawable/ic_menu_add"></item>
    <item android:state_checked="false" android:drawable="@android:drawable/ic_menu_day"></item>
</selector>
activity_main.xml
<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"
    tools:context=".MainActivity" >
    <RadioGroup
        android:id="@+id/rg_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         >
    </RadioGroup>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f00"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v4.view.ViewPager>
</LinearLayout>
fragment_layout.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_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#f00"
        android:text="AAA"
        />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <ImageView
        android:id="@+id/iv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    <TextView
        android:id="@+id/title_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_item"
        android:text="name"
        />
    <TextView
        android:id="@+id/content_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_item"
        android:text="aaa"
        android:layout_alignBottom="@id/iv_item"
        />
</RelativeLayout>
21 ViewPager RadioGroup的更多相关文章
- Android底部导航栏创建——ViewPager + RadioGroup
		
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
 - 安卓TabHost+ViewPager+RadioGroup多功能模板整理
		
如今安卓比較流行的布局就是类似新闻client和手机QQ那种的底端可选择,上面的个别页面能够滑动选择. 在測试过程中发现用安卓自带的TabHost去构建.非常难得到自己定义的效果. 因此採用TabHo ...
 - ViewPager+RadioGroup实现标题栏切换,Fragment切换
		
1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...
 - 自定义ViewPager+RadioGroup联动效果的实现
		
package com.loaderman.myviewpager; import android.os.Bundle; import android.support.v7.app.AppCompat ...
 - 仿照微信的界面,即ViewPager+Fragment的结合使用
		
主布局文件: android:drawableTop="@drawable/weixin_bg"用的是状态选择器,所以要写4个状态选择器,图片的 <RelativeLayou ...
 - ViewPager+Fragment实现滑动切换页面
		
1.实现思路 主界面四个导航按钮使用RadioButton,通过Selector 设置它的drawableTop属性来设置所显示的图片.通过 FragmentPagerAdapter 实现切换. 2. ...
 - Android ViewPager+TabHost实现首页导航
		
今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就 ...
 - 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
		
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
 - Android应用主界面底部菜单实现
		
介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的 <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...
 
随机推荐
- poj 1046 ——Color Me Less
			
提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Su ...
 - bzoj 4180: 字符串计数
			
Description SD有一名神犇叫做Oxer,他觉得字符串的题目都太水了,于是便出了一道题来虐蒟蒻yts1999. 他给出了一个字符串T,字符串T中有且仅有4种字符 'A', 'B', 'C', ...
 - 习题9-8 uva1631
			
题意: 给你一串密码,每次我们可以转动1-3个数字,求转出最终答案的最小步数 思路: 感觉自己好坑,最开始想的是dp[cur][t1][t2][t3]也就是t1的位置以及连续的三个数的状态 但是卡死循 ...
 - [hdu5608]function
			
题意:$\sum_{d|n}f(d)=n^{2}-3n+2$,求$\sum_{i=1}^{n}f(i)\mod 10^{9}+7$ , $n \leqslant 10^{9}$ $\left( T \ ...
 - SpringBoot学习之自动依赖
			
在前面使用SSM集成时,我们可以使用注解实现无配置化注入,但是这种依赖被进行“人工干预了的”,换句话就是说我们手动进行装配,那么此时还没有达到SpringBoot这种自动装配的效果,那么究竟Sprin ...
 - Python【第二课】 字符串,列表,字典,集合,文件操作
			
本篇内容 字符串操作 列表,元组操作 字典操作 集合操作 文件操作 其他 1.字符串操作 1.1 字符串定义 特性:不可修改 字符串是 Python 中最常用的数据类型.我们可以使用引号('或&quo ...
 - Maven的pom.xml文件结构之基本配置packaging和多模块聚合结构(微服务)
			
1. packaging packaging给出了项目的打包类型,即作为项目的发布形式,其可能的类型.在Maven 3中,其可用的打包类型如下: jar,默认类型 war ejb ear rar pa ...
 - java如何获得数据库表中各字段的字段名
			
public class TestDemo { public static Connection getConnection() { Connection conn = null; try { Cla ...
 - mysql数据库索引类型和原理
			
索引初识: 最普通的情况,是为出现在where子句的字段建一个索引.为方便讲述,我们先建立一个如下的表. CREATE TABLE mytable ( id serial primary key, c ...
 - 90. Subsets II(中等,编写代码有难度)
			
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...