android - 使用 ListFragment 和 ViewPager 时未找到源

标签 android android-layout android-viewpager android-listfragment

我一直在尝试创建一个简单的水平滚动选项。 我有 2 个 ListFragment,我想在它们之间滚动。

从 onCreateView 函数返回 View 时出现“找不到源”错误。

这是我的 MainActivity 类:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
        final ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction fragmentTransaction) {
    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        Fragment a;
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new FragmentA();

                default:
                    return new FragmentB();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }
}

这是我的 ListFragment 类之一(第二个是相同但名称和布局文件不同的类):

    public class FragmentA extends ListFragment {

        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.favorites_layout, container, false);
                updateStationsView(0);
                return rootView;
            }
}

updateStationView 是一个填充列表的函数。删除它没有帮助,所以我认为它是无害的。

错误是在:

之后立即抛出的
return rootView;

返回到 MainActivity 的 onCreate 函数。

当我将 ListFragment 更改为一个简单的 Fragment 时,它起作用了。我想我缺乏一些知识来解决这个问题。我真的很想用 ListFragment...

有人可以帮忙吗? 非常感谢您的努力。

添加 XML 文件:

activity_main.xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

favorites_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

   <ListView 
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />
</LinearLayout>

最佳答案

我很确定问题是您的 ListFragment 自定义布局不包含 ListView。这些代码行导致错误,因为您需要在“favourites_layout”.xml 文件中包含一个 ListView。

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.favorites_layout, container, false);
      updateStationsView(0);
      return rootView;
 }

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

因此您的布局可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

   <ListView 
     android:id="@android:id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />
</LinearLayout>

更多内容请看这里: http://developer.android.com/reference/android/app/ListFragment.html

关于android - 使用 ListFragment 和 ViewPager 时未找到源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134605/

相关文章:

android - Facebook AudienceNetwork 错误

android - 从 Recycler 中的 Firebase 存储加载图像显示图像发生变化

android - 请求权限 READ_SMS 后,包安装程序在 Android M 中崩溃

android - 尝试更改设备配置时找不到 ID XXX 的 View

javascript - NestedScrollView 开始位置错误,布局较大

java - 想要在 viewpager 页面上启动动画,但页面正在预加载

java - 本地化不显示在设备上

Android模拟器调试时显示空白

android - 滑动 View 分页器 fragment 不会移动选项卡

android - 想用ViewPager,android.support.* 无法被识别?