Chinaunix首页 | 论坛 | 博客
  • 博客访问: 22865
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-25 14:52:57

      wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改。本文分享android开发Fragment中监听onKey事件主要代码,供技术员参考学习。

      项目中越来越多的用到Fragment,下面记录一下在ActionBar中监听Fragment的onKey事件。

  ActionBar实现Onkey事件,判断当前的fragment是哪一个,是不是所需要的Fragment,然后在需要监听OnKey事件的Fragment中写一个静态方法,传递keycode与event事件即可。

点击(此处)折叠或打开

  1. package info.androidhive.tabsswipe;
  2. import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
  3. import android.annotation.SuppressLint;
  4. import android.app.SearchManager;
  5. import android.content.Context;
  6. import android.support.v7.app.ActionBar;
  7. import android.support.v7.app.ActionBar.Tab;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.support.v7.widget.SearchView;
  10. import android.os.Bundle;
  11. import android.support.v4.app.Fragment;
  12. import android.support.v4.view.MenuItemCompat;
  13. import android.support.v4.view.ViewPager;
  14. import android.util.Log;
  15. import android.view.KeyEvent;
  16. import android.view.Menu;
  17. import android.view.MenuItem;

  18. @SuppressLint("NewApi")
  19. public class MainActivity extends ActionBarActivity implements
  20.         ActionBar.TabListener {

  21.     private ViewPager viewPager;
  22.     private TabsPagerAdapter mAdapter;
  23.     private ActionBar actionBar;
  24.     private Fragment fg;
  25.     // Tab titles
  26.     private String[] tabs = { "TopRatedFragment", "GamesFragment", "MoviesFragment" };

  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);

  31.         // Initilization
  32.         viewPager = (ViewPager) findViewById(R.id.pager);
  33.         actionBar = getSupportActionBar();
  34.         mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
  35.         viewPager.setOffscreenPageLimit(3);
  36.         viewPager.setAdapter(mAdapter);
  37.         actionBar.setHomeButtonEnabled(false);
  38.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

  39.         // Adding Tabs
  40.         for (String tab_name : tabs) {
  41.             actionBar.addTab(actionBar.newTab().setText(tab_name)
  42.                     .setTabListener(this));
  43.         }

  44.         /**
  45.          * on swiping the viewpager make respective tab selected
  46.          * */
  47.         viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

  48.             public void onPageSelected(int position) {
  49.                 // on changing the page
  50.                 // make respected tab selected
  51. // actionBar.setSelectedNavigationItem(position);
  52.                 actionBar.selectTab(actionBar.getTabAt(position));
  53.                 mAdapter.getItem(position);
  54.                 
  55.             }

  56.             public void onPageScrolled(int arg0, float arg1, int arg2) {
  57.             }

  58.             public void onPageScrollStateChanged(int arg0) {
  59.             }
  60.         });
  61.     }

  62.     @Override
  63.     public boolean onCreateOptionsMenu(Menu menu) {
  64. //
  65.         SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  66.         getMenuInflater().inflate(R.menu.main, menu);
  67.         MenuItem searchItem = menu.findItem(R.id.action_settings);
  68.         SearchView searchview = (SearchView)MenuItemCompat.getActionView(searchItem);
  69.         searchview.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  70.         return super.onCreateOptionsMenu(menu);
  71.     }
  72.     
  73.     
  74.     public void onTabReselected(Tab arg0,
  75.             android.support.v4.app.FragmentTransaction arg1) {
  76.         // TODO Auto-generated method stub
  77.         
  78.     }

  79.     public void onTabSelected(Tab arg0,
  80.             android.support.v4.app.FragmentTransaction arg1) {
  81.         // TODO Auto-generated method stub
  82.         viewPager.setCurrentItem(arg0.getPosition());
  83.         fg = mAdapter.getItem(arg0.getPosition());
  84.         Log.d("fg", fg+"");
  85.     }

  86.     public void onTabUnselected(Tab arg0,
  87.             android.support.v4.app.FragmentTransaction arg1) {
  88.         // TODO Auto-generated method stub
  89.         
  90.     }

  91.     @Override
  92.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  93.         // TODO Auto-generated method stub
  94.         Log.d("ActionBar", "OnKey事件");
  95.         if(fg instanceof GamesFragment){
  96.             GamesFragment.onKeyDown(keyCode, event);
  97.         }
  98.         return super.onKeyDown(keyCode, event);
  99.     }
  100. }

原文详情地址:http://git.oschina.net/einsqing/wemall-mobile

wemall-mobile商城详情地址:http://www.koahub.com/home/product/56

wemall官网地址:http://www.wemallshop.com

WeMall - 开源微商城 微信商城 商城源码 分销商城 b2b2c商城系统

阅读(145) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~