0

我是android开发的新手。我按照教程http://slidenerd.com/2015/07/22/android-design-support-library/ 将相同的片段添加到 tablayout。但是我需要向 tablayout 添加 3 个独特的片段,这些片段从网络服务器。如何进行编程????这是代码`package com.example.naveen.helloworld;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class AppMainScreen extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTablayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_main_screen2);
    mToolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(mToolbar);
    mTablayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
    mTablayout.setupWithViewPager(mViewPager);
}

class PagerAdapter extends FragmentStatePagerAdapter {
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment =null;
        switch (position) {
            case 0:                    return new Fragment();
            case 1:                    return new Fragment();
        }
        return null;
    }

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

    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "News Feed";
            case 1:
                return "Events";
            default:
                return "News Feed";
        } }   }}`

提前谢谢。

4

1 回答 1

0

寻呼机适配器内部的getItem方法设置片段,因此只需将开关盒修改为

switch(position){
   case 0: 
     return new NewsFragment();
   case 1: 
     return new EventsFragment();
}

NewsFragment和替换EventsFragment为您需要显示的相应片段

于 2015-08-10T18:25:00.373 回答