我想使用滑动屏幕,但全屏,没有显示标签,如何在Android工作室中执行此操作?
答案 0 :(得分:0)
我不太确定你在寻找什么,但请尝试以下代码
布局/ activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
/**
* method to initialize our views
*/
public void initUI() {
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
viewPager.setOffscreenPageLimit(1);
viewPager.setCurrentItem(0);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
/**
* ViewPagerAdapter
*/
private static class ViewPagerAdapter extends FragmentPagerAdapter {
ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int num) {
Fragment frag=null;
switch (num) {
case 0:
frag = new Fragment1();
break;
case 1:
frag = new Fragment2();
break;
case 2:
frag = new Fragment3();
break;
}
return frag;
}
@Override
public int getCount() {
return 3;
}
}
}
更多参考here