2016-12-20 103 views
0

片段示出了在viewpager空屏幕添加片段viewpager是空

已經使用這個庫,用於將突片gihub

簡稱該溶液添加片段adding fragments to viewpager

這裏是代碼

public class HorizontalNtbActivity extends android.support.v4.app.FragmentActivity { 


@Override 

protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_horizontal_ntb); 


    initUI(); 
} 

private void initUI() { 

    CreateFragment createFragment = new CreateFragment(); 
    GroupFragment groupFragment= new GroupFragment(); 
    HomeFragment homeFragment= new HomeFragment(); 
    ProfileFragment profileFragment = new ProfileFragment(); 
    DashPagerAdapter dashPagerAdapter = new DashPagerAdapter(getSupportFragmentManager(),4); 
    dashPagerAdapter.addFragment(homeFragment); 
    dashPagerAdapter.addFragment(createFragment); 
    dashPagerAdapter.addFragment(profileFragment); 
    dashPagerAdapter.addFragment(groupFragment); 
    final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb); 
    viewPager.setAdapter(dashPagerAdapter); 


    final String[] colors = getResources().getStringArray(R.array.tabcolors); 

    final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal); 

    final ArrayList<NavigationTabBar.Model> models = new ArrayList<>(); 
    models.add(
      new NavigationTabBar.Model.Builder(

        getResources().getDrawable(R.drawable.ic_first), 
        Color.parseColor(colors[0])) 
        .selectedIcon(getResources().getDrawable(R.drawable.ic_sixth)) 
        .title("Home") 
        .badgeTitle("NTB") 
        .build() 
    ); 
    models.add(
      new NavigationTabBar.Model.Builder(
        getResources().getDrawable(R.drawable.ic_second), 
        Color.parseColor(colors[1])) 
        .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth)) 
        .title("Groups") 
        .badgeTitle("with") 
        .build() 
    ); 
    models.add(
      new NavigationTabBar.Model.Builder(
        getResources().getDrawable(R.drawable.ic_third), 
        Color.parseColor(colors[2])) 
        .selectedIcon(getResources().getDrawable(R.drawable.ic_seventh)) 
        .title("Create") 
        .badgeTitle("state") 
        .build() 
    ); 
    models.add(
      new NavigationTabBar.Model.Builder(
        getResources().getDrawable(R.drawable.ic_fourth), 
        Color.parseColor(colors[3])) 
        .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth)) 
        .title("Profile") 
        .badgeTitle("icon") 
        .build() 
    ); 
    navigationTabBar.setBgColor(Color.parseColor("#424242")); 
    navigationTabBar.setActiveColor(Color.parseColor("#ffffff")); 

    navigationTabBar.setInactiveColor(Color.parseColor("#9E9E9E")); 
    navigationTabBar.setModels(models); 
    navigationTabBar.setViewPager(viewPager, 0); 
    navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(final int position) { 
      navigationTabBar.getModels().get(position).hideBadge(); 

      Toast.makeText(HorizontalNtbActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     public void onPageScrollStateChanged(final int state) { 

     } 
    }); 

    navigationTabBar.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      for (int i = 0; i < navigationTabBar.getModels().size(); i++) { 
       final NavigationTabBar.Model model = navigationTabBar.getModels().get(i); 
       navigationTabBar.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         model.hideBadge(); 
        } 
       }, i * 100); 
      } 
     } 
    }, 500); 
}} 

這是適配器

public class DashPagerAdapter extends FragmentPagerAdapter { 
public final List<Fragment> fragments = new ArrayList<>(); 

public DashPagerAdapter(FragmentManager fm, int NumOfTabs) { 

    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
} 

int mNumOfTabs; 

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
     case 0: { 
      return HomeFragment.newInstance("sdfsdfsdfsdf"); 
     } 
     case 1: { 
      return CreateFragment.newInstance("adfsdfsfd"); 
     } 

     case 2: { 
      return GroupFragment.newInstance("adfsdfsfd"); 
     } 
     case 3: { 
      return ProfileFragment.newInstance("abcd"); 

     } 
     default: 
      return null; 

    } 
} 

public void addFragment(Fragment fragment) { 
    fragments.add(fragment); 
    notifyDataSetChanged(); 
} 

@Override 
public boolean isViewFromObject(final View view, final Object object) { 
    return view.equals(object); 
} 

@Override 
public int getCount() { 
    return 4; 
} 
} 

這些是片段。所有其他片段相似

public class HomeFragment extends Fragment { 

public HomeFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_home, container, false); 
    TextView textView = (TextView) view.findViewById(R.id.homefragmenttextview); 
    textView.setText(getArguments().getString("msg")); 
    // Inflate the layout for this fragment 
    return view; 
} 

public static HomeFragment newInstance(String text) { 

    Bundle args = new Bundle(); 
    args.putString("msg", text); 

    HomeFragment fragment = new HomeFragment(); 
    fragment.setArguments(args); 
    return fragment; 
} 
} 

這裏是如何看起來 image for reference

回答

0

在適配器中,更改此

@Override 
public boolean isViewFromObject(final View view, final Object object) 
{ 
    return view.equals(object); 
} 

對此

@Override 
public boolean isViewFromObject(final View view, final Object object) 
{ 
    return view == object; 
} 
+0

這不利於。 –