简体   繁体   中英

Can't get activity to communicate with fragment through interface

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM