-
Notifications
You must be signed in to change notification settings - Fork 503
/
MultiFragmentWithViewPagerActivity.java
70 lines (50 loc) · 1.75 KB
/
MultiFragmentWithViewPagerActivity.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package sample.kingja.loadsir.target;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import sample.kingja.loadsir.R;
/**
* Description:TODO
* Create Time:2017/9/4 10:56
* Author:KingJA
* Email:kingjavip@gmail.com
*/
public class MultiFragmentWithViewPagerActivity extends AppCompatActivity {
private List<Fragment>fragments=new ArrayList<>();
private String[]tabTitles={"Fragment A","Fragment B"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_viewpager);
ViewPager viewpager = findViewById(R.id.viewpager);
TabLayout tablayout = findViewById(R.id.tablayout);
fragments.add(new FragmentA());
fragments.add(new FragmentB());
viewpager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
tablayout.setupWithViewPager(viewpager);
tablayout.setTabMode(TabLayout.MODE_FIXED);
}
private class PagerAdapter extends FragmentPagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
}