我在Main FragmentActivity中实现了一个Drawer,但是不知道如何在master / detail片段中添加一个后导航按钮....发现几天仍然没有答案....任何专家都可以帮忙吗?
>> MainActivity with Drawer
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
CreateMenuItem();
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
switch (position) {
case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, FragmentA.newInstance(), FragmentA.TAG).commit();
break;
case 1:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, FragmentB.newInstance(), FragmentB.TAG).commit();
break;
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, FragmentC.newInstance(), FragmentC.TAG).commit();
break;
case 3:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, FragmentD.newInstance(), FragmentD.TAG).commit();
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
>> Fragment A (Master Fragment)
public class FragmentA extends Fragment {
public FragmentA(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentA, container, false);
Button button= (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentDetail fd = new FragmentDetail();
android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentdetail, fd);
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
>> FragmentDetail (Detail Fragment) > How to add a back button instead the navigator drawer menu
public class FragmentDetail extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
return rootView;
}
}
我在Main FragmentActivity中实现了一个Drawer,但是不知道如何在master / detail片段中添加一个后导航按钮....发现几天仍然没有答案....任何专家都可以帮忙吗?
答案 0 :(得分:0)
/ ** *处理所有碎片的背压(在碎片活动中使用此功能) * * * /
@Override
public void onBackPressed() {
super.onBackPressed();
}
答案 1 :(得分:0)
我通常将onKeyListener设置为onResume中的View。根据我的学习,你必须注意在视图上设置 setFocusableInTouchMode()和requestFocus。
这是我用于此目的的一个示例:
@Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
return false;
}
});
}