ViewPager+Fragment简单应用
利用ViewPager+Fragment实现多页面
- 创建四个Fragment
在每个布局中载入对应layout
其中的一个代码,其他类似
public class IndexFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_index,container,false);
return view;
}
}
- 在activity_main.xml中添加ViewPager,四个TextView
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#009688">
<TextView
android:id="@+id/tv_index"
android: