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?

4
  • Can you describe what you are trying to achieve, Do you want to send swipe action to fragment? Commented Dec 29, 2017 at 8: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. Commented Dec 29, 2017 at 8:52
  • You can call the load methods from activity and then directly pass it to fragment by getting registered fragment: stackoverflow.com/questions/8785221/…. The listener implementation for this is wrong AFAIK. Commented Dec 29, 2017 at 8: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. Commented Dec 29, 2017 at 8:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Couldn't thegetActivity() call potentially be null in onCreateView(). I think it should be in onActivityCreated().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.