import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import jiaoyibo.bawei.com.jiaoyibo20190109.base.BaseActivity;
import jiaoyibo.bawei.com.jiaoyibo20190109.fragment.FragmentOne;
import jiaoyibo.bawei.com.jiaoyibo20190109.fragment.FragmentThree;
import jiaoyibo.bawei.com.jiaoyibo20190109.fragment.FragmentTwo;
public class MainActivity extends BaseActivity implements View.OnClickListener {
private TextView mTextMessage;
//获取布局的集合
private List<Fragment> list = new ArrayList<>();
//自带的点击跳转页面
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
viewPager.setCurrentItem(2);
return true;
}
return false;
}
};
private BottomNavigationView navigation;
private ViewPager viewPager;
@Override
protected void initData() {
//添加布局
list.add(new FragmentOne());
list.add(new FragmentTwo());
list.add(new FragmentThree());
//viewPager适配
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
/**
* viewpager每次切换的时候, 会重新创建当前界面及左右界面三个界面,
* 每次切换都要重新oncreate,
* 所以只要设置viewPager setOffscreenPageLimit即可避免这个问题
*/
viewPager.setOffscreenPageLimit(list.size());
//设置viewPager监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
navigation.setSelectedItemId(R.id.navigation_home);
break;
case 1:
navigation.setSelectedItemId(R.id.navigation_dashboard);
break;
case 2:
navigation.setSelectedItemId(R.id.navigation_notifications);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
protected void initView() {
viewPager = findViewById(R.id.viewPager);
mTextMessage = (TextView) findViewById(R.id.message);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
}
ViewPager自带点击变色,图片
最新推荐文章于 2020-02-17 21:14:42 发布