반응형


Build.gradle

Dependencies에 compile 'com.android.support:support-v4:23.4.0'추가


1
2
3
4
5
6
7
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
}
cs



activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.example.leedowon.tool.MainActivity">
 
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/view">
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"/>
 
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"
            ></android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>
 
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        ></android.support.v4.view.ViewPager>
 
</RelativeLayout>
 
cs




toolbar_layout.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    android:id="@+id/toolBar"
    app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
    >
 
</android.support.v7.widget.Toolbar>
cs



ViewPagerAdaper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
 
import java.util.ArrayList;
 
public class ViewPagerAdapter extends FragmentPagerAdapter {
 
    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();
 
    public void addFragments(Fragment fragments, String  Titles){
        this.fragments.add(fragments);
        this.tabTitles.add(Titles);
    }
 
    public ViewPagerAdapter(FragmentManager fm){
        super(fm);
    }
 
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }
 
    @Override
    public int getCount() {
        return fragments.size();
    }
 
    //TabLayout에 보여질 Title
    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}
 
cs


MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
 
public class MainActivity extends AppCompatActivity {
 
    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter adapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar)findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragments(new Home(),"Home");
        adapter.addFragments(new Top(),"top");
 
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
 
    }
}
 
cs


MainActivity.java 에서 사용한 Fragment는 
APP > New > Fragment 에서 만든다.


반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기