如何更新viewPager的当前视图?

时间:2014-07-20 13:16:51

标签: android android-viewpager toggle

我需要更改切换上所有视图的大小。我做到了,但是 textize仅从第3个视图更改。第一个和第二个得到了 不变。请建议更新所有观点。

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);

    }
}

我知道第一个没有改变,因为它没有重新加载,但第二个视图怎么样?请在切换按钮单击时帮助更改所有视图的文本大小。

3 个答案:

答案 0 :(得分:1)

ViewPager创建并保留当前页面旁边的另一个页面,这说明了为什么第二个页面保持不变。

要实现您想要的功能,您需要在单击切换按钮时手动更新大小,您可以在ViewPagerAdapter中保留对当前视图的引用,并创建一个方法以返回该视图,如下所示

public TextView getCurrentView(){
    return txtNewsDesc;
}

然后在addMainButtonListener方法中使用您创建的方法获取当前视图并更新文本大小

togButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        if (isChecked) {
            fntSize=50;
        } else {
            fntSize=30;
        }
        ((ViewPagerAdapter)viewPager).getCurrentView().setTextSize(fntSize);
    }

答案 1 :(得分:0)

你应该使用

public int getItemPosition(Object object) {
return POSITION_NONE;

}

setTag()中使用instantiateItem()而不是调用notifyDataSetChanged()使用findViewWithTag()并更新游览文字大小。

要查看此内容并在此帖子中https://stackoverflow.com/a/8024557

答案 2 :(得分:0)

我找到了解决方案

创建界面RefreshFragment

public interface RefreshFragment{void refresh();}

在作为寻呼机项目的片段中实现RefreshFragment

并在创建寻呼机适配器的类中执行以下操作

circlePageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                ((RefreshFragment) pagerAdapter.getItem(position)).refresh();
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

我希望这是你的解决方案