今天向大家介绍一个项目中基本都会用到的JPTabBar,只用简单的几句代码就实现啦以前复杂的底部导航,地址https://github.com/peng8350/JPTabBar,先看一下github上给出的实现出来的效果:
好啦 ,下面下面自己来一个简单的demo来实现,先看一下出来的效果:
第一步:
在自己工程的App下的build.gradle下添加:
repositories {
jcenter()
}
dependencies{
compile 'com.jpeng:JPTabBar:1.1.2'
}
在自己的主页面去布局文件中:
@Titles
private static final String[] mTitles = {"页面一","页面二","页面三","页面四"};
@SeleIcons
private static final int[] mSeleIcons = {R.mipmap.tab1_selected,R.mipmap.tab2_selected,R.mipmap.tab3_selected,R.mipmap.tab4_selected};
@NorIcons
private static final int[] mNormalIcons = {R.mipmap.tab1_normal, R.mipmap.tab2_normal, R.mipmap.tab3_normal, R.mipmap.tab4_normal};
activity_main.xml:
package com.example.administrator.jptabbar;
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.view.View;
import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.animate.Animatable;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
public class MainActivity extends AppCompatActivity {
@Titles
private static final String[] mTitles = {"页面一","页面二","页面三","页面四"};
@SeleIcons
private static final int[] mSeleIcons = {R.mipmap.tab1_selected,R.mipmap.tab2_selected,R.mipmap.tab3_selected,R.mipmap.tab4_selected};
@NorIcons
private static final int[] mNormalIcons = {R.mipmap.tab1_normal, R.mipmap.tab2_normal, R.mipmap.tab3_normal, R.mipmap.tab4_normal};
private JPTabBar tabbar;
private ViewPager vp_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabbar = (JPTabBar) findViewById(R.id.tabbar);
vp_main = (ViewPager) findViewById(R.id.vp_main);
tabbar.setContainer(vp_main);
//显示圆点模式的徽章
//设置容器
tabbar.ShowBadge(0,999);
tabbar.setDismissListener(new BadgeDismissListener() {
@Override
public void onDismiss(int position) {
}
});
tabbar.setTabListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int index) {
}
@Override
public void onClickMiddle(View middleBtn) {
}
});
initView();
}
private void initView() {
vp_main.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return JPFragment.newInstance(position+"");
}
@Override
public int getCount() {
return 4;
}
});
}
}
fragment_layout.xml:
package com.example.administrator.jptabbar;
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.view.View;
import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.animate.Animatable;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
public class MainActivity extends AppCompatActivity {
@Titles
private static final String[] mTitles = {"页面一","页面二","页面三","页面四"};
@SeleIcons
private static final int[] mSeleIcons = {R.mipmap.tab1_selected,R.mipmap.tab2_selected,R.mipmap.tab3_selected,R.mipmap.tab4_selected};
@NorIcons
private static final int[] mNormalIcons = {R.mipmap.tab1_normal, R.mipmap.tab2_normal, R.mipmap.tab3_normal, R.mipmap.tab4_normal};
private JPTabBar tabbar;
private ViewPager vp_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabbar = (JPTabBar) findViewById(R.id.tabbar);
vp_main = (ViewPager) findViewById(R.id.vp_main);
tabbar.setContainer(vp_main);
//显示圆点模式的徽章
//设置容器
tabbar.ShowBadge(0,999);
tabbar.setDismissListener(new BadgeDismissListener() {
@Override
public void onDismiss(int position) {
}
});
tabbar.setTabListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int index) {
}
@Override
public void onClickMiddle(View middleBtn) {
}
});
initView();
}
private void initView() {
vp_main.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return JPFragment.newInstance(position+"");
}
@Override
public int getCount() {
return 4;
}
});
}
}
package com.example.administrator.jptabbar;
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.view.View;
import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.animate.Animatable;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
public class MainActivity extends AppCompatActivity {
@Titles
private static final String[] mTitles = {"页面一","页面二","页面三","页面四"};
@SeleIcons
private static final int[] mSeleIcons = {R.mipmap.tab1_selected,R.mipmap.tab2_selected,R.mipmap.tab3_selected,R.mipmap.tab4_selected};
@NorIcons
private static final int[] mNormalIcons = {R.mipmap.tab1_normal, R.mipmap.tab2_normal, R.mipmap.tab3_normal, R.mipmap.tab4_normal};
private JPTabBar tabbar;
private ViewPager vp_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabbar = (JPTabBar) findViewById(R.id.tabbar);
vp_main = (ViewPager) findViewById(R.id.vp_main);
tabbar.setContainer(vp_main);
//显示圆点模式的徽章
//设置容器
tabbar.ShowBadge(0,999);
tabbar.setDismissListener(new BadgeDismissListener() {
@Override
public void onDismiss(int position) {
}
});
tabbar.setTabListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int index) {
}
@Override
public void onClickMiddle(View middleBtn) {
}
});
initView();
}
private void initView() {
vp_main.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return JPFragment.newInstance(position+"");
}
@Override
public int getCount() {
return 4;
}
});
}
}