//+++++++++TwoFragment ++++++++
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.asus.mn1.DYFragment.RMFragment;
import com.bwie.asus.mn1.DYFragment.ZhengFragment;
import java.util.ArrayList;
public class TwoFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag2, container, false);
tabLayout = view.findViewById(R.id.tab);
viewPager = view.findViewById(R.id.viewPager2);
final ArrayList<Fragment> list=new ArrayList<>();
list.add(new RMFragment());
list.add(new ZhengFragment());
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
});
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("正在热播");
tabLayout.getTabAt(1).setText("即将上映");
return view;
}
}
//对应布局
<android.support.design.widget.TabLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/tab">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager2"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
</android.support.v4.view.ViewPager>
//内部内容
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.asus.mn1.R;
public class RMFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.re_item, container, false);
return view;
}
}