问题
I have downloaded a code from Github.I can not run the program.The problem is the method addOnPageChangeListener is undefined.Here's the error:
The method addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener) is undefined for the type ViewPager
What should i do? Should I use another method? Here's the code:
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
public class FourthActivity extends AppCompatActivity {
// Need this to link with the Snackbar
private CoordinatorLayout mCoordinator;
//Need this to set the title of the app bar
private CollapsingToolbarLayout mCollapsingToolbarLayout;
private FloatingActionButton mFab;
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ViewPager mPager;
private YourPagerAdapter mAdapter;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
mCoordinator = (CoordinatorLayout) findViewById(R.id.root_coordinator);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mAdapter = new YourPagerAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);
//Notice how the Tab Layout links with the Pager Adapter
mTabLayout.setTabsFromPagerAdapter(mAdapter);
//Notice how The Tab Layout adn View Pager object are linked
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Notice how the Coordinator Layout object is used here
Snackbar.make(mCoordinator, "FAB Clicked", Snackbar.LENGTH_SHORT).setAction("DISMISS", null).show();
}
});
//Notice how the title is set on the Collapsing Toolbar Layout instead of the Toolbar
mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.title_activity_fourth));
}
回答1:
Find your dependency of android support-v4 library in build.gradle, upgrade the version to 22.2.1 or 23.0.1, the line looks like this
compile 'com.android.support:support-v4:23.0.1'
来源:https://stackoverflow.com/questions/32396846/addonpagechangelistener-is-undefined