当片段更改标签时,文本不会更改

时间:2017-02-08 13:14:40

标签: java android android-studio android-fragments android-tablayout

当屏幕更新并更改标签时,其他标签的文字仍然具有第一个标签的特征。

private int mPage;

public static FeedFrag newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    FeedFrag fragment = new FeedFrag();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage = getArguments().getInt(ARG_PAGE);
}

// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.feed_frag, container, false);
    TextView first = (TextView) view.findViewById(R.id.Feed);
    first.setText("feed");
    return view;
}

这是另一个标签

public class InStockFrag extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    public static InStockFrag newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        InStockFrag fragment = new InStockFrag();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    // Inflate the fragment layout we defined above for this fragment
    // Set the associated text for the title
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.in_stock_frag, container, false);
        TextView second = (TextView) view.findViewById(R.id.InStock);
        second.setText("In Stock");
        return view;
    }
}

主要活动

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

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new SimpleFragmentPagerAdapter(getSupportFragmentManager()));

    // Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabsStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {
           // Toast.makeText(getActivity().this, "Selected page positon: " + position, Toast.LENGTH_SHORT).show();
        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // Code goes here
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {

            // Code goes here
        }

    });
    tabsStrip.setViewPager(viewPager);
}

SimpleFragmentAdapter:

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Feed", "In Stock", "Upcoming" };

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

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

    @Override
    public Fragment getItem(int position) {
        return FeedFrag.newInstance(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
} 

该应用程序将3个标签上的文字返回为" Feed"而不是其他的话。

1 个答案:

答案 0 :(得分:0)

是的,很明显。这是因为你没有在getItem中的SimpleFragmentPagerAdapter方法中正确调用你的片段:你总是在调用FeedFrag

尝试像这样更改您的视图寻呼机适配器的代码,并在getItem方法内,将代码调用您的第三个片段,即标题为“即将到来”的片段:

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Feed", "In Stock", "Upcoming" };

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

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

    @Override
    public Fragment getItem(int position) {

        if (position==0) {
            return FeedFrag.newInstance(position + 1);
        } else if (position == 1) {
            return InStockFrag.newInstance(position + 1); 
        } else {
            //HERE RETURN SAME METHOD OF UPCOMING FRAGMENT
        }

    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

}