我想使用一个名为MainActivity的活动,该活动具有抽屉布局和主内容容器(使用FrameLayout)。抽屉布局目前只是一个普通的ListView,目前只有两个项目。我为每件物品制作了两个碎片。第一个项目是主页,第二个项目是保存。当调用MainActivity onCreate()时,我用HomeFragment替换容器,然后我可以使用Drawer导航到SavedFragment。导航功能运行良好,没有任何问题。我想完成的任务是,每当我点击抽屉上的第二个项目进入SavedFragment时,汉堡图标都应该变为后退按钮/向上按钮。我通过浏览同一问题上的许多StackOverflow主题来更改它,但我无法添加点击事件。它根本听不进去。我在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);
}}
保存的片段代码
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);
}
}}
我在上面尝试了一些东西,但没有成功,所以我发布了我尝试实现解决方案的代码的干净版本。
我确信这些解决方案是正确的,但我无法理解为什么这些方案对我不起作用
我应用了这里给出的解决方案:当使用片段时,在Android导航抽屉图像和向上插入符号之间切换
如果您使用的是碎片,则必须在MainActivity 中使用navigationUp
@Override
public boolean onSupportNavigateUp() {
getSupportFragmentManager().popBackStack();
return true;
}