我需要更改切換的所有視圖的大小。我做到了,但 文本大小隻從第三個視圖改變。第一個和第二個得到 不變。請建議更新所有視圖。如何更新viewPager的currentview?
public class MainActivity extends Activity {
ViewPager viewPager;
public static ArrayList<NewsItem> newslist;
public static PagerAdapter adapter;
public static ProgressDialog progressDialog;
LayoutInflater inflater;
public static float fntSize=30;
private ToggleButton togButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newslist = new ArrayList<NewsItem>();
adapter = new ViewPagerAdapter(getApplicationContext(), newslist);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
addMainButtonListener();
loadNews();
}
public void addMainButtonListener() {
togButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
togButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
fntSize=50;
} else {
fntSize=30;
}
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.activity_main, menu);
return (super.onCreateOptionsMenu(menu));
}
private void loadNews(){
progressDialog= ProgressDialog.show(this,"Progress Dialog Title Text","Process Description Text");
News news = new News(getApplicationContext());
news.execute();
viewPager.setCurrentItem(0);
}
}
而且我ViewPager類是
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
ArrayList<NewsItem> newslist = new ArrayList<NewsItem>();
LayoutInflater inflater;
public TextView txtNewsDesc;
public ViewPagerAdapter(Context context, ArrayList<NewsItem> newslist) {
this.context = context;
this.newslist = newslist;
}
@Override
public int getCount() {
return this.newslist.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.news_item, container,
false);
txtNewsDesc = (TextView) itemView.findViewById(R.id.news_desc);
NewsItem news = this.newslist.get(position);
txtNewsDesc.setTextSize(MainActivity.fntSize);
txtNewsDesc.setText(news.getDesc());
// Add viewpager_item.xml to ViewPager
Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/preeti.ttf");
txtNewsDesc.setTypeface(typeFace);
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
我知道第一不改變,因爲它不重裝但對於第二個看法?請幫助更改切換按鈕點擊時所有視圖的文字大小。
非常感謝。但是,雖然更改顯示在第二頁,但我仍不能看到當前頁面中的更改。請建議我。 – impream
好的,第二個解決方案是使用viewPager.getChildAt(0),然後viewPager.getChildAt(1)獲取第一個和第二個頁面的視圖,然後您可以訪問textview並更新這兩個頁面的文本大小。 –
實際上viewPager.getChildAt(0)和viewPager.getChildAt(1)給出viewPager的第一頁和第二頁,但它不給出當前頁面的當前視圖和相對下一個視圖。如果我理解有任何錯誤,如果是,那麼plz建議。我希望所有的改變都能夠在切換按鈕被點擊的所有視圖頁面中實現。 – impream