1

我正在使用CursorFragmentPagerAdapterviewPager显示书页。每页包含一个TextView,我想更改所有页面内所有 TextViews 的 TextSize,每本书大约有 400 页。

我通过更改 TextView 的 textSize 并调用来做到这一点,pagerAdapter.notifyDataSetChanged()但当前、下一页和前一页根本不会影响,其他页面的 textSize 会改变。

这是我的代码: CursorFragmentPagerAdapter

public abstract class CursorFragmentPagerAdapter extends FragmentPagerAdapter
{

    protected boolean mDataValid;
    protected Cursor mCursor;
    protected Context mContext;
    protected SparseIntArray mItemPositions;
    protected HashMap<Object, Integer> mObjectMap;
    protected int mRowIDColumn;
    protected String title;

    public CursorFragmentPagerAdapter(Context context, FragmentManager fm, Cursor cursor)
    {
        super(fm);
        init(context, cursor);
    }

    void init(Context context, Cursor c)
    {
        mObjectMap = new HashMap<Object, Integer>();
        boolean cursorPresent = c != null;
        mCursor = c;
        mDataValid = cursorPresent;
        mContext = context;
        mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
    }

    @Override
    public int getItemPosition(Object object)
    {
        return POSITION_NONE;
    }

    public void setItemPositions()
    {
        mItemPositions = null;

        if (mDataValid)
        {
            int count = mCursor.getCount();
            mItemPositions = new SparseIntArray(count);
            mCursor.moveToPosition(-1);
            while (mCursor.moveToNext())
            {
                int rowId = mCursor.getInt(mRowIDColumn);
                int cursorPos = mCursor.getPosition();
                mItemPositions.append(rowId, cursorPos);
            }
        }
    }

    @Override
    public Fragment getItem(int position)
    {
        if (mDataValid)
        {
            mCursor.moveToPosition(position);
            return getItem(mContext, mCursor);
        }
        return null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        int rowId = mCursor.getInt(mRowIDColumn);
        Object obj = super.instantiateItem(container, position);
        mObjectMap.put(obj, Integer.valueOf(rowId));

        return obj;
    }

    public abstract Fragment getItem(Context context, Cursor cursor);

    @Override
    public int getCount()
    {
        if (mDataValid)
        {
            return mCursor.getCount();
        }
        else
        {
            return 0;
        }
    }
}

BookPageFragment

public class BookPageFragment extends Fragment
{
    int fontsize = 20;
    String text = "";

    static BookPageFragment newInstance(Context context, String text, int fontsize)
    {
        BookPageFragment f = new BookPageFragment();
        Bundle args = new Bundle();
        args.putString("text", text);
        args.putInt("fontsize", fontsize);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
        {
            text = getArguments().getString("text");
            fontsize = getArguments().getInt("fontsize");
        }
    }

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

        TextView tv = (TextView) v.findViewById(R.id.pageText);
        tv.setTextSize(fontsize);
        tv.setText(Html.fromHtml(text));
        return v;
    }
}

bookActivity

public class bookActivity extends FragmentActivity
{
    ViewPager viewPager = null;
    CursorFragmentPagerAdapter pagerAdapter = null;
    int fontSize;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pages);

        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        fontSize = pref.getInt(PREF_FONT_SIZE_OFFSET, 20);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        new loadBookThread().execute(0);
    }

    public class loadBookThread extends AsyncTask<Integer, Integer, Cursor>
    {
        . . .

        @Override
        protected Cursor doInBackground(Integer... params)
        {
            Cursor cursor = database.getInstance().getbookCursor(bookId);
            pageCounts = cursor.getCount();
            return cursor;
        }

        @Override
        protected void onPostExecute(Cursor result)
        {
            pagerAdapter = new CursorFragmentPagerAdapter(bookActivity.this, getSupportFragmentManager(), result) 
            {
                @Override
                public Fragment getItem(Context context, Cursor cursor)
                {
                    return BookPageFragment.newInstance(bookActivity.this, cursor.getString(1), fontSize);
                }
            }

            viewPager.setAdapter(pagerAdapter);

            super.onPostExecute(result);
        }
    }

    public void changeFontSize(int n)
    {
        fontSize += n;
        prefEdit.putInt(PREF_FONT_SIZE_OFFSET, fontSize);
        ((TextView)findViewById(R.id.pageText)).setTextSize(fontSize);
        pagerAdapter.notifyDataSetChanged();
        viewPager.invalidate();
        prefEdit.commit();
    }
}
4

2 回答 2

2

通过使用FragmentStatePagerAdapter而不是解决问题FragmentPagerAdapter

于 2013-05-25T06:13:38.807 回答
0

编辑: 如果要动态设置文本大小,可以在 Fragment 类中执行此操作BookPageFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);
    View v = inflater.inflate(R.layout.rate_fragment, container,false);


    yourTextView = (TextView)v.findViewById(R.id.your_text_view);
    yourTextView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Large);//here I used a default Android style, but you should define and use yours

(...)

如果问题只是显示的当前片段,我看到的唯一解决方案是“刷新”活动。这是代码(您需要关闭并重新打开活动):

finish();//ends the current book activity
Intent intent = new Intent(this, bookActivity.class);
startActivity(intent);//restart the activity

我不确定是否有除此之外的其他解决方案(我希望有),我添加以使用它在我的应用程序上动态“刷新”我的应用程序主题。

注意:您必须在修改文本大小的代码之后添加上面的 3 行。

于 2013-05-23T06:33:04.027 回答