我正在使用CursorFragmentPagerAdapter
和viewPager
来显示书页。每个页面都包含TextView
,我想在所有页面中更改所有TextView的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();
}
}
答案 0 :(得分:1)
使用FragmentStatePagerAdapter
代替FragmentPagerAdapter
解决了问题。
答案 1 :(得分: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行。