如何在单击导航抽屉项目时打开扩展ListFragment的类?

时间:2017-03-16 16:44:59

标签: android

在此函数中单击nav_manage我想打开扩展ListFragment的ViewTasks。有人可以建议一些代码示例吗? 这是我的MainActivity中的一个功能。

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    displaySelectedScreen(item.getItemId());
return true;
}



  private void displaySelectedScreen(int itemId)
{
    Fragment fragment = null;
    //initializing the fragment object which is selected
    switch (itemId) {
        case R.id.nav_send:
            fragment = new Invitation();
            break;
        case R.id.nav_camera:
            fragment=new Home();
            break;
        case R.id.nav_manage:
            fragment=new ViewTasks();
            break;
        default:
            Toast.makeText(MainActivity2.this,"Choose Something",Toast.LENGTH_LONG).show();
    }
    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

我的ViewTasks课程:

public class ViewTasks extends ListFragment {
private Button addButton;
private TaskManagerApplication app;
private TaskListAdapter adapter;
private Button removeButton;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.home_fragment, container, false);

}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Task Scheduler");
    app = (TaskManagerApplication)getActivity().getApplication();
    adapter = new TaskListAdapter(this.getActivity(), app.getCurrentTasks());
    setListAdapter(adapter);
    setUpViews();

}
@Override
public void onResume() {
    super.onResume();
    adapter.forceReload();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    adapter.toggleTaskCompleteAtPosition(position);
    Task t = adapter.getItem(position);
    app.saveTask(t);
}

protected void removeCompletedTasks() {
    Long[] ids = adapter.removeCompletedTasks();
    app.deleteTasks(ids);
}

private void setUpViews() {
    addButton = (Button)getView().findViewById(R.id.add_button);
    removeButton = (Button)getView().findViewById(R.id.remove_button);

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_frame, new AddTaskActivity())
                    .commit();
        }
    });
    removeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            removeCompletedTasks();
        }
    });

}

}

我希望此函数打开ViewTasks片段,该片段包含已安排任务的列表视图。 当我按照与上述情况相同的模式显示此错误时:

不兼容的类型:

必需:android.app.Fragment

找到:com.happy.taskmanager.ViewTasks

0 个答案:

没有答案