What to think of and write the first issue: focus on simpler and understandable code to solve the problems encountered by Android Xiaobai
viewPager+fragment
viewPager implementation can slide left and right to switch interfaces
There are three materials: the ViewPager of the main page UI, the new Fragment, the interface you made, and the new ViewPager adapter
Upper Code:
1. Write Fragment UI first
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:id="@+id/text" android:text="Test text" /> </LinearLayout> //Then.java public class MyFrag extends Fragment { String text ; int background; //The purpose of constructing a method is to display a slightly different background color and content in the same UI public MyFrag(String text,int background) { this.text = text; this.background = background; } private TextView textView; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.myfrag,container,false); textView = view.findViewById(R.id.text); textView.setText(text); view.setBackgroundColor(background); return view; } } 2,Then write the adapter ViewPagerAdapter class MyAdapter extends FragmentPagerAdapter{ FragmentManager manager; List<Fragment> fragmentList; //These methods are generated by pressing alt+enter after inheritance. As for the list < fragment > that I write, I will use it to put several fragments later public MyAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.manager = fm; this.fragmentList = fragmentList; } @NonNull @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } } 3,Finally, the main page MainActivity.java Close out of UI: <?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:gravity="center" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPage" android:background="#0f0" android:layout_width="match_parent" android:layout_height="400sp"/> </LinearLayout> .java public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private List<Fragment> fragments = new ArrayList<>(); private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = findViewById(R.id.viewPage); fragments.add(new MyFrag("Test fragment 1", Color.BLUE)); fragments.add(new MyFrag("Test fragment 2", Color.RED)); fragments.add(new MyFrag("Test fragment 3", Color.YELLOW)); adapter = new MyAdapter(getSupportFragmentManager(),fragments); viewPager.setAdapter(adapter); } } //This is to complete the most basic switching function. As for any interface, you can design it yourself, build a fragmen t, and then put it in according to the rules //The general framework is just like this. It can't be changed without its clan. It's easy to use it moreweixin_43272727 Published 1 original article, praised 0, visited 8 Private letter follow