我有一个包含Tablayout和viewpager的XML布局。我在片段中附加了带tablayout的viewpager,我希望在点击标签和交换时更改片段。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#262626"
app:tabIndicatorColor="#FFF6EEF1"
app:tabSelectedTextColor="#FFF9EFC6"
app:tabTextColor="#FFF6EEF1"
app:tabGravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>
Java文件是:
package com.example.eventor;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.eventor.tabfragments.CalanderFragment;
import com.example.eventor.tabfragments.HomeFragments;
import com.example.eventor.tabfragments.SearchFragment;
import com.example.eventor.tabfragments.UserFragment;
public class TabFragment extends Fragment implements ActionBar.TabListener {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4;
private int[] tabIcons = new int[]{
R.drawable.ic_home_outline_white_48dp,
R.drawable.ic_date_range_black_48dp,
R.drawable.ic_search_black_48dp,
R.drawable.ic_person_outline_black_48dp
};
FragmentAdapter fr = new FragmentAdapter(getFragmentManager());
public TabFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tablayout,container,false);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.pager);
tabLayout.setupWithViewPager(viewPager);
viewPager.setAdapter(new FragmentAdapter(getChildFragmentManager()));
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
return x;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition(),true);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public class FragmentAdapter extends FragmentPagerAdapter {
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "home";
case 1:
return "calander";
case 2:
return "search";
case 3:
return "profile";
}
return null;
}
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HomeFragments();
case 1:
return new CalanderFragment();
case 2:
return new SearchFragment();
case 3:
return new UserFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
}
}
单击选项卡时,它不会显示相应的片段。但是交换片段很好。 感谢
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个:在你的xml中放置viewpager
下面的tablayout
。 android:layout_below="@+id/tabs"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tabs"></android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#262626"
app:tabGravity="center"
app:tabIndicatorColor="#FFF6EEF1"
app:tabSelectedTextColor="#FFF9EFC6"
app:tabTextColor="#FFF6EEF1" />
</RelativeLayout>
代码中的:在ActionBar.TabListener
onCreate()
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
@Override
public void onTabReselected(TabLayout.Tab tab)
{
}
});