其硬件後退按鈕時,側邊菜單我的菜單代碼着隱藏被點擊
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Menu();
init();
}
public void init(){
LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
mRecomended.setOnClickListener(new OnClick());
LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
mLive.setOnClickListener(new OnClick());
LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
mLike.setOnClickListener(new OnClick());
LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
mFavorite.setOnClickListener(new OnClick());
LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
mHistory.setOnClickListener(new OnClick());
LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
mFeedback.setOnClickListener(new OnClick());
LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
mSettings.setOnClickListener(new OnClick());
}
private class OnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
switch (v.getId()) {
case R.id.recomended:
intent.setClass(MainActivity.this,MainActivity.class);
bundle.putString("theme", "Recomended");
bundle.putString("From", "MainActivity");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Recomended");
break;
case R.id.live:
intent.setClass(MainActivity.this,LiveActivity.class);
bundle.putString("theme", "Live");
bundle.putString("From", "MainActivity");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Live");
break;
default:
break;
}
}
}
public void Menu(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//remove ToolBar Tittle
getSupportActionBar().setDisplayShowTitleEnabled(false);
// configure the SlidingMenu
final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
// setting menu width
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.left_fragment);
ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
tool_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menu.toggle();
}
});
menu.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
public void Bundle(){
Bundle getbundle =this.getIntent().getExtras();
ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
switch(getbundle.getString("theme")){
case "Recomended":
iRecomended.setVisibility(View.VISIBLE);
break;
case "Live":
ImageView iLive = (ImageView)findViewById(R.id.live_line);
iLive.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Like":
ImageView iLike = (ImageView)findViewById(R.id.like_line);
iLike.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Favorite":
ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
iFavorite.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "History":
ImageView iHistory = (ImageView)findViewById(R.id.history_line);
iHistory.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Feedback":
ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
iFeedback.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Settings":
ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
iSettings.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
}
}}
和我下一頁
public class LiveActivity extends AppCompatActivity {
String[] name = {"1","1","1","1"};
String[] nickname = {"MidnightSnack","MidnightSnack","MidnightSnack","MidnightSnack"};
String[] date = {"March 1,2016","March 1,2016","March 1,2016","March 1,2016"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.live_main);
Menu();
Bundle();
RecycleView();
init();
}
public void Bundle(){
Bundle getbundle =this.getIntent().getExtras();
ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
switch(getbundle.getString("theme")){
case "Recomended":
iRecomended.setVisibility(View.VISIBLE);
break;
case "Live":
ImageView iLive = (ImageView)findViewById(R.id.live_line);
iLive.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Like":
ImageView iLike = (ImageView)findViewById(R.id.like_line);
iLike.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Favorite":
ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
iFavorite.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "History":
ImageView iHistory = (ImageView)findViewById(R.id.history_line);
iHistory.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Feedback":
ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
iFeedback.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Settings":
ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
iSettings.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
}
}
public void Menu(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//remove ToolBar Tittle
getSupportActionBar().setDisplayShowTitleEnabled(false);
final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
// setting menu width
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.left_fragment);
ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
tool_menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.showMenu();
}
});
}
public void RecycleView(){
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ArrayList<PostOfLiveActivity> data = new ArrayList<>();
for(int i = 0; i < name.length; i++) {
data.add(new PostOfLiveActivity(name[i], null, nickname[i],date[i]));
}
AdapterOfLiveActivity adapter = new AdapterOfLiveActivity(this, data);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public void init(){
LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
mRecomended.setOnClickListener(new OnClick());
LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
mLive.setOnClickListener(new OnClick());
LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
mLike.setOnClickListener(new OnClick());
LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
mFavorite.setOnClickListener(new OnClick());
LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
mHistory.setOnClickListener(new OnClick());
LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
mFeedback.setOnClickListener(new OnClick());
LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
mSettings.setOnClickListener(new OnClick());
}
private class OnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
switch (v.getId()) {
case R.id.recomended:
intent.setClass(LiveActivity.this,MainActivity.class);
bundle.putString("theme", "Recomended");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Recomended");
break;
case R.id.live:
intent.setClass(LiveActivity.this,LiveActivity.class);
bundle.putString("theme", "Live");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Live");
break;
default:
break;
}
}
}
}
當我LiveActivity單擊硬件後退按鈕,回MainActivity但我的幻燈片菜單也打開了,我怎麼能隱藏他們,當我點擊硬件後退按鈕?
感謝您的回答,但我仍然從這個錯誤,我不知道爲什麼它不工作,它太壞 –
請確保滑動菜單實例是正確的。把'private SlidingMenu menu'放在方法外部作爲一個全局變量,然後將其正確地實例化爲'menu = new SlidingMenu(this);'在Menu()方法內部。確保你刪除了'final'關鍵字 – HendraWD
它對我的工作!非常感謝你 我只是忘了刪除「最終」!!!!! –