我有我的sidemenu,我也有一些片段,我替换了我的片段这是我的来源:
public class MainActivity extends FragmentActivity {
public static ScrollerLinearLayout sideSlideLayout;
public static ListView slidemenulistview;
private StradaSlideMenuAdapter slidemenuadapter;
public static int fragmentposition=0;
private String[] menu_namesGeo = {
"Home", "Door", "street"};
int[] menu_images = {
R.drawable.menunewsimg, R.drawable.menuchartimg, R.drawable.menunearimg
};
public static ImageView menuButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sideSlideLayout = (ScrollerLinearLayout) findViewById(R.id.menu_content_side_slide_layout);
menuButton = (ImageView) findViewById(R.id.main_tmp_button);
slidemenulistview = (ListView) findViewById(R.id.slidemenulistview);
slidemenulistview.setEnabled(false);
menuButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
slidemenulistview.setEnabled(true);
sideSlideLayout.scroll();
}
});
slidemenuadapter = new StradaSlideMenuAdapter(MainActivity.this,
menu_namesGeo, menu_images);
slidemenulistview.setAdapter(slidemenuadapter);
slidemenulistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int possition, long arg3) {
//btn_categorry.setEnabled(true);
navigateTo(possition);
sideSlideLayout.scroll();
}
});
navigateTo(0);
}
private void navigateTo(int position) {
switch (position) {
case 0:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,
MainPageViewpager.newInstance(),
MainPageViewpager.TAG).commit();
fragmentposition=0;
break;
case 1:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,
DealBoxNearFragment.newInstance(),
DealBoxNearFragment.TAG).commit();
fragmentposition=1;
break;
case 2:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,
DealBoxAddBalanceFragment.newInstance(),
DealBoxAddBalanceFragment.TAG).commit();
fragmentposition=2;
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK &&fragmentposition==0)
{
Toast.makeText(getApplicationContext(), "First fragment", Toast.LENGTH_SHORT).show();
return true;
}
else if(keyCode == KeyEvent.KEYCODE_BACK &&fragmentposition==1)
{
onBackPressed();
return true;
}
return false;
}
}
我写了onKeyDownlistener
,但我的代码不起作用。我需要每次都控制片段的替换。我可以编写代码吗?如果有人知道解决方案,请帮助我
感谢
答案 0 :(得分:2)
您需要将片段添加到导航后台堆栈。
Android的文档:http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
答案 1 :(得分:1)
你必须覆盖OnBackPressed而不是OnKeyDown来拦截系统默认'
@Override
public void onBackPressed() {
if(I_WANT_TO_INTERCEPT){
//Control your fragment transaction here
}
else
{
super.onBackPressed();
}
}
` 请注意,如果您不调用超级方法,您的应用程序将无法正常工作。