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 ...
随机推荐
- Python3玩转儿 机器学习(1)
机器学习的基础概念 数据 著名的鸢尾花数据 https://en.wikipedia.org/wiki/lris_flower_data_set lris setossa ...
- hdu 5428
题意:一个数是这n个数的乘,找出它一个不是素数的最小因子 求出所有数的所有质因子中最小的两个,相乘就是答案.如果所有数字的质因子个数不到两个,那么就是无解. #include<iostream& ...
- POJ 2832 How Many Pairs?
Description You are given an undirected graph G with N vertices and M edges. Each edge has a length. ...
- Luogu1613 跑路
题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的空间跑路器,每秒钟 ...
- H3C系列之三层交换机系统版本升级
本文涉及到的硬件与软件交换机:H3C S3600-28TP-SItftp软件:tftpd32小软件升级的文件:S36SI_E-CMW310-R1702P44.zip 关于升级的文件说明如下: S36S ...
- IOS UITextView支持输入、复制、粘贴、剪切自定义表情
UITextView是ios的富文本编辑控件,除了文字还可以插入图片等.今天主要介绍一下UITextView对自定义表情的处理. 1.首先识别出文本中的表情文本,然后在对应的位置插入NSTextAtt ...
- IOS WebViewJavascriptBridge 使用以及原理分析
本文转自:https://www.jianshu.com/p/b8d4285395c6 概述 从两个方面来讲: js不能直接调用oc的方法 oc可以通过如下函数调用js代码 - (void)evalu ...
- 模块机制 之commonJs、node模块 、AMD、CMD
在其他高级语言中,都有模块中这个概念,比如java的类文件,PHP有include何require机制,JS一开始就没有模块这个概念,起初,js通过<script>标签引入代码的方式显得杂 ...
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- MYSQL 表左连接 ON AND 和ON WHERE 的区别
首先是针对左右连接,这里与inner join区分 在使用left join时,on and 和on where会有区别 1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条 ...