0

I am trying to get my activity to make a call to my fragment when my viewpager detects any swiping.

Here is my activity code.

public interface SwipeListener {
    void swipe();
}

private SwipeListener mSwipeListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog);
    ButterKnife.bind(this);
    setSupportActionBar(mToolbar);

    int startingFragmentPosition = 0;

    mSwipeListener = (SwipeListener) this;

    ClothingSectionsPagerAdapter sectionsPagerAdapter =
            new ClothingSectionsPagerAdapter(getSupportFragmentManager());
    sectionsPagerAdapter.setContext(this);
    mViewPager.setAdapter(sectionsPagerAdapter);
    mTabLayout.setupWithViewPager(mViewPager);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    mViewPager.setCurrentItem(startingFragmentPosition);

    mViewPager.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View view, DragEvent dragEvent) {
            mSwipeListener.swipe();
            return false;
        }
    });
}

In my fragment I implement this interface and override the method. However when I run the app. It crashes, saying that it cannot cast this to SwipeListener in the onCreate method. How do I fix this?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54
  • Can you describe what you are trying to achieve, Do you want to send swipe action to fragment? – Sasi Kanth Dec 29 '17 at 08:48
  • I want the fragment to start loading data early. Because currently when there is a swipe event, the fragment doesn't load the data automatically. – Tom Finet Dec 29 '17 at 08:52
  • You can call the load methods from activity and then directly pass it to fragment by getting registered fragment: https://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager. The listener implementation for this is wrong AFAIK. – Sasi Kanth Dec 29 '17 at 08:55
  • Your implementation is wrong. Here you are just type casting fragment context into SwipeListener. You need to implement interface in activity and set the interface from there. – Sunil Sunny Dec 29 '17 at 08:59

2 Answers2

1

This is because you are trying to put your activity as SwipeListener. Your fragment should implement this interface, and then put this fragment as the SwipeListener.

I think is better to pass to your ClothingSectionsPageAdapter the list of fragments (creating them in your activity) so you can have a reference, or maybe create your SwipeListener in the same method you create your list of fragments.

0

This is not how it works . You have to register callback to publisher component (In your case Activity). If you want to pass swipe action to fragment form Activity . You should implement SwipeListener in Fragment and set it to Activity . Below is an Example.

public class MainActivity extends AppCompatActivity {
private List<SwipeListener> listeners = new ArrayList<>();
public void addSwipeListener(SwipeListener listener) {
    listeners.add(listener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    mViewPager.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View view, DragEvent dragEvent) {
            for (SwipeListener listener : listeners) {
                listener.swipe();
            }
            return false;
        }
    });
}

}

And the example fragment.

class FragmentA extends Fragment implements SwipeListener{
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
 ((MainActivity)getActivity()).addSwipeListener(this);
    super.onActivityCreated(savedInstanceState);
 }

    @Override
    public void swipe() {
        // here you will get callback
    }
}

I have made a list of callback so you can manage multiple fragments, modify it as per your need . And do not forget to remove listener on fragmnent's Detach.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Couldn't the`getActivity()` call potentially be `null` in `onCreateView()`. I think it should be in `onActivityCreated()`. – Mark Dec 29 '17 at 09:09