Skip to content
This repository was archived by the owner on Nov 5, 2020. It is now read-only.

Commit 64b9a9f

Browse files
committed
tablayout 布局
1 parent 1084fcf commit 64b9a9f

File tree

7 files changed

+177
-65
lines changed

7 files changed

+177
-65
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.zhaoqi99.snnu_android;
2+
3+
4+
import android.os.Bundle;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
11+
/**
12+
* A simple {@link Fragment} subclass.
13+
*/
14+
public class AboutFragment extends Fragment {
15+
16+
17+
public AboutFragment() {
18+
// Required empty public constructor
19+
}
20+
21+
22+
@Override
23+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
24+
Bundle savedInstanceState) {
25+
// Inflate the layout for this fragment
26+
return inflater.inflate(R.layout.fragment_about, container, false);
27+
}
28+
29+
}

app/src/main/java/io/github/zhaoqi99/snnu_android/MainActivity.java

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
import android.support.annotation.NonNull;
44
import android.support.design.widget.NavigationView;
5-
import android.support.design.widget.TabLayout;
6-
import android.support.v4.app.Fragment;
75
import android.support.v4.view.GravityCompat;
8-
import android.support.v4.view.ViewPager;
96
import android.support.v4.widget.DrawerLayout;
10-
import android.support.v7.app.ActionBar;
117
import android.support.v7.app.ActionBarDrawerToggle;
128
import android.support.v7.app.AppCompatActivity;
139
import android.os.Bundle;
@@ -18,77 +14,52 @@
1814

1915
import com.stephentuso.welcome.WelcomeHelper;
2016

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
24-
2517
public class MainActivity extends AppCompatActivity {
2618

2719
WelcomeHelper welcomeScreen;
2820
NavigationView navigationView;
2921
DrawerLayout drawerLayout;
3022
Toolbar mToolBar;
3123

32-
TabLayout tablayout;
33-
private mViewPagerFragmentAdapter mAdapter;
34-
private List<String> mTabTitleList;
35-
ViewPager mViewPager;
36-
private List<Fragment> mFragmentList;
24+
3725

3826
@Override
3927
protected void onCreate(Bundle savedInstanceState) {
4028
super.onCreate(savedInstanceState);
4129
setContentView(R.layout.activity_main);
30+
4231
welcomeScreen = new WelcomeHelper(this, MyWelcomeActivity.class);
4332
welcomeScreen.show(savedInstanceState);
33+
4434
drawerLayout = this.findViewById(R.id.drawer_layout);
4535
mToolBar = this.findViewById(R.id.mToolBar);
4636
navigationView=this.findViewById(R.id.navigation_view);
47-
tablayout=this.findViewById(R.id.tablayout);
48-
mViewPager=this.findViewById(R.id.mViewPager);
4937

5038
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
5139
mToolBar, 0, 0);
5240
drawerLayout.addDrawerListener(mDrawerToggle);
5341
mDrawerToggle.syncState(); // 添加此句,toolbar左上角显示开启侧边栏图标
54-
5542
//侧栏菜单按钮事件
5643
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
5744
@Override
5845
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
5946
switch (menuItem.getItemId()) {
6047
case R.id.navigation_item_home:
6148
Toast.makeText(MainActivity.this, "x", Toast.LENGTH_SHORT).show();
49+
switchToNews();
50+
break;
51+
case R.id.navigation_item_about:
52+
switchToAbout();
6253
break;
6354
}
6455
drawerLayout.closeDrawer(GravityCompat.START);
6556
return true;
6657
}
6758
});
68-
InitTab();
6959
setSupportActionBar(mToolBar);
60+
switchToAbout();
7061
}
7162

72-
//初始化tab
73-
public void initFragmentList(){
74-
mFragmentList = new ArrayList<Fragment>();
75-
mFragmentList.add(new Tab());
76-
mFragmentList.add(new Tab());
77-
}
78-
79-
public void initTabTitleList() {
80-
mTabTitleList = new ArrayList<String>();
81-
mTabTitleList.add("新闻");
82-
mTabTitleList.add("通知");
83-
}
84-
85-
void InitTab(){
86-
initFragmentList();
87-
initTabTitleList();
88-
mAdapter = new mViewPagerFragmentAdapter(getSupportFragmentManager(), mFragmentList,mTabTitleList);
89-
mViewPager.setAdapter(mAdapter);
90-
tablayout.setupWithViewPager(mViewPager);
91-
}
9263

9364
@Override
9465
protected void onSaveInstanceState(Bundle outState) {
@@ -120,6 +91,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
12091
return true;
12192
}
12293

