我正在使用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();
}
}
最佳答案
通过使用FragmentStatePagerAdapter
代替FragmentPagerAdapter
解决了问题。