Android学习之界面切换的两种效果实现(直接跳转,滑动切换)
【摘要】
直接跳转
Intent intent=new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);12
...
直接跳转
Intent intent=new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
- 1
- 2
Fragment+ViewPager,滑动切换界面
Fragment
有两个包来源,一个系统内置的android.app.Fragment
。
另一个是support-v4
,后者实用性更强,记得选后者!
别忘了在主活动对应的xml文件中加入viewpager
!!!
主活动的实现
public class MainActivity extends AppCompatActivity {
//声明存储fragment的集合
private ArrayList<Fragment> fragments;
ActivityFragment1 activityFragment1;
ActivityFragment2 activityFragment2;
private ViewPager viewPager;
FragmentManager fragmentManager;//声明fragment管理
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在主布局中根据id找到ViewPager
viewPager = (ViewPager) findViewById(R.id.viewPager);
//实例化,类是抽象的,而对象的实际存在的。
activityFragment1 = new ActivityFragment1();
activityFragment2 = new ActivityFragment2();
fragments = new ArrayList<>();
//添加fragments到集合中
fragments.add(activityFragment1);
fragments.add(activityFragment2);
fragmentManager = getSupportFragmentManager();
//为ViewPager设置适配器用于部署fragments
viewPager.setAdapter(new MyFragmentPagerAdapter(fragmentManager));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {//界面切换
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//主界面的按钮
Button button1 = findViewById(R.id.button111);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);//切换到第一个界面
}
});
Button button2 = findViewById(R.id.button222);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);//切换到第二个界面
}
});
}
//定义类MyFragmentPagerAdapter,类包括属性和方法。
//创建FragmentPagerAdapter
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
}
- 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
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
分活动的实现
public class ActivityFragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
// Button button = (Button) view.findViewById(R.id.button3);
// button.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Toast.makeText(getContext(), "123", Toast.LENGTH_SHORT).show();
// }
// });
return view;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/81395262
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)