94+
private void switchToAbout() {
95+
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new AboutFragment()).commit();
96+
// mToolbar.setTitle(R.string.navigation_book);
97+
}
98+
private void switchToNews() {
99+
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new NewsFragment()).commit();
100+
// mToolbar.setTitle(R.string.navigation_book);
101+
}
123102
// @Override
124103
// public void onBackPressed()
125104
// {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.zhaoqi99.snnu_android;
2+
3+
4+
import android.os.Bundle;
5+
import android.support.design.widget.TabLayout;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v4.app.FragmentActivity;
8+
import android.support.v4.app.FragmentManager;
9+
import android.support.v4.view.ViewPager;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
18+
/**
19+
* A simple {@link Fragment} subclass.
20+
*/
21+
public class NewsFragment extends Fragment {
22+
23+
TabLayout tablayout;
24+
private mViewPagerFragmentAdapter mAdapter;
25+
ViewPager mViewPager;
26+
private List<String> mTabTitleList;
27+
private List<Fragment> mFragmentList;
28+
29+
public NewsFragment() {
30+
// Required empty public constructor
31+
}
32+
33+
34+
@Override
35+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
36+
Bundle savedInstanceState) {
37+
View view= inflater.inflate(R.layout.fragment_news, container, false);
38+
tablayout=view.findViewById(R.id.tablayout);
39+
mViewPager=view.findViewById(R.id.mViewPager);
40+
41+
InitTab();
42+
return view;
43+
}
44+
//初始化tab
45+
private void InitTab(){
46+
mFragmentList = new ArrayList<Fragment>();
47+
mFragmentList.add(new Tab());
48+
mFragmentList.add(new Tab());
49+
50+
mTabTitleList = new ArrayList<String>();
51+
mTabTitleList.add("新闻");
52+
mTabTitleList.add("通知");
53+
54+
mAdapter = new mViewPagerFragmentAdapter(getActivity().getSupportFragmentManager(), mFragmentList,mTabTitleList);
55+
mViewPager.setAdapter(mAdapter);
56+
tablayout.setupWithViewPager(mViewPager);
57+
}
58+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@
77
android:layout_height="match_parent">
88

99
<LinearLayout
10+
android:id="@+id/main_content"
11+
android:orientation="vertical"
1012
android:layout_width="match_parent"
11-
android:layout_height="match_parent"
12-
android:orientation="vertical">
13+
android:layout_height="match_parent">
1314

14-
<android.support.v7.widget.Toolbar
15-
android:id="@+id/mToolBar"
15+
<android.support.design.widget.AppBarLayout
16+
android:id="@+id/appbar"
1617
android:layout_width="match_parent"
1718
android:layout_height="wrap_content"
18-
android:background="@color/colorPrimary"
19-
android:fitsSystemWindows="true"
20-
android:minHeight="?attr/actionBarSize"
21-
app:theme="@style/Widget.AppCompat.ActionBar">
22-
23-
</android.support.v7.widget.Toolbar>
24-
25-
<android.support.design.widget.TabLayout
26-
android:id="@+id/tablayout"
19+
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
20+
21+
<android.support.v7.widget.Toolbar
22+
android:id="@+id/mToolBar"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:background="@color/colorPrimary"
26+
app:layout_collapseMode="pin"
27+
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
28+
</android.support.design.widget.AppBarLayout>
29+
30+
<FrameLayout
31+
android:id="@+id/frame_content"
32+
android:layout_below="@+id/appbar"
2733
android:layout_width="match_parent"
28-
android:layout_height="wrap_content">
29-
</android.support.design.widget.TabLayout>
34+
android:layout_height="match_parent"
35+
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
3036

31-
<android.support.v4.view.ViewPager
32-
android:id="@+id/mViewPager"
33-
android:layout_width="match_parent"
34-
android:layout_height="match_parent">
35-
36-
</android.support.v4.view.ViewPager>
37+
</FrameLayout>
3738
</LinearLayout>
3839

39-
4040
<!--左侧导航菜单-->
4141

4242
<android.support.design.widget.NavigationView
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".AboutFragment">
7+
8+
<!-- TODO: Update blank fragment layout -->
9+
<TextView
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:text="@string/hello_blank_fragment" />
13+
14+
</LinearLayout>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".NewsFragment">
7+
8+
<android.support.design.widget.TabLayout
9+
android:id="@+id/tablayout"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content">
12+
13+
</android.support.design.widget.TabLayout>
14+
15+
<android.support.v4.view.ViewPager
16+
android:id="@+id/mViewPager"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent">
19+
20+
</android.support.v4.view.ViewPager>
21+
22+
</FrameLayout>
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
66
tools:context=".Tab">
77

88
<!-- TODO: Update blank fragment layout -->
9-
<android.support.v7.widget.RecyclerView
10-
android:id="@+id/recycler_view"
11-
android:layout_width="fill_parent"
12-
android:layout_height="match_parent">
13-
</android.support.v7.widget.RecyclerView>
9+
<LinearLayout
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:orientation="vertical">
1413

14+
15+
<!--<android.support.v7.widget.RecyclerView-->
16+
<!--android:id="@+id/recycler_view"-->
17+
<!--android:layout_width="match_parent"-->
18+
<!--android:layout_height="match_parent">-->
19+
<!--</android.support.v7.widget.RecyclerView>-->
20+
<TextView
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
android:text="666" />
24+
</LinearLayout>
1525
</FrameLayout>

0 commit comments

Comments
 (0)