0

I am using a ViewPager with my own PagerAdapter (Note: Not using FragmentPagerAdapter). I am trying to communicate with my fragments via my Activity, but when I try and get a reference to the fragment it always returns null.

Here is my instantiate method in my custom PagerAdapter:

@Override
public Object instantiateItem(ViewGroup container, int position) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = null;

    switch (position) {
    case 0:
        itemView = inflater.inflate(R.layout.left, container, false);
        break;
    case 1:
        itemView = inflater.inflate(R.layout.middle, container, false);
        break;
    case 2:
        itemView = inflater.inflate(R.layout.right, container, false);
        break;
    case 3:
        itemView = inflater.inflate(R.layout.right_right, container, false);
        break;
    }

    ((ViewPager) container).addView(itemView);
    return itemView;
}

Here is my xml for middle.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.example.testtabswipe.MiddleFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</fragment>

Here is my xml for my MiddleFragment class (filenmae: middle_fragment.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/middle_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff555" >


    <TextView 
        android:text="Testing some text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="columnWidth"
        android:numColumns="2" 
        android:background="#fbb111" />

</RelativeLayout>

And finally, this is what I'm calling in my Activity to get a reference to the Fragment:

MiddleFragment middleFragment = (MiddleFragment) getSupportFragmentManager().findFragmentById(R.id.middle_fragment);
if(middleFragment == null)
    Log.d("Karl", "middleFragment Fragment null");
else
    Log.d("Karl", "middleFragment Fragment is not null");

It always results in "middleFragment Fragment null"

Any ideas?

PS: This is affecting all fragments not just middle.

basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

0

Please look at my answer here for communicating with Fragments inside ViewPager. The idea is to set new data in adapter that you want to send to Fragments and call notifyDataSetChanged() which in turn will call getItemPosition(). From where you will get reference to Fragment:

@Override
public int getItemPosition(Object object) {
    if (object instanceof UpdateableFragment) {
        ((UpdateableFragment) object).update(xyzData);
    }
    //don't return POSITION_NONE, avoid fragment recreation. 
    return super.getItemPosition(object);
}
Community
  • 1
  • 1
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103