-1

I have a viewPager with say 4 pages. All 4 pages uses same Xml. When i do an event in 1st page somehow it always triggers in the last page.

Here is my PagerAdapter

@Override
public Object instantiateItem(ViewGroup container, int pos) {
    View desktopView;

    OnTouchListener tl = null;

    desktopView = act.getLayoutInflater().inflate(
            act.getViewPagerLayout(groupName), null);

    RelativeLayout rr_appContainer, rr_dialogContainer;
    ImageView rr_home_container = (ImageView) desktopView
            .findViewById(R.id.imageView_forClick);

 Button buttonChange = (Button)desktopView.findViewById(R.id.B1);
 Button buttonDelete = (Button)desktopView.findViewById(R.id.B2);


    rr_appContainer = (RelativeLayout) desktopView
            .findViewById(R.id.rr_home_container);
    rr_dialogContainer = (RelativeLayout) desktopView
            .findViewById(R.id.rr_dialogView);
    ..........

   buttonDelete.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            deletestuff();

        }

 buttonChange.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            changeColorOfStuff();

        }

   .....
 return desktopView;

}

What is happening is, When i click on buttonChange from 1st page it supposed to change the color of text on 1st page, but actually it is changing color of the last page. Similarly buttonDelete is deleting color from last page.

Regardless of what page i am in, its reflecting those changes on last page. Any help would be appreciated.

hemantsb
  • 2,049
  • 2
  • 20
  • 29
  • Where is `buttonChange` defined? If it is outside your inner layouts here and in a global, then the last page will probably overwrite it (since it is the last one created inside `instantiateItem` to fire). – Jay Snayder Jan 28 '15 at 15:16
  • buttonChange is defined in instantiateItem. – hemantsb Jan 28 '15 at 15:20
  • Just guessing here but you're manipulating the "current fragment"? Because viewpager by default creates and caches the fragments long before they appear on screen, meaning the last created fragment which is the "current fragment" will get hit by events. The minimum amount of cached fragments is the previous, visible and next iirc. – G_V Jan 28 '15 at 15:44
  • from "current fragment" you mean? – hemantsb Jan 28 '15 at 15:47

1 Answers1

0

From the context given here, the deleteStuff() and changeColorOfStuff() can only be members of the Fragment/Activity that owns the adapter, or the adapter itself. So these methods can only act on members of those classes. ViewPager asks the adapter for the fragments it is going to display. However, the text in the fragment being shown by the ViewPager belong to the that fragment. To act on that text, you need a method that's a member of that fragment. The usual way to do this is to use a custom fragment. For example:

Custom Fragment (inner class):

public static class CustomFragment extends Fragment {
    //members of the fragment
    TextView yourTextView;
    ...
    public static CustomFragment newInstance(int pos) {
        CustomFragment fragment = new CustomFragment();
        //get whatever info you need for this page
        Bundle args = getInfoSomehow(pos);
        fragment.setArguments(args)
        return fragment;
    }

    @Override
    public View onCreateView(Layout inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(....
        yourTextView = root.findViewById(...) //this is the text view you want to change things in
        //all the stuff you're currently doing in instantiateItem()
        return root;
    }

    private void deleteStuff() {
        //whatever you need to do. But notice that here it's acting on the TextView that belongs to this particular fragment
    }

    private void changeColorOfStuff() {...}
    ...
}

Then in your instantiateItem(...)

@Override
public Object instantiateItem(ViewGroup container, int pos) {
    return CustomFragment.newInstance(pos);
}
h0x0
  • 485
  • 3
  • 11
  • No, I need to add items in instantiateItem , just like getView works for BaseAdapter. – hemantsb Jan 28 '15 at 15:56
  • Could you explain why this needs to be done in this method? Since you get to define newInstance(), you can put any information you want into the new fragment. – h0x0 Jan 28 '15 at 16:07