<?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">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/black"
android:paddingTop="4dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_weight="1"
android:checked="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rbmag1"
android:button="@null"/>
<RadioButton
android:id="@+id/rb2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rbmag2"
android:button="@null"/>
<RadioButton
android:id="@+id/rb3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rbmag3"
android:button="@null"/>
<RadioButton
android:id="@+id/rb4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rbmag4"
android:button="@null"/>
</RadioGroup>
</RelativeLayout>
package com.example.myfment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager viewPager; //viewPager布局
private RadioGroup group; //RadioGroup控件
private RadioButton[] buttons; //RadioButton控件,控件较多,写为数组形式
private List<Fragment> data; //集合,用于存储fragment布局
MyFragmentAdapter adapter; //适配器(连接fragment和ViewPager)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//声明控件
viewPager = (ViewPager) findViewById(R.id.viewPager);
group = (RadioGroup) findViewById(R.id.radiogroup);
//获取button数组
buttons = new RadioButton[group.getChildCount()];
//循环,使button按钮一一对应Group长度
for(int i = 0; i < group.getChildCount(); i++){
//对应修改
buttons[i] = (RadioButton) group.getChildAt(i);
}
//实例化集合
data = new ArrayList<>();
data.add(new A_Fragment());
data.add(new B_Fragment());
data.add(new C_Fragment());
data.add(new D_Fragment());
//设置按钮监听事件
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
//循环每一个按钮id,进行查找
for(int i = 0; i < group.getChildCount(); i++){
//判断当前按钮id是否与选中按钮id相同
if(buttons[i].getId() == checkId){
//如果相等,将当前页面修改为所对应的按钮的id(相当于修改页面,因为修改了页面的id)
viewPager.setCurrentItem(i);
}
}
}
});
//实例化适配器
adapter = new MyFragmentAdapter(getSupportFragmentManager(), data);
//设置适配器
viewPager.setAdapter(adapter);
//设置滑动监听事件
viewPager.addOnPageChangeListener(this);
}
//viewPager滑动监听事件
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//ViewPager滑动时执行的方法
@Override
public void onPageSelected(int position) {
//循环查找
buttons[position].setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@mipmap/x"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="铁拳·蔚"
android:textColor="#f7a707"
android:textSize="30sp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:id="@+id/t"
android:layout_below="@id/w"
android:layout_centerHorizontal="true"
android:layout_width="400dp"
android:layout_height="200dp"
android:src="@mipmap/a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/t"
android:text="123"
android:scrollbars="vertical"
android:fadeScrollbars="false"
/>
</RelativeLayout>
package com.example.myfment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
/**
* Created by 帅比浩宇 on 2017/12/12.
*/
public class MyFragmentAdapter extends FragmentStatePagerAdapter {
//集合
private List<Fragment> list;
//FragmentManager 碎片管理器
public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
package com.example.myfment;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by 帅比浩宇 on 2017/12/12.
*/
public class A_Fragment extends Fragment implements View.OnClickListener {
private Button buton;
public A_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
buton = ((Button) view.findViewById(R.id.t));
buton.setOnClickListener(this);
return view;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
NotificationManager nm = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder =new Notification.Builder(getActivity());
builder.setSmallIcon(R.mipmap.ic_launcher);
nm.notify(1,builder.build());
}
}
package com.example.myfment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 帅比浩宇 on 2017/12/12.
*/
public class B_Fragment extends Fragment {
public B_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
package com.example.myfment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 帅比浩宇 on 2017/12/12.
*/
public class C_Fragment extends Fragment {
public C_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_c, container, false);
}
}
package com.example.myfment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 帅比浩宇 on 2017/12/12.
*/
public class D_Fragment extends Fragment {
public D_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_d, container, false);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@mipmap/o"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暗夜杀手·薇恩"
android:textColor="#0ed7b9"
android:textSize="30sp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:id="@+id/t1"
android:layout_below="@id/e"
android:layout_centerHorizontal="true"
android:layout_width="400dp"
android:layout_height="200dp"
android:src="@mipmap/b"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="着黑暗。"
android:layout_below="@id/t1"
android:textColor="#f2ea0a"/>
</RelativeLayout>