main xml界面
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewpager"
android:layout_weight="9"
/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
main java界面
package com.example.lenovo.myprojecttwo;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.lenovo.myprojecttwo.Adapter.MypagerAdapter;
import com.example.lenovo.myprojecttwo.fragment.fragment_01;
import com.example.lenovo.myprojecttwo.fragment.fragment_02;
import com.example.lenovo.myprojecttwo.fragment.fragment_03;
import com.example.lenovo.myprojecttwo.fragment.fragment_04;
import com.example.lenovo.myprojecttwo.fragment.fragment_05;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.viewpager);
tabLayout=findViewById(R.id.tablayout);
ArrayList<Fragment> list=new ArrayList<>();
list.add(new fragment_01());
list.add(new fragment_02());
list.add(new fragment_03());
list.add(new fragment_04());
list.add(new fragment_05());
MypagerAdapter mypagerAdapter=new MypagerAdapter(getSupportFragmentManager(),list);
viewPager.setAdapter(mypagerAdapter);
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("首页");
tabLayout.getTabAt(1).setText("电影");
tabLayout.getTabAt(2).setText("影院");
tabLayout.getTabAt(3).setText("演出");
tabLayout.getTabAt(4).setText("我的");
}
}
fragment中的两个fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchview"
android:layout_weight="1">
</SearchView>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab"
/>
<android.support.v4.view.ViewPager
android:layout_weight="14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewpager2"
/>
</LinearLayout>
里面两个fragment的代码
package com.example.lenovo.myprojecttwo.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.lenovo.myprojecttwo.Adapter.MymovieAdapter;
import com.example.lenovo.myprojecttwo.R;
import java.util.ArrayList;
public class fragment_02 extends Fragment{
private ViewPager viewPager2;
private TabLayout tab;
public fragment_02() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.item_02,container,false);
viewPager2=view.findViewById(R.id.viewpager2);
tab=view.findViewById(R.id.tab);
ArrayList<Fragment> list=new ArrayList<>();
list.add(new fragment_one());
list.add(new fragment_two());
MymovieAdapter mymovieAdapter=new MymovieAdapter(getFragmentManager(),list);
viewPager2.setAdapter(mymovieAdapter);
tab.addTab(tab.newTab());
tab.addTab(tab.newTab());
tab.setupWithViewPager(viewPager2);
tab.getTabAt(0).setText("正在热播");
tab.getTabAt(1).setText("激情预告");
return view;
}
}