impream
impream

Reputation: 143

How to update currentview of viewPager?

I need to change the size of all views on toggle. I did it but the textsize got changed only from 3rd view. The first and second got unchanged. Please suggest to update all the views.

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

And My ViewPager class is

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

    }
}

I know the first is not changed since it is not reloaded but what about 2nd view? Please help to change the text size of all the views on toggle button click.

Upvotes: 2

Views: 4715

Answers (3)

FAHAD HAMMAD ALOTAIBI
FAHAD HAMMAD ALOTAIBI

Reputation: 143

i found solution

crerate interface RefreshFragment

public interface RefreshFragment{void refresh();}

in the fragment which is pager item implement RefreshFragment

and at the class where you create the pager adapter do the folowing

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

i hope this is your solution

Upvotes: 0

Omar Albelbaisy
Omar Albelbaisy

Reputation: 827

The ViewPager creates and retains another page beside the current one and that demonstrates why the second stay the same.

To achieve what you want you need to update the size manually when the toggle button clicked, you can keep a reference to the current view in the ViewPagerAdapter and create a method to return that view as follow

public TextView getCurrentView(){
    return txtNewsDesc;
}

Then in addMainButtonListener method get the current view using the method you have created and update the text size

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

Upvotes: 1

Rajnish Mishra
Rajnish Mishra

Reputation: 836

You should use

public int getItemPosition(Object object) {
return POSITION_NONE;

}

use setTag() in instantiateItem() and instead of called notifyDataSetChanged() use findViewWithTag() and update tour text size.

For see this and in this thread https://stackoverflow.com/a/8024557

Upvotes: 0

Related Questions