gpt4 book ai didi

Android:更改viewPager页面内所有TextViews的文本大小

转载 作者:行者123 更新时间:2023-11-30 03:35:24 25 4
gpt4 key购买 nike

我正在使用 CursorFragmentPagerAdapterviewPager 来显示书页。每个页面都包含一个 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 解决了问题。

关于Android:更改viewPager页面内所有TextViews的文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com