首先是到注册清单中 添加权限 这个不用导依赖哦
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
分为 fragment包 MainActivity主方法类 Util类 (判断网络) res中 drawable 添加 图片与5个.xml
Util类:
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * Created by lenovo on 2018/3/11. */ public class Util { public static int getType(Context context){ int mState=-1;//-1代表无网络 //获取系统提供的服务,转换成连接管理类,这个类专门处理连接相关的东西 ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); //NetworkInfo封装了网络连接的信息 NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo(); //如果网络连接的信息等于空的话,代表无网络 if (activeNetworkInfo==null){ return mState; } int type = activeNetworkInfo.getType(); if (type==systemService.TYPE_WIFI){//代表现在是wifi网络 mState=1; }else if (type==systemService.TYPE_MOBILE){ mState=0;//代表现在是蜂窝网络 } return mState; } }
fragment包中的5个Fragment01 - Fragment05 类
Fragment01类:
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; public class Fragment01 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01, null); return view; } }
Fragment02类:
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; public class Fragment02 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment02, null); return view; } }
Fragment03类:
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; public class Fragment03 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment03, null); return view; } }
Fragment04类:
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; public class Fragment04 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment04, null); return view; } }
Fragment05类:
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; public class Fragment05 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment05, null); return view; } }
MainActivity主方法类:
import android.app.ProgressDialog; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ViewPager mVip; private RadioGroup mRa; List<Fragment> fragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setTitle("请求数据中"); progressDialog.getProgress(); initView(); //创建集合 fragmentList=new ArrayList<>(); //声明方法 Fragment01 fragment01 = new Fragment01(); Fragment02 fragment02 = new Fragment02(); Fragment03 fragment03 = new Fragment03(); Fragment04 fragment04 = new Fragment04(); Fragment05 fragment05 = new Fragment05(); //添加到集合中 fragmentList.add(fragment01); fragmentList.add(fragment02); fragmentList.add(fragment03); fragmentList.add(fragment04); fragmentList.add(fragment05); mVip.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }); mVip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: mRa.check(R.id.btn1); break; case 1: mRa.check(R.id.btn2); break; case 2: mRa.check(R.id.btn3); break; case 3: mRa.check(R.id.btn4); break; case 4: mRa.check(R.id.btn5); break; } } @Override public void onPageScrollStateChanged(int state) { } }); int type = Util.getType(this); if(type==-1){ Toast.makeText(this,"请检查网络",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"网络正常",Toast.LENGTH_LONG).show(); } } private void initView() { mVip = (ViewPager) findViewById(R.id.vip); mRa = (RadioGroup) findViewById(R.id.ra); mRa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.btn1: mVip.setCurrentItem(0); break; case R.id.btn2: mVip.setCurrentItem(1); break; case R.id.btn3: mVip.setCurrentItem(2); break; case R.id.btn4: mVip.setCurrentItem(3); break; case R.id.btn5: mVip.setCurrentItem(4); break; } } }); } }
res中 drawable 添加 图片与5个.xml
![](https://i-blog.csdnimg.cn/blog_migrate/6c3f2d0880eb4ab547aca07fa2fd55d9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/336c7ff18241e68a3c006b7ef9eb11a2.png)
beijin.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/f" android:state_checked="false"></item> <item android:drawable="@drawable/f1" android:state_checked="true"></item> </selector>
beijin2.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/fx" android:state_checked="false"></item> <item android:drawable="@drawable/fx1" android:state_checked="true"></item> </selector>
beijin3.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/g" android:state_checked="false"></item> <item android:drawable="@drawable/g1" android:state_checked="true"></item> </selector>
beijin4.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/s" android:state_checked="false"></item> <item android:drawable="@drawable/s1" android:state_checked="true"></item> </selector>
beijin5.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/w" android:state_checked="false"></item> <item android:drawable="@drawable/w1" android:state_checked="true"></item> </selector>
字体 点击
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000" android:state_checked="false"></item> <item android:color="@color/colorAccent" android:state_checked="true"></item> </selector>
图片都在下面的链接中:↓↓
http://note.youdao.com/noteshare?id=bb1ecd184efb02922f63b4088a46693e
布局:
主布局:
<?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.bwie.fragment_viewpage_radiogroup.MainActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/vip" android:layout_weight="9" ></android.support.v4.view.ViewPager> <RadioGroup android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/ra" android:orientation="horizontal" android:layout_weight="1" > <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn1" android:layout_weight="1" android:gravity="center" android:checked="true" android:background="@drawable/beijin4" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn2" android:layout_weight="1" android:gravity="center" android:background="@drawable/beijin" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn3" android:layout_weight="1" android:gravity="center" android:background="@drawable/beijin3" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn4" android:layout_weight="1" android:gravity="center" android:background="@drawable/beijin2" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn5" android:layout_weight="1" android:gravity="center" android:background="@drawable/beijin5" android:button="@null" /> </RadioGroup> </LinearLayout>
fragment01布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout>
fragment02布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout>
fragment03布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout>
fragment04布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout>
fragment05布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout>