I follow the tutorial of developing swipe-able tabs. When I import:
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
Android Studio shows me cannot resolve the symbol "ViewPager" and "FragmentActivity". How to solve this? thanks.
Below is the entire code of my project.
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
When ViewPager used FragmentStatePagerAdapter to page through a large or unknown number of fragment, use FragmentStateAdapter with ViewPager2. Fragment-based adapter classes inheriting from FragmentPagerAdapter or FragmentStatePagerAdapter always accept a single FragmentManager object as a constructor parameter.
FragmentActivity. Base class for activities that wish to use some of the newer platform features on older Android devices. Base class for activities that want to use the support-based Fragments . When using the <fragment> tag, this implementation can not use the parent view's ID as the new fragment's ID.
A FragmentManager manages Fragments in Android, specifically, it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments. getPageTitle (int pos): (optional) Similar to getItem () this methods returns the title of the page at index pos.
There is no android.app.FragmentActivity. It is android.support.v4.app.FragmentActivity. It was working before I did the steps I mentioned. If I use the android.support.v4.app.FragmentActivity i got ClassNotFoundException
Add the support library to the dependencies section. For example, to add the v4 support library, add the following lines:
dependencies { ... compile "com.android.support:support-v4:18.0.+" }
I have solved it. I installed everything but I did not import the external library into my library. It was not installed automatically during creation of the new project. So I just opened the project structure and imported the dependencies -> add support-v4 library.
Btw, thanks you guys for helping me a lot and posting the suggestion to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With