版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/juer2017/article/details/85159019
1.截图:
2.实现
首先页面布局(图片自己找哈)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="@color/colorLightWhite"/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="horizontal"
android:weightSum="3"
android:padding="5dp">
<RadioButton
android:id="@+id/rb_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/tebbar_one"
android:gravity="center"
android:text="手机壁纸"
android:textColor="@color/selector_tv_fen"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rb_system"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/tebbar_two"
android:gravity="center"
android:text="电脑壁纸"
android:textColor="@color/selector_tv_fen"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rb_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/tebbar_three"
android:gravity="center"
android:text="个人中心"
android:textColor="@color/selector_tv_fen"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
fragment_one.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
</LinearLayout>
fragment_two.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
fragment_three.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
详细布局内容自己填充哦
然后上Java代码:
activity:
package com.example.wallpaper.activity;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
public abstract class BaseActivity extends FragmentActivity {
protected BaseActivity act;
protected final String TAG = getClass().getSimpleName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
act = this;
setContentView(getLayoutID());
initView();
initData();
initListener();
// 透明状态栏
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}*/
}
@LayoutRes
protected abstract int getLayoutID();
protected abstract void initListener();
protected abstract void initView();
protected abstract void initData();
@SuppressWarnings("unchecked")
protected <E> E f(int id) {
return (E) findViewById(id);
}
}
package com.example.wallpaper.activity;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.wallpaper.R;
import com.example.wallpaper.adapter.PagerMainAdapter;
import com.example.wallpaper.fragment.FragmentOne;
import com.example.wallpaper.fragment.FragmentThree;
import com.example.wallpaper.fragment.FragmentTwo;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends BaseActivity {
private ViewPager vp;
private RadioGroup rg;
private int[] rbs = {R.id.rb_home, R.id.rb_system, R.id.rb_nav};
private List<Fragment> mFragments;
private boolean mBackKeyPressed = false;//记录是否有首次按键
//简化后的方法
@Override
protected int getLayoutID() {
return R.layout.activity_main;
}
@Override
protected void initView() {
vp = f(R.id.vp);
rg = f(R.id.rg);
}
@Override
protected void initData() {
mFragments = new ArrayList<>();
FragmentOne one = new FragmentOne();
FragmentTwo two = new FragmentTwo();
FragmentThree three = new FragmentThree();
mFragments.add(one);
mFragments.add(two);
mFragments.add(three);
// 设置填充器
vp.setAdapter(new PagerMainAdapter(getSupportFragmentManager(), mFragments));
// 设置缓存页面数
vp.setOffscreenPageLimit(2);
}
@Override
protected void initListener() {
//radioGroup的点击事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
for (int i = 0; i < rbs.length; i++) {
if (rbs[i] != checkedId) continue;
//加载滑动
vp.setCurrentItem(i);
}
}
});
//ViewPager的点击事件 vp-rg互相监听:vp
vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
rg.check(rbs[position]);
}
});
//设置一个默认页
rg.check(rbs[0]);
}
public void onBackPressed() {
if (!mBackKeyPressed) {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {//延时两秒,如果超出则清除第一次记录
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
} else {
finish();
}
}
}
adapter:
package com.example.wallpaper.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class PagerMainAdapter extends FragmentPagerAdapter {
private final List<Fragment> frags;
public PagerMainAdapter(FragmentManager fm, List<Fragment> frags) {
super(fm);
this.frags = frags;
}
@Override
public Fragment getItem(int position) {
return frags.get(position);
}
@Override
public int getCount() {
return frags.size();
}
}
最后fragment:
package com.example.wallpaper.fragment;
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;
import com.example.wallpaper.R;
public class FragmentOne extends Fragment{
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
package com.example.wallpaper.fragment;
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;
import com.example.wallpaper.R;
public class FragmentTwo extends Fragment{
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
package com.example.wallpaper.fragment;
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;
import com.example.wallpaper.R;
public class FragmentThree extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
return view;
}
}
还有coloe和drawable资源文件:
selector_tv_fen.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorRed" android:state_checked="true" />
<item android:color="@color/colorGray" />
</selector>
tebbar_one.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/one_on" android:state_checked="true" />
<item android:drawable="@drawable/one_off" android:state_checked="false" />
</selector>
tebbar_two.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/two_on" android:state_checked="true" />
<item android:drawable="@drawable/two_off" android:state_checked="false" />
</selector>
tebbar_three.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/three_on" android:state_checked="true" />
<item android:drawable="@drawable/three_off" android:state_checked="false" />
</selector>
大致目录如下: