在单独的片段中实现两个RecyclerView,但在同一个活动中

时间:2015-09-20 05:08:48

标签: java android xml android-fragments

我在MainActivity.java中有一个工作的RecyclerView和常规事件的片段“概述”,但我想在一个单独的片段(“提醒”)中实现第二个。在我的MainActivity的OnCreate中,设置了第一个片段,我面临的问题是无法使用setContentView两次。

这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    OverviewAdapter mAdapter;

    private RecyclerView reminderRecyclerView;
    private RecyclerView.LayoutManager reminderLayoutManager;
    RemindersAdapter reminderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);


        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        events();
        mAdapter = new OverviewAdapter(events);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onNavigationDrawerItemSelected(int element) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = new Fragment();
        element++;
        // int frame = R.id.container;

        String position = "Overview";
        switch(element) {

            case 1:
                position = "Overview";
                mTitle = getString(R.string.title_section1);
                // frame = R.id.container;
                break;

            case 2:
                position = "Reminders";
                mTitle = getString(R.string.title_section2);
                fragment = new Reminders();

                setContentView(R.layout.reminder_recycler);
                // REMINDERS RECYCLERVIEW SETUP BELOW
                setContentView(R.layout.reminder_recycler);
                reminderRecyclerView = (RecyclerView) findViewById(R.id.my_reminder_recycler_view);

                // use this setting to improve performance if you know that changes
                // in content do not change the layout size of the RecyclerView
                reminderRecyclerView.setHasFixedSize(true);

                reminderEvents();
                // use a linear layout manager
                reminderLayoutManager = new LinearLayoutManager(this);
                reminderRecyclerView.setLayoutManager(reminderLayoutManager);


                reminderAdapter = new RemindersAdapter(reminderEventsList);
                reminderRecyclerView.setAdapter(reminderAdapter);

                break;

            case 3:
                position = "Categories";
                mTitle = getString(R.string.title_section3);
                fragment = new Categories();
                // frame = R.id.categories;
                break;

            case 4:
                position = "Smart Suggestions";
                mTitle = getString(R.string.title_section4);
                fragment = new SmartSuggestions();
                // frame = R.id.smart_suggestions;
                break;

        }
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();

    }
}

我在Reminders类中被赋予空指针异常,如下所示:

public class Reminders extends Fragment {

private RecyclerView reminderRecyclerView;
private RecyclerView.LayoutManager reminderLayoutManager;
RemindersAdapter reminderAdapter;
Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.reminders, container, false);

    // Inflate the layout for this fragment
    return rootView;



}

public List<ReminderEvents> reminderEventsList = new ArrayList<>();
public void reminderEvents() {

    reminderEventsList.add(new ReminderEvents("Example reminder", "Wed 12 July"));
    reminderEventsList.add(new ReminderEvents("Example reminder1", "Wed 12 July"));
    reminderEventsList.add(new ReminderEvents("Example reminder2", "Wed 12 July"));
    reminderEventsList.add(new ReminderEvents("Example reminder3", "Wed 12 July"));

  }

}

0 个答案:

没有答案