Home > Mobile >  TabLayout + ViewPage + page switching implementation solutions for multiple fragments are commonly u
TabLayout + ViewPage + page switching implementation solutions for multiple fragments are commonly u

Time:10-21

Don't know the place is as follows:
 
List. The add (new Tab1Fragment ());
List. The add (new Tab2Fragment ());
List. The add (new Tab3Fragment ());

The Tab1Fragment () what is not clear, the red error, not this thing, this is my other bloggers borrowed from the Internet, so I don't know something may be a little less, other places are roughly idea meaning, it don't quite understand, lead to can not run out, solving 1 study the common progress together ,

1. This is a class, and I write the adapter also inside,
 
Public class MainActivity extends AppCompatActivity {
Private TabLayout TabLayout;
Private ViewPager ViewPager.
//write a set list, each page is fragments, save
Private List The list;
Private MyAdapter adapter.
Private String [] titles={" page 1 ", "page 2", "page 3", "page 4", "page 5", "page six"};

//==============& gt;
@ Override
Protected void onCreate (@ Nullable Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
//instantiate
ViewPager=the findViewById (R.i d.v iewPage);
TabLayout=the findViewById (R.i which abLayout);
//page, data sources, it is to create three pages (fragments)
List=new ArrayList<> (a);
List. The add (new Tab1Fragment ());
List. The add (new Tab2Fragment ());
List. The add (new Tab3Fragment ());
//viewPage adapter, to obtain the Fragment manager
Adapter=new MyAdapter (getSupportFragmentManager ());
ViewPager. SetAdapter (adapter);
//will viewPage and tabLayout binding together, a move another will follow
TabLayout. SetupWithViewPager (viewPager);
}


//===============& gt; Create fragments adapter
The class MyAdapter extends FragmentPagerAdapter {

Public MyAdapter (FragmentManager FM) {
Super (FM);
}
//for each page of the subscript
@ NonNull
@ Override
Public fragments the getItem (int position) {
Return the list. The get (position);
}
//get the size of the list
@ Override
Public int getCount () {
Return the list. The size ();
}

//rewrite this method, which will set the title of each TAB
@ Nullable
@ Override
Public CharSequence getPageTitle (int position) {
Return titles [position];
}
}
}


2. This is the layout
 
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"

TabIndicatorColor: Tab below the horizontal line color
TabTextColor: TAB of the default text color
TabSelectedTextColor: the font color for the selected TAB
TabMode: mode (Scrollable fixed).
The fixed mode needs and tabGravity properties,
The tabGravity attribute values for the fill.
TabIndicatorHeight: Settings TAB at the bottom of the horizontal level. 0 dp, don't show.
- & gt;

The android: id="@ + id/tabLayout"
Android: layout_width="match_parent"
Android: layout_height="40 dp"
App: tabSelectedTextColor="59 # d"
/>

The android: id="@ + id/viewPage"
Android: layout_width="match_parent"
Android: layout_height="match_parent"
/>

CodePudding user response:

Is the pieces you wrote (fragments), fragments is a kind of Activity can be embedded in the segments of the UI, can make programs more reasonable and make full use of large screen space, the purpose is to fit the screen of the tablet computer, it can be seen as a small Activity, also known as the Activity, using fragments can divide the screen into pieces, and then grouping, a modular management, fragments cannot be used alone, need to use nested in the Activity, Activity eventually inherit the Activity, debris eventually inherit the fragments
  • Related