选项卡布局中的Android Recycler View

时间:2016-07-28 17:08:26

标签: android android-fragments android-recyclerview android-pageradapter

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

我现在遇到的问题是设置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

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

1 个答案:

答案 0 :(得分:0)

您的PagerAdapter类扩展了FragmentPagerAdapter。然后它的构造函数TAKES一个FragmentPagerAdapter进行初始化。那么要初始化FragmentPagerAdapter你需要一个FragmentPagerAdapter吗?

我认为该参数应该是FragmentManager。