android使用ViewPager实现底部菜单栏和左右滑动效果,加载多个Activity
2015-07-14 10:10
1046 查看
一.首先是布局文件:
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
<RadioGroup
android:id="@+id/tab_radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio1"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:checked="true"
android:drawableTop="@drawable/ic_launcher"
android:text="页面1" />
<RadioButton
android:id="@+id/radio2"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面2" />
<RadioButton
android:id="@+id/radio3"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面3" />
<RadioButton
android:id="@+id/radio4"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面4" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
子页面布局文件(数量根据需求来定);
tab1
<?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:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面一"
android:textSize="72dp"/>
</LinearLayout>
tab2
<?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:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面二"
android:textSize="72dp"/>
</LinearLayout>
二、代码文件:
public class MainActivity extends Activity {
// 页卡内容
private ViewPager mPager;
// Tab页面列表
private List<View> listViews;
private Intent tab1Intent;
private Intent tab2Intent;
private Intent tab3Intent;
private Intent tab4Intent;
LocalActivityManager groupActivity;
RadioButton radio1, radio2, radio3, radio4;
RadioButton[] radios;
RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
groupActivity = new LocalActivityManager(this, true);
groupActivity.dispatchCreate(savedInstanceState);
mPager = (ViewPager) findViewById(R.id.vPager);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radiogroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
RadioButton[] radioButtons = { radio1, radio2, radio3, radio4 };
radios = radioButtons;
tab1Intent = new Intent(this, Tab1Activity.class);
tab2Intent = new Intent(this, Tab2Activity.class);
tab3Intent = new Intent(this, Tab3Activity.class);
tab4Intent = new Intent(this, Tab4Activity.class);
listViews = new ArrayList<View>();
listViews.add(getView("1", tab1Intent));
listViews.add(getView("2", tab2Intent));
listViews.add(getView("3", tab3Intent));
listViews.add(getView("4", tab4Intent));
//获取子页面上的控件
//View v1=listViews.get(0);
//v1.findViewById(R.id.**);
MyPagerAdapter mpAdapter = new MyPagerAdapter(listViews);
mPager.setAdapter(mpAdapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
radio1.setChecked(true);
radiochange(radios);
break;
case 1:
radio2.setChecked(true);
radiochange(radios);
break;
case 2:
radio3.setChecked(true);
radiochange(radios);
break;
case 3:
radio4.setChecked(true);
radiochange(radios);
break;
}
}
@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
}
});
radiochange(radios);
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch (arg1) {
case R.id.radio1:
radiochange(radios);
mPager.setCurrentItem(0);
break;
case R.id.radio2:
radiochange(radios);
mPager.setCurrentItem(1);
a2d5
break;
case R.id.radio3:
radiochange(radios);
mPager.setCurrentItem(2);
break;
case R.id.radio4:
radiochange(radios);
mPager.setCurrentItem(3);
break;
}
}
});
}
private View getView(String id, Intent intent) {
return groupActivity.startActivity(id, intent).getDecorView();
}
public void radiochange(RadioButton[] radioButtons) {
for (RadioButton radioButton : radioButtons) {
if (radioButton.isChecked()) {
radioButton
.setTextColor(getResources().getColor(R.color.ablue));
} else {
radioButton.setTextColor(Color.BLACK);
}
}
}
}
//子窗体代码(根据需求数量而定)
public class Tab1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
}
}
源码:http://download.csdn.net/detail/yangfan106/8897269
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
<RadioGroup
android:id="@+id/tab_radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio1"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:checked="true"
android:drawableTop="@drawable/ic_launcher"
android:text="页面1" />
<RadioButton
android:id="@+id/radio2"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面2" />
<RadioButton
android:id="@+id/radio3"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面3" />
<RadioButton
android:id="@+id/radio4"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面4" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
子页面布局文件(数量根据需求来定);
tab1
<?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:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面一"
android:textSize="72dp"/>
</LinearLayout>
tab2
<?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:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面二"
android:textSize="72dp"/>
</LinearLayout>
二、代码文件:
public class MainActivity extends Activity {
// 页卡内容
private ViewPager mPager;
// Tab页面列表
private List<View> listViews;
private Intent tab1Intent;
private Intent tab2Intent;
private Intent tab3Intent;
private Intent tab4Intent;
LocalActivityManager groupActivity;
RadioButton radio1, radio2, radio3, radio4;
RadioButton[] radios;
RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
groupActivity = new LocalActivityManager(this, true);
groupActivity.dispatchCreate(savedInstanceState);
mPager = (ViewPager) findViewById(R.id.vPager);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radiogroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
RadioButton[] radioButtons = { radio1, radio2, radio3, radio4 };
radios = radioButtons;
tab1Intent = new Intent(this, Tab1Activity.class);
tab2Intent = new Intent(this, Tab2Activity.class);
tab3Intent = new Intent(this, Tab3Activity.class);
tab4Intent = new Intent(this, Tab4Activity.class);
listViews = new ArrayList<View>();
listViews.add(getView("1", tab1Intent));
listViews.add(getView("2", tab2Intent));
listViews.add(getView("3", tab3Intent));
listViews.add(getView("4", tab4Intent));
//获取子页面上的控件
//View v1=listViews.get(0);
//v1.findViewById(R.id.**);
MyPagerAdapter mpAdapter = new MyPagerAdapter(listViews);
mPager.setAdapter(mpAdapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
radio1.setChecked(true);
radiochange(radios);
break;
case 1:
radio2.setChecked(true);
radiochange(radios);
break;
case 2:
radio3.setChecked(true);
radiochange(radios);
break;
case 3:
radio4.setChecked(true);
radiochange(radios);
break;
}
}
@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
}
});
radiochange(radios);
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch (arg1) {
case R.id.radio1:
radiochange(radios);
mPager.setCurrentItem(0);
break;
case R.id.radio2:
radiochange(radios);
mPager.setCurrentItem(1);
a2d5
break;
case R.id.radio3:
radiochange(radios);
mPager.setCurrentItem(2);
break;
case R.id.radio4:
radiochange(radios);
mPager.setCurrentItem(3);
break;
}
}
});
}
private View getView(String id, Intent intent) {
return groupActivity.startActivity(id, intent).getDecorView();
}
public void radiochange(RadioButton[] radioButtons) {
for (RadioButton radioButton : radioButtons) {
if (radioButton.isChecked()) {
radioButton
.setTextColor(getResources().getColor(R.color.ablue));
} else {
radioButton.setTextColor(Color.BLACK);
}
}
}
}
//子窗体代码(根据需求数量而定)
public class Tab1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
}
}
源码:http://download.csdn.net/detail/yangfan106/8897269
相关文章推荐
- Android布局优化策略
- Android -- 解决Android Studio 和 Android SDK Manager 无法在线更新的问题
- android判断一个功能是否可用
- android 滑动导航效果FragmentTabViewPager
- Android Studio使用技巧系列教程(四)
- Android版xx助手之天天酷跑外挂详细分析
- Android-无法隐藏标题栏
- ANDROID AIDL(安桌接口定义语言)
- Android中的线程与进程之间的关系简单解释
- ViewPager禁止滑动以及它与内层滑动控件水平方向上事件冲突的解决方法
- 自定义类似RadioButton的单选、多选的ListView
- android:自定义组合控件Weight(高仿猫眼底部菜单栏)
- android的DialogFragment设置全屏之后,里面的View不能充满整个屏幕
- Android R文件报错问题
- Android 国内集成使用谷歌地图
- Android Studio for windows环境搭建
- Android Studio 问题 - Clear Read-Only Status
- 完美解决Android SDK Manager无法更新
- Android 开发 之 Fragment 详解
- 解决Android Studio中某一个Module不能启动的问题