Fragment中監聽onKey事件,沒你想象的那麼難。
阿新 • • 發佈:2019-02-19
專案中越來越多的用到Fragment,在用Fragment取代TabHost的時候遇到了一個問題,我們都知道,TabHost的Tab為Activity例項,有OnKey事件,但是Fragment中沒有,但是又必須監聽OnKey事件怎麼辦(不僅僅是退出哦),如果僅僅是退出我們可以在Activity中進行統一處理.
下面記錄一下在ActionBar中監聽Fragment的onKey事件。
ActionBar實現Onkey事件,判斷當前的fragment是哪一個,是不是所需要的Fragment,然後在需要監聽OnKey事件的Fragment中寫一個靜態方法,傳遞keycode與event事件即可。
package info.androidhive.tabsswipe; import info.androidhive.tabsswipe.adapter.TabsPagerAdapter; import android.annotation.SuppressLint; import android.app.SearchManager; import android.content.Context; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBarActivity;import android.support.v7.widget.SearchView; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @SuppressLint("NewApi") public class MainActivity extends ActionBarActivity implements ActionBar.TabListener { private ViewPager viewPager; private TabsPagerAdapter mAdapter; private ActionBar actionBar; private Fragment fg; // Tab titles private String[] tabs = { "TopRatedFragment", "GamesFragment", "MoviesFragment" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initilization viewPager = (ViewPager) findViewById(R.id.pager); actionBar = getSupportActionBar(); mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setOffscreenPageLimit(3); viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Adding Tabs for (String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name) .setTabListener(this)); } /** * on swiping the viewpager make respective tab selected * */ viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { public void onPageSelected(int position) { // on changing the page // make respected tab selected // actionBar.setSelectedNavigationItem(position); actionBar.selectTab(actionBar.getTabAt(position)); mAdapter.getItem(position); } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageScrollStateChanged(int arg0) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.action_settings); SearchView searchview = (SearchView)MenuItemCompat.getActionView(searchItem); searchview.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); return super.onCreateOptionsMenu(menu); } public void onTabReselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) { // TODO Auto-generated method stub } public void onTabSelected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) { // TODO Auto-generated method stub viewPager.setCurrentItem(arg0.getPosition()); fg = mAdapter.getItem(arg0.getPosition()); Log.d("fg", fg+""); } public void onTabUnselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) { // TODO Auto-generated method stub } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub Log.d("ActionBar", "OnKey事件"); if(fg instanceof GamesFragment){ GamesFragment.onKeyDown(keyCode, event); } return super.onKeyDown(keyCode, event); } }
其中一個Fragment
package info.androidhive.tabsswipe; import info.androidhive.tabsswipe.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public class GamesFragment extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view == null) { view = inflater.inflate(R.layout.fragment_games, container, false); } ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeView(view); } return view; } @Override public void onResume() { super.onResume(); // 判斷當前fragment是否顯示 if (getUserVisibleHint()) { showdata(); } } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); // 每次切換fragment時呼叫的方法 if (isVisibleToUser) { showdata(); } } private void showdata() { Toast.makeText(getActivity(), "Game", Toast.LENGTH_LONG).show(); } public static boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == event.KEYCODE_BACK) { Log.d("GameFragmet事件", "OK"); } return true; } }
最後當我在Fragment中觸發Onkey事件後列印日誌