Android 选项卡布局中的 Recycler 视图

Android Recycler View in tab layout

我正在尝试设置一个包含 3 个选项卡的 tablayout,每个选项卡中都会有一个回收站视图。我发现了这个问题: 这与我想要的类似,我正在尝试设置它,然后在我开始工作后设置我的数据检索。

我现在遇到的问题是设置 PagerAdapter。这是我的代码:

public class WorkoutDaysActivity extends BaseActivity{

ListView mListView = new ListView(this);
ArrayList<CustomObject> w29w1m;
CustomListViewAdapter mCustomListViewAdapter;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_days);
    mToolBar = activateToolbar();
    setUpNavigationDrawer();

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), WorkoutDaysActivity.this)

    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }

}


@Override
public void onResume() {
    super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three" };
    Context context;

    public PagerAdapter(FragmentPagerAdapter fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new WorkoutDetailFragment();
            case 1:
                return new WorkoutDetailFragment();
            case 2:
                return new WorkoutDetailFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(WorkoutDaysActivity.this).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) tab.findViewById(R.id.custom_text);
        tv.setText(tabTitles[position]);
        return tab;
    }

}}

我遇到错误的第一个地方是

PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), WorkoutDaysActivity.this)

    viewPager.setAdapter(pagerAdapter);

错误是:

 PagerAdapter (android.support.v4.app.FragmentPagerAdapter, Context) in PagerAdapter cannot be applied to: (android.support.v4.app.FragmentManager, WorkoutDaysActivity.this) 

android.support.v4.app.FragmentManager是红色下划线部分。第二个错误在这部分:

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three" };
    Context context;

    public PagerAdapter(FragmentPagerAdapter fm, Context context) {
        super(fm);
        this.context = context;
    }

错误在super(fm);部分,它指出:

FragmentPagerAdapter (android.support.v4.app.FragmentManager) in FragmentPagerAdapter cannot be applied to: android.support.v4.app.FragmentPagerAdapter

我觉得这只是导入错误的一个简单错误,但我尝试修复它失败了。非常感谢任何帮助!

您的 PagerAdapter class 扩展了 FragmentPagerAdapter。然后它的构造函数采用一个 FragmentPagerAdapter 来初始化。因此,要初始化 FragmentPagerAdapter,您已经需要 FragmentPagerAdapter 了吗?

我认为该参数应该是 FragmentManager。