ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Adapt my code to your library #73

Closed AlexandruDev closed 9 years ago

AlexandruDev commented 9 years ago
@Override
      public Fragment getItem(int position) {
            switch (position % 5) {
                case 0:
                    Intent myIntent = getActivity().getIntent();
                    Bundle bundle = myIntent.getExtras();
                    myIntent.putExtra("id", position);
                    Frag1 frag1 = new Frag1().newInstance();
                    frag1.setArguments(bundle);
                    return frag1;
                case 1:
                    Intent myIntent1 = getActivity().getIntent();
                    Bundle bundle1 = myIntent1.getExtras();
                    myIntent1.putExtra("id", position);
                    Frag2 frag2 = new Frag2().newInstance();
                    frag2.setArguments(bundle1);
                    return frag2;
                    ...

How can I adapt the above code so it will work with your library?

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getFragmentManager(), FragmentPagerItems.with(getActivity())
            .add("First tab", Frag1.class) 
            .add("Second tab", Frag2.class)
            .create());

Thanks a lot for this library!

ogaclejapan commented 9 years ago

Hi, @MasterzYuri

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getFragmentManager(), FragmentPagerItems.with(getActivity())
            .add("First tab", Frag1.class) 
            .add("Second tab", Frag2.class)
            .create()) {
  @Override
  public Fragment getItem(int position) {
    return getPagerItem(position % 5).instantiate(context, position);
  }
};

If you want to get a position in the Fragment side, you can get in this code.

class Frag1 {

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    int position = FragmentPagerItem.getPosition(getArguments());
    ...
  }

}

Solved?

AlexandruDev commented 9 years ago

Kind of... I'm having another issue and I have no ideea what's causing it. I recorded it so you can understand better what's happening: https://vid.me/NZQ4

So, my app is based on Navigation Drawer, it consists of a few fragments, one of the fragments has a ViewPager ( as you can see in the video ) with 3 tabs. The first time I click on that fragments it works perfectly, but if I press on it again the background will change to grey, it also affects the scrolling.

public class MainPlusViewPager extends Fragment {

public static MainPlusViewPager newInstance() {
    return new MainPlusViewPager();
}
public MainPlusViewPager() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main_plus_viewpager,
            container, false);

    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getFragmentManager(), FragmentPagerItems.with(getActivity())
            .add("RED", F1.class)
            .add("BLUE", F2.class)
            .add("GREEN", F3.class)
            .create());

    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_plus);
    viewPager.setAdapter(adapter);

    SmartTabLayout viewPagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab_plus);
    viewPagerTab.setViewPager(viewPager);

    return view;
}

}
AlexandruDev commented 9 years ago

Issue solved, changed extends Fragment to extends AppCompatActivity

Thanks again for this library!

ogaclejapan commented 9 years ago

:+1: