使用导航抽屉在碎片中导航

时间:2015-04-15 13:48:57

标签: android android-layout android-fragments navigation

我想使用一个名为MainActivity的活动,它有一个抽屉布局和主要内容容器(使用FrameLayout)。现在的抽屉布局只是一个普通的ListView,现在只有两个项目。我为每个项目制作了两个片段。第一项是Home,第二项是Saved。 当调用MainActivity onCreate()时,我用HomeFragment替换容器,然后从那里我可以使用Drawer导航到SavedFragment。 导航工作完美,没有问题。 我想要实现的任务是,每当我通过点击抽屉上的第二个项目进入SavedFragment时,汉堡包图标应该变为后退按钮/向上按钮。 我设法通过在同一个问题上经历了很多StackOverflow主题来改变它,但我无法在其上添加click事件。它不会听取点击。我在StackOverflow上阅读了一些非常好的东西,并期望它对我也有用,但也许我的错误在我的代码中。所以我在这里分享代码:

MainActivity(仅限应用程序中的活动)代码

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {


Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView listView;
Fragment mFragment;
ArrayList<NavigationDataRecord> drawerRecord;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    listView = (ListView) findViewById(R.id.drawer_left);
    listView.setOnItemClickListener(this);
    drawerRecord = new ArrayList<NavigationDataRecord>();
    drawerRecord.add(new NavigationDataRecord(R.mipmap.ic_home_grey600_24dp, "Home"));
    drawerRecord.add(new NavigationDataRecord(R.mipmap.ic_save_grey600_24dp, "Saved"));
    CustomAdapter adapter = new CustomAdapter(this, R.layout.list_row, drawerRecord);
    listView.setAdapter(adapter);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);


    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.app_name, R.string.app_name) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu();
        }
    };


        mFragment = new HomeFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.container, mFragment).commit();

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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);

}




@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



    switch (position) {
        case 0:
            mFragment = new HomeFragment();
            break;

        case 1:
            mFragment = new SavedFragment();
            break;

        default:
           break;

    }

    getSupportFragmentManager().beginTransaction().replace(R.id.container,mFragment).addToBackStack(null).commit();
    mDrawerLayout.closeDrawer(listView);

}}

SavedFragment Code

public class SavedFragment extends Fragment {


List<SavedInformation> list;
RecyclerView cardView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.saved_fragment, container, false);
    list = new ArrayList<SavedInformation>();
    makeCardList();
    cardView = (RecyclerView) view.findViewById(R.id.cardList);
    //cardView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    cardView.setLayoutManager(llm);
    SavedInformationAdapter adapter = new SavedInformationAdapter(list, getActivity());
    cardView.setAdapter(adapter);


    return view;
}

private void makeCardList() {
    DatabaseAdapter adapter = new DatabaseAdapter(getActivity());
    Cursor cursor = adapter.getData();

    while (cursor.moveToNext()) {
        SavedInformation information = new SavedInformation();
        information.setUsername(cursor.getString(0));
        information.setDomainName(cursor.getString(1));
        information.setImageURL(cursor.getString(2));
        list.add(information);

    }
}}

我尝试了一些这些,但没有运气,所以我发布了我尝试实现解决方案的代码的干净版本。

我确信这些解决方案是正确的,但我无法理解为什么那些对我不起作用。

我应用了此处给出的解决方案:Switching between Android Navigation Drawer image and Up caret when using fragments

1 个答案:

答案 0 :(得分:1)

如果您正在使用片段,那么您必须在MainActivity中使用

@Override
public boolean onSupportNavigateUp() {
    getSupportFragmentManager().popBackStack();
    return true;
}