Android Studio 无法解析 fragmentActivity 和 ViewPager 导入 [英] Android Studio Can't resolve fragmentActivity and ViewPager imports
本文介绍了Android Studio 无法解析 fragmentActivity 和 ViewPager 导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循开发可滑动标签的教程.当我导入时:
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 显示我无法解析符号ViewPager"和FragmentActivity".如何解决这个问题?谢谢.
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) {
}
}
推荐答案
- 确保您已使用 SDK 管理器下载了 Android 支持库.
- 打开应用程序的 build.gradle 文件.
将支持库添加到依赖项部分.例如,要添加 v4 支持库,请添加以下几行:
- Make sure you have downloaded the Android Support Repository using the SDK Manager.
- Open the build.gradle file for your application.
Add the support library to the dependencies section. For example, to add the v4 support library, add the following lines:
依赖项{...编译com.android.support:support-v4:18.0.+"}
dependencies { ... compile "com.android.support:support-v4:18.0.+" }
这篇关于Android Studio 无法解析 fragmentActivity 和 ViewPager 导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文