如何在viewpager的所有片段中调用更改文本大小?

时间:2014-10-04 16:46:40

标签: android android-fragments

我想更改viewpager片段中的文字。

这是我的FragmentActivity Code:

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class ScreenSlideActivity extends FragmentActivity {

    private static final int NUM_PAGES = 1000;

    private ViewPager mPager;

    private PagerAdapter mPagerAdapter;

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

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                invalidateOptionsMenu();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

        menu.findItem(R.id.action_previous).setEnabled(
                mPager.getCurrentItem() > 0);

        MenuItem item = menu
                .add(Menu.NONE,
                        R.id.action_next,
                        Menu.NONE,
                        (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1) ? R.string.action_finish
                                : R.string.action_next);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_previous:
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            return true;

        case R.id.action_next:
            mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            return true;
        case R.id.action_larger_text_size:
            //call method in fragment setTextSizeLarger()
            return true;
        case R.id.action_smaller_text_size:
            //call method in fragment setTextSizeSmaller()
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ScreenSlidePageFragment.create(position);
        }

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

这是我的片段:

public class ScreenSlidePageFragment extends Fragment {
    public static final String ARG_PAGE = "page";
    private int mPageNumber;
    private TextView tvText1;
    public static ScreenSlidePageFragment create(int pageNumber) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ScreenSlidePageFragment() {
    }

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

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

        tvText1 = (TextView) rootView.findViewById(android.R.id.text1);
        tvText1.setText(
                getString(R.string.title_template_step, mPageNumber + 1));

        return rootView;
    }

    public void setTextSizeLarger()
    {
        tvText1.setTextSize((float)20);
    }

    public void setTextSizeSmaller()
    {
        tvText1.setTextSize((float)19);
    }

    public int getPageNumber() {
        return mPageNumber;
    }
}

我想要实现的是菜单项更大的文本和更小的文本可以工作。 如果单击将setTextSize所有片段中的所有TextView。 以及如何做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用LocalBroadcastManager

来完成

在你的片段注册接收器中,当点击菜单项并且用户选择例如大图标发送广播时,如下所示:

Intent intent = new Intent(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

并在收到广播的片段中查看其动作并更改文字大小。

BroadcastReceiver onTextSizeChanged = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                    // Change the text size here
            }
        };

这样你也可以发送文字大小。

另一个解决方案是让片段向菜单添加项目(文本大小),当它发生变化时,设置用户文本大小。为此,您必须将setHasOptionsMenu(true);添加到您的片段中。

更新:

因为您尚未注册接收者,所以必须在onCreateViewonCreate注册,并在onDestroy取消注册。这是注册部分:

IntentFilter filter = new IntentFilter(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onTextSizeChanged, filter);

并取消注册:

LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(onTextSizeChanged);

如此简单的逻辑步骤:

1)当用户选择项目使用

发送广播时
Intent intent = new Intent(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

2)在您的片段中将onTextSizeChanged声明为接收者并覆盖onReceive

3)在上述生命周期中注册并取消注册您的接收者(onTextSizeChanged)。