我的标签活动未在标签上显示标题

时间:2016-04-30 12:04:56

标签: java android android-fragments android-tabbed-activity

这是我的主要活动

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    TabLayout tablayout;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mViewPager = (ViewPager) findViewById(R.id.container);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.

        mSectionsPagerAdapter.addFragments(new UanNumbersFragment(),"UAN");
        mSectionsPagerAdapter.addFragments(new BlankFragment(),"Emergency");

        mViewPager.setAdapter(mSectionsPagerAdapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> tabtitles= new ArrayList<>();

        public void addFragments (Fragment fragments,String tabtitles)
        {
            this.fragments.add(fragments);
            this.tabtitles.add(tabtitles);
        }

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabtitles.get(position);
        }
    }
}

这是我要在标签中显示的Fragment

public class UanNumbersFragment extends Fragment {

    public UanNumbersFragment() {}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

这是我的另一个Fragment第二个标签

public class BlankFragment extends Fragment {

    public BlankFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank2, container, false);
    }
}

2 个答案:

答案 0 :(得分:0)

使用PagerSlidingTabStrip库来解决这个问题我也遇到了这个问题,现在它正在用于标签和移动设备。

答案 1 :(得分:0)

然后你可能想在http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/处遵循这个惊人的教程,因为这是一步一步的步骤,不会有任何混淆。

关于json,你可以这样做,

 JSONArray jsonArray = new JSONArray("keyName");

    for (int i = 0, len = jsonArray.length(); i < len; i++) {
        JSONObject entry = jsonArray.getJSONObject(i);

        String a = entry.getString("a");

        String b = entry.getString("b");


        }

然后使用基础适配器将其填充到列表视图中。