//依赖
compile 'com.zhy:base-adapter:2.0.0'
compile 'com.android.support:recyclerview-v7:26.+'
compile 'com.android.support:design:26.+'
compile 'com.android.support:percent:26.+'
//Main代码
public class MainActivity extends AppCompatActivity {
private List<String> datas = new ArrayList<String>();
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
TextView drabutton = (TextView) findViewById(R.id.drabutton);
drabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
//横向滑动的集合
datas.add("推荐");
datas.add("要闻");
datas.add("娱乐");
datas.add("科技");
//适配器
vpsp vpsp = new vpsp(getSupportFragmentManager());
viewPager.setAdapter(vpsp);
//进行关联
tabLayout.setupWithViewPager(viewPager);
}
class vpsp extends FragmentPagerAdapter {
//有参数的构造
public vpsp(FragmentManager fm) {
super(fm);
}
//返回选项卡的文本 ,,,添加选项卡
@Override
public CharSequence getPageTitle(int position) {
return datas.get(position);
}
//创建fragment对象并返回
@Override
public Fragment getItem(int position) {
content content = new content();
Bundle bundle = new Bundle();
bundle.putString("name",datas.get(position));
content.setArguments(bundle);
return content; }
//返回数量
@Override
public int getCount() {
return datas.size();
}
}
}
<android.support.design.widget.CoordinatorLayout 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.example.sdfgh.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?attr/colorPrimary"
android:scaleType="fitXY"
android:src="@drawable/imgback"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabIndicatorColor="#a05"
app:tabIndicatorHeight="4dp"
app:tabSelectedTextColor="#000"
app:tabTextColor="#fff9"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ffffff"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/drabutton"
android:text="点击"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
// content类frament
public class content extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = View.inflate(getActivity(), R.layout.contentlayout, null);
TextView cte = inflate.findViewById(R.id.cte);
Bundle arguments = getArguments();
String name = arguments.getString("name");
Log.e("chen", "onCreateView: ------"+name );
cte.setText(name);
return inflate;
}
}
// contentlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dfxcvnxcv"
android:textColor="@color/colorAccent"/>
</LinearLayout>
//Main2代码
public class Main2Activity extends AppCompatActivity {
private Toolbar mToolbar;
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mToolbar = (Toolbar) findViewById(R.id.id_toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_navigation);
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
//<activity android:name=".NavigationActivity" android:theme="@style/AppThemeNoActionBar"></activity>
//<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
//初始化toolbar,这里得使用NoActionBar的主题,使用ToolBar替换系统自带的ActionBar达到自己的需求
// setSupportActionBar(mToolbar);
ActionBar actionBar = getSupportActionBar();
//关联图标和侧滑栏图标
actionBar.setHomeAsUpIndicator(R.drawable.menu);
//设置actionBar和侧滑栏关联
actionBar.setDisplayHomeAsUpEnabled(true);
//初始化drawerlayout和navigationview
if (mNavigationView != null) {
//设置监听回调
mNavigationView.setNavigationItemSelectedListener(new NavigationView
.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//根据选中不同的选项来进行不同的操作
switch (item.getItemId()) {
case R.id.nav_home:
mToolbar.setTitle("主页");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("主页")).commit();
// mToolbar.setTitle("主页");
break;
case R.id.nav_friends:
mToolbar.setTitle("我的好朋友");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("我的好友")).commit();
// mToolbar.setTitle("我的好友");
break;
case R.id.nav_discussion:
mToolbar.setTitle("热门");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("热文论坛")).commit();
// mToolbar.setTitle("热文论坛");
break;
case R.id.nav_messages:
mToolbar.setTitle("我的消息");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("我的消息")).commit();
// mToolbar.setTitle("我的消息");
break;
case R.id.sub1:
mToolbar.setTitle("我是子项");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("子项1")).commit();
// mToolbar.setTitle("子项1");
break;
case R.id.sub2:
mToolbar.setTitle("哈哈");
// getSupportFragmentManager().beginTransaction().replace(R.id.content,
// ItemFragement.newInstance("子项2")).commit();
// mToolbar.setTitle("子项2");
break;
}
//设置选项选中效果
item.setChecked(true);
//选了侧边栏选项之后,关闭侧边栏
mDrawerLayout.closeDrawers();
//这里返回true有选中的效果,源码中有解释
return true;
}
});
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//点击左上角的菜单选项,这是在actionBar.setHomeAsUpIndicator(R.drawable.center_image_collection);
// 这儿设置的。
case android.R.id.home:
//点击之后打开侧滑栏
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_navigation"
tools:context="com.example.sdfgh.Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="陈旭"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:theme="@style/NavigationViewStyle"
app:itemTextAppearance="@style/NavigationViewStyle"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header"
app:layout_widthPercent="85%"
app:menu="@menu/draw_view">
</android.support.design.widget.NavigationView>
</android.support.percent.PercentRelativeLayout>
</android.support.v4.widget.DrawerLayout>
//nav_header布局文件
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_navigation"
tools:context="com.example.sdfgh.Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="陈旭"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:theme="@style/NavigationViewStyle"
app:itemTextAppearance="@style/NavigationViewStyle"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header"
app:layout_widthPercent="85%"
app:menu="@menu/draw_view">
</android.support.design.widget.NavigationView>
</android.support.percent.PercentRelativeLayout>
</android.support.v4.widget.DrawerLayout>
//draw_view 这个是menu的布局,不会创建的话上篇博客有
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:title="了解会员特权"/>
<item
android:id="@+id/nav_messages"
android:title="QQ钱包"/>
<item
android:id="@+id/nav_friends"
android:title="个性装扮"/>
<item
android:id="@+id/nav_discussion"
android:title="我的收藏"/>
</group>
<item android:title="实用工具">
<menu>
<item
android:id="@+id/sub1"
android:title="我的相册"
/>
<item
android:id="@+id/sub2"
android:title="我的文件"
/>
</menu>
</item>
</menu>