Frament+ViewPager
viewpage可以做简单的导航,到页面菜单,可以使用视图滑动,实现像lanucher左右滑动等等,
在布局文件中加入控件viewpager,这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载xml布局文件,他是不会显示内容的
1.frament作为activity界面的一部分组成出现。
2.可以一个activity界面同时出现对个frament,并且一个frament也可以再多个activity中使用
3.在activity运行中可以动态添加,移除,替换frament
结果如下:能够自由切换效果
这是主页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android_pagetframent.MainActivity"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Res_ViewPager">
</android.support.v4.view.ViewPager>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/res_RadioGroup"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/xiao"
android:background="@drawable/yanse"
android:paddingLeft="60dp"
android:text="微信"
android:button="@null"
android:id="@+id/res_1"
/>
<RadioButton
android:id="@+id/res_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yanse"
android:button="@null"
android:drawableTop="@drawable/xiao"
android:paddingLeft="100dp"
android:text="通讯录" />
<RadioButton
android:id="@+id/res_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yanse"
android:button="@null"
android:drawableTop="@drawable/xiao"
android:paddingLeft="90dp"
android:text="发现" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="103dp"
android:drawableTop="@drawable/xiao"
android:background="@drawable/yanse"
android:button="@null"
android:text="我的"
android:id="@+id/res_4"
/>
</RadioGroup>
</LinearLayout>
每一个按钮都是一个View视图,所以我写了四个View,在调用的时候转成Frament
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_pagetframent.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="这是发现界面"
android:background="#66000000"
/>
</LinearLayout>
ActivityJAVA类
package com.example.android_pagetframent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/14 0014.
*/
public class weixinFrament extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.weixin,null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_pagetframent.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="这是通讯录界面"
android:background="#66ff00ff"
/>
</LinearLayout>
ActivityJAVA类
package com.example.android_pagetframent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/14 0014.
*/
public class tongxunluFrament extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tongxunlu,null);
}
}
每个视图都有一个 ActivityJAVA类为了能实现切换效果,我就继承了Frament碎片,把所有的View转成碎片
加入到一个集合中去根据他所点击的来切换。
package com.example.android_pagetframent;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
private ViewPager res_viewPager;
private List<Fragment> fragmentList=new ArrayList<>();
private RadioGroup res_radioGroup;
private int[] list={R.id.res_1,R.id.res_2,R.id.res_3,R.id.res_4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res_viewPager = (ViewPager) findViewById(R.id.Res_ViewPager);
//这是我自己写的四个Frament
faxianFrament faxian=new faxianFrament();
tongxunluFrament tong=new tongxunluFrament();
weixinFrament weixain=new weixinFrament();
woFrament wo=new woFrament();
fragmentList.add(faxian);
fragmentList.add(tong);
fragmentList.add(weixain);
fragmentList.add(wo);
res_viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
res_radioGroup = (RadioGroup) findViewById(R.id.res_RadioGroup);
res_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//拿到我所点击的id,进行判断要跳转的页面
if(checkedId==list[0]){
res_viewPager.setCurrentItem(0);
}else if(checkedId==list[1]){
res_viewPager.setCurrentItem(1);
}else if(checkedId==list[2]){
res_viewPager.setCurrentItem(2);
}else if(checkedId==list[3]){
res_viewPager.setCurrentItem(3);
}
}
});
//设置默认被选中的
res_viewPager.setCurrentItem(0);
res_radioGroup.check(list[0]);
//ViewPager控件的滑动监听
res_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
//此方法是在状态改变的时候调用
@Override
public void onPageScrollStateChanged(int state) {
//state==0 代表什么都没做
//state==1 代表正在滑动
//state==2 代表滑动完毕
if (state==2){
//当他滑动的时候调用这个方法,一切换页面成功则刷新一次按钮选中的是他相对应的
界面,
res_radioGroup.check(list[res_viewPager.getCurrentItem()]);
} } }); } class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }}//res_viewPager.getCurrentItem();拿到当前页面的下标