与标签和碎片问题 [英] problems with the tabs and the fragment

查看:206
本文介绍了与标签和碎片问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的应用程序选项卡的固定刷卡+与Eclipse,我不明白我怎么能为每个标签片段。我知道这是一个愚蠢的问题,但我只是无法理解。感谢所有谁回应。
在code是如下:

 包com.example.newapp;进口java.util.Locale中;进口android.app.ActionBar;
进口android.app.FragmentTransaction;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.app.NavUtils;
进口android.support.v4.view.ViewPager;
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;公共类MainActivity扩展FragmentActivity工具
        ActionBar.TabListener {/ **
 *本{@link android.support.v4.view.PagerAdapter},将提供
 *对于每个部分的片段。我们使用
 * {@link android.support.v4.app.FragmentPagerAdapter}衍生物,其
 *将让每一个片段加载在内存中。如果这成为太内存
 *密集,它可能是最好切换到
 * {@link android.support.v4.app.FragmentStatePagerAdapter}。
 * /
SectionsPagerAdapter mSectionsPagerAdapter;/ **
 *本{@link ViewPager}将承载部分内容。
 * /
ViewPager mViewPager;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    //设置操作栏。
    最后的动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    //创建将返回一个片段为三个的适配器
    //应用的主要部分。
    mSectionsPagerAdapter =新SectionsPagerAdapter(
            getSupportFragmentManager());    //设置的ViewPager与部分适配器。
    mViewPager =(ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);    //当不同部分之间滑动,选择对应的
    // 标签。我们也可以使用ActionBar.Tab#select()来做到这一点,如果我们有
    //为Tab的参考。
    mViewPager
            .setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
                @覆盖
                公共无效使用onPageSelected(INT位置){
                    actionBar.setSelectedNavigationItem(位置);
                }
            });    //对于每一个应用中的部分,添加标签,操作栏。
    的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
        //创建对应于定义的页面标题文字标签
        //适配器。同时,指定此活动的对象,它实现
        //将TabListener接口,回调(监听程序)进行时
        //这个选项卡中选择。
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(I))
                .setTabListener(本));
    }
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,
        FragmentTransaction fragmentTransaction){
    //当选择给定的标签上,切换到相应的页面
    //的ViewPager。
    mViewPager.setCurrentItem(tab.getPosition());
}@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,
        FragmentTransaction fragmentTransaction){
}@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,
        FragmentTransaction fragmentTransaction){
}/ **
 * A {@link FragmentPagerAdapter}返回对应片段
 *章节/标签/页面。
 * /
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {    公共SectionsPagerAdapter(FragmentManager FM){
        超(FM);
    }    @覆盖
    公共片段的getItem(INT位置){
        //的getItem被称为实例化片段为给定的页面。
        //返回一个DummySectionFragment(定义为静态内部类
        //下面)附页码为它的独立参数。
        片段片段=新DummySectionFragment();
        捆绑ARGS =新包();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
        fragment.setArguments(参数);
        返回片段;
    }    @覆盖
    公众诠释的getCount(){
        //显示3总页​​数。
        返回3;
    }    @覆盖
    公共CharSequence的getPageTitle(INT位置){
        区域设置L = Locale.getDefault();
        开关(位置){
        情况下0:
            返回的getString(R.string.title_section1).toUpperCase(升);
        情况1:
            返回的getString(R.string.title_section2).toUpperCase(升);
        案例2:
            返回的getString(R.string.title_section3).toUpperCase(升);
        }
        返回null;
    }
}/ **
 *一个虚拟片段重新presenting应用程序的一部分,但只是
 *显示虚拟文本。
 * /
公共静态类DummySectionFragment扩展片段{
    / **
     *片段参数重新presenting本节号
     *片段。
     * /
    公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;    公共DummySectionFragment(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main_dummy,
                集装箱,FALSE);
        TextView的dummyTextView =(TextView中)rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments()调用getInt(
                ARG_SECTION_NUMBER)));
        返回rootView;
    }
}
}


解决方案

片段在你实施 FragmentPagerAdapter (要创建 SectionsPagerAdapter ),在方法的getItem()。见例如:

  @覆盖
公共片段的getItem(INT位置){
    开关(位置){
        情况下0:
            返回新MyFragment0();
        情况1:
            返回新MyFragment1();
        案例2:
            返回新MyFragment2();
    }
    返回null;
}

getCount将方法应该返回要提供碎片的数量。然后, FragmentActivity 将调用的getItem 需要一个片段时。

I created an application "fixed tabs + swipe" with Eclipse, I can not understand how I can create a Fragment for each tab. I know it's a stupid question but I just can not understand. Thanks to all who respond. The code is as follows

package com.example.newapp;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }
}
}

解决方案

The Fragments have to be created in your implementation of the FragmentPagerAdapter (SectionsPagerAdapter), in the method getItem(). See example :

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new MyFragment0();
        case 1:
            return new MyFragment1();
        case 2:
            return new MyFragment2();
    }
    return null;
}

The getCount method should return the number of fragments you want to be available. Then, the FragmentActivity will call getItem when a fragment is needed.

这篇关于与标签和碎片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