使用FragmentStatePagerAdapter android在SlidingTabLayout viewpager中同时调用两个片段的问题

时间:2016-03-08 11:12:21

标签: android android-fragments android-viewpager fragmentstatepageradapter

朋友您好我有SlidingTabLayout viewpager,所以下面是我的代码

main.xml中

<slider.SlidingTabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_below="@+id/tabs"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                </android.support.v4.view.ViewPager>

Main.java

public class MainActivity extends AppCompatActivity {

// Declaring Your View and Variables
private Toolbar toolbar;
private ViewPager pager;
private ViewPagerAdapter adapter;
private SlidingTabLayout tabs;
private CharSequence Titles[] = {"Calls", "Chats", "Contacts" };
int Numboftabs = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles
    // fot the Tabs and Number Of Tabs.
    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,
            Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);
    tabs.setViewPager(pager);
}

}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

CharSequence Titles[];
int NumbOfTabs;

public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[],
                        int mNumbOfTabsumb) {
    super(fm);
    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;
}

// This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new Call();

        case 1:
            return new Chat();
        case 2:
            return new Contact();

    }
    return null;
}

// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
    return NumbOfTabs;
}

}

Call.java

  public class Call extends Fragment {


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

    return v;
}

}

Chat.java

 public class Chat extends Fragment {


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

    return v;
   }

}

Contact.java

  public class Contact extends Fragment {


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

    return v;
}

}

当我在代码上面运行时,最初有两个片段调用,如 Call.java Chat.java ,当我从左向右滚动并移至 Chat.java 那时 Contact.java 会在后台调用,但我希望在屏幕上显示时调用每个片段所以我知道如何解决这个问题?< / p>

0 个答案:

没有答案