//mian主页面
package com.example.tablatout.activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.Button;
import com.example.tablatout.R;
import com.example.tablatout.fragment.Fragmentfour;
import com.example.tablatout.fragment.Fragmentone;
import com.example.tablatout.fragment.Fragmentthree;
import com.example.tablatout.fragment.Fragmenttwo;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
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;
case R.id.navigation_fu:
viewpager.setCurrentItem(3);
return true;
}
return false;
}
};
private ViewPager viewpager;
private BottomNavigationView navigation;
private Button b1;
private Button b2;
private Button b3;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
viewpager = findViewById(R.id.viewpager);
b1 = findViewById(R.id.b1);
b2 = findViewById(R.id.b2);
b3 = findViewById(R.id.b3);
drawer = findViewById(R.id.drawer);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new Fragmentone();
case 1:
return new Fragmenttwo();
case 2:
return new Fragmentthree();
case 3:
return new Fragmentfour();
}
return null;
}
@Override
public int getCount() {
return 4;
}
});
viewpager.addOnPageChangeListener(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;
case 3:
navigation.setSelectedItemId(R.id.navigation_fu);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.b1:
drawer.closeDrawers();
break;
case R.id.b2:
drawer.closeDrawers();
break;
case R.id.b3:
drawer.closeDrawers();
break;
}
}
}
//布局
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".activity.Main2Activity"
android:orientation=“vertical”>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"></android.support.v4.view.ViewPager>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="0dp"
android:layout_weight="1"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
/>
</LinearLayout>
<LinearLayout
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"
/>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ss"
/>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ss"/>
<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ss"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>