1

I've followed tutorials and have managed to construct a ViewPager with three frames. I've also registered clicks on a button (using xml onClick) and each time changed the text of the button to "Clicks: 1", "Clicks: 2", etc... The only thing I've not succeeded with is to instantly update the ViewPager to show the new text of the button (the button never updates).

My main source of inspiration:
http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

This is my FragmentActivity:

package com.fragmentviewpager;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

    private PagerAdapter pagerAdapter;


    ViewPager pager;

    Button buttonLeft;
    int clickCounter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_layout);

        pager = (ViewPager) findViewById(R.id.viewpager);

        buttonLeft = (Button) findViewById(R.id.buttonLeft);


        //initialise the viewpager
        initialisePaging();
    }


    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, FragmentLeft.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentMiddle.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentRight.class.getName()));

        pagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);


        pager.setAdapter(this.pagerAdapter);
        pager.setCurrentItem(1);
    }

    // THIS IS THE PLACE WHERE I NEED TO ADD SOMETHING FOR WANTED RESULT
    public void buttonClick(View v) {
        ++clickCounter;
        buttonLeft.setText("Clicks: " + clickCounter);
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }

}

And this is my FragmentPageAdapter:

package com.fragmentviewpager;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;


public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> frags) {
        super(fm);
        fragments = frags;
    }


    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

I've tried to do things like

pagerAdapter.notifyDataSetChanged();

and

pagerAdapter.startUpdate((ViewGroup)findViewById(R.layout.viewpager_layout));
pagerAdapter.instantiateItem((ViewGroup)findViewById(R.layout.viewpager_layout), 0);
pagerAdapter.finishUpdate((ViewGroup)findViewById(R.layout.viewpager_layout));

But nothing seems to work and I suspect that I have misunderstood something essential in the nature of the ViewPager or maybe Fragments.

I've read this question ViewPager PagerAdapter not updating the View but it only makes me more confused. I guess alvarolb might be on to something but I'm not able to implement his suggestions in my code.

Community
  • 1
  • 1

0 Answers0