一、创建activity(WxActivity)
二、修改WxActivity对应的布局文件activity_wx
<?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.administrator.myapplication.WxActivity">
<android.support.v4.view.ViewPager
android:id="@+id/wx_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/content_lin"></android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/content_lin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/contact_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="联系人" />
<Button
android:id="@+id/friend_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="朋友圈" />
<Button
android:id="@+id/news_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="消息" />
</LinearLayout>
</RelativeLayout>
三、创建fragment(ContactFragment,FriendFragment,NewsFragment)修改其对应的布局文件fragment_contact,fragment_news,fragment_friend
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.myapplication.fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:text="联系人" />
</FrameLayout>
四、创建MyPageAdapter适配器
public class MyPageAdapter extends FragmentPagerAdapter{
//创建fragmentList集合
private List<Fragment> fragmentList;
//构造方法实现传值
public MyPageAdapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
//返回fragmentList对象
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//返回fragmentList对象个数
@Override
public int getCount() {
return fragmentList.size();
}
}
五、修改WxActivity
public class WxActivity extends AppCompatActivity implements View.OnClickListener {
//创建button对象
private Button contactBtn;
private Button friendBtn;
private Button newsBtn;
//创建viewpager对象
private ViewPager wxVp;
//创建fragmentList集合,并进行实力化
List<Fragment> fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx);
//绑定ID
bangId();
//实例化fragment对象
ContactFragment contactFragment = new ContactFragment();
FriendFragment friendFragment = new FriendFragment();
NewsFragment newsFragment = new NewsFragment();
//将fragment对象通过add()方法放入fragmentList集合中
fragmentList.add(contactFragment);
fragmentList.add(friendFragment);
fragmentList.add(newsFragment);
//创建MyPageAdapter页面适配器
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragmentList);
//绑定适配器
wxVp.setAdapter(adapter);
}
private void bangId() {
contactBtn = findViewById(R.id.contact_btn);
friendBtn = findViewById(R.id.friend_btn);
newsBtn = findViewById(R.id.news_btn);
wxVp = findViewById(R.id.wx_vp);
contactBtn.setOnClickListener(this);
friendBtn.setOnClickListener(this);
newsBtn.setOnClickListener(this);
wxVp.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.contact_btn:
//通过setCurrentItem()方法实现点击跳转;
wxVp.setCurrentItem(0);
break;
case R.id.friend_btn:
wxVp.setCurrentItem(1);
break;
case R.id.news_btn:
wxVp.setCurrentItem(2);
break;
default:
break;
}
}
}
六、效果展现